-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol.py
108 lines (66 loc) · 2.03 KB
/
sol.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import re
from typing import Tuple, Callable, Iterable, Optional
import sys
import math,functools,itertools
fname=sys.argv[1] if len(sys.argv)>1 else "input"
f = open(fname)
ftext=f.read()
from collections import defaultdict
#copied from https://github.com/joefarebrother/adventofcode/blob/master/misc_utils.py
def lmap(f: Callable, *xs) -> list:
return list(map(f, *xs))
def mint(x, default=None):
try: return int(x)
except ValueError: return default
def ints(x: str) -> list[int]:
ex = r'(?:(?<!\d)-)?\d+'
return lmap(int,re.findall(ex, x))
def ints_locs(x: str) -> list[tuple[int,tuple[int,int]]]:
ex = r'(?:(?<!\d)-)?\d+'
return [(int(x.group()),x.span()) for x in re.finditer(ex, x)]
class Pt(tuple):
def __add__(self,other):
return Pt([x+y for x,y in zip(self,other,strict=True)])
def __sub__(self,other):
return Pt([x-y for x,y in zip(self,other,strict=True)])
def __rsub__(self,other):
return Pt([y-x for x,y in zip(self,other,strict=True)])
def __radd__(self,other):
return Pt([y+x for x,y in zip(self,other,strict=True)])
def __rmul__(self,other):
return Pt([other*x for x in self])
def left(self):
x,y=self
return Pt((y,-x))
def right(self):
x,y=self
return Pt((-y,x))
def pt(*args):
if len(args)==1: return Pt(args[0])
else: return Pt(args)
dirs = {
"R":Pt((1,0)),
"U":Pt((0,-1)),
"L":Pt((-1,0)),
"D":Pt((0,1))
}
ods = list(dirs.values())
#odirs = [(0,1),(1,0),(0,-1),(-1,0)]
ddirs = lmap(Pt,[(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1),(-1,0),(-1,1)])
##Input handling
flns=[line.strip() for line in ftext.split("\n")[:-1]]
f=flns
fgroups = ftext.split("\n\n")
if len(fgroups)>1:
fgs = [g.split("\n") for g in fgroups]
fgns = [lmap(ints,g) for g in fgs]
try: xs = lmap(int,f)
except Exception: pass
try: xss = lmap(ints,f)
except Exception: pass
tot=0
d=defaultdict(int)
for y,line in enumerate(flns):
for x,c in enumerate(line):
d[x,y]=c
print(tot)