-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoctools.py
212 lines (172 loc) · 6.35 KB
/
aoctools.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import os
import requests
def toArray(lst, *args):
if len(args) == 0:
return lst
N, *args = args
return toArray([toArray(lst[i:i+N]) for i in range(0, len(lst), N)], *args)
def makeArray(value, *args):
if len(args) == 1:
if isinstance(value, (set, list)):
value = value.__class__(x for x in value)
return [value]*args[0]
N, *args = args
return list(makeArray(value, *args)[:] for x in range(N))
def array2DToDict(array):
ret = {}
for x, r in enumerate(array):
for y, c in enumerate(r):
ret[(x, y)] = c
return ret, x+1, y+1
def transpose2d(A):
return [list(x) for x in zip(*A)]
from collections import namedtuple
Point = namedtuple('Point', ('x', 'y'))
class Pt(Point):
def __add__(self, rhs):
return Pt(self.x + rhs[0],
self.y + rhs[1])
def __sub__(self, rhs):
return Pt(self.x - rhs[0],
self.y - rhs[1])
def __mul__(self, rhs):
return Pt(self.x*rhs, self.y*rhs)
def __div__(self, rhs):
return Pt(self.x/rhs, self.y/rhs)
@property
def pt(self):
return (self.x, self.y)
def dir(self):
return Pt(self.x/(abs(self.x) if self.x != 0 else 1),
self.x/(abs(self.y) if self.y != 0 else 1))
def move(self, dir, d = 1):
return Pt(self.x + dir[0]*d,
self.y + dir[1]*d)
def n(self, d = 1):
return Pt(self.x, self.y + d)
def s(self, d = 1):
return Pt(self.x, self.y - d)
def e(self, d = 1):
return Pt(self.x + d, self.y)
def w(self, d = 1):
return Pt(self.x - d, self.y)
def ne(self, d = 1):
return Pt(self.x + d, self.y + d)
def nw(self, d = 1):
return Pt(self.x - d, self.y + d)
def se(self, d = 1):
return Pt(self.x + d, self.y - d)
def sw(self, d = 1):
return Pt(self.x - d, self.y - d)
def gridNbrs(self, bl = None, tr = None):
ret = [self.n(), self.s(), self.e(), self.w()]
if tr:
return [x for x in filter(lambda x: x.inrange(bl, tr), ret)]
return ret
def allNbrs(self, bl = None, tr = None):
ret = [self.n(), self.ne(), self.e(), self.se(), self.s(), self.sw(), self.w(), self.nw()]
if tr:
return [x for x in filter(lambda x: x.inrange(bl, tr), ret)]
return ret
def inrange(self, bl, tr):
return bl.x <= self.x <= tr.x and bl.y <= self.y <= tr.y
dir1 = Pt(0, 0).gridNbrs()
dir2 = Pt(0, 0).allNbrs()
def turnRight(d):
return dir1[ (dir1.index(d) + 1) % 4 ]
def turnLeft(d):
return dir1[ (dir1.index(d) - 1) % 4 ]
from queue import PriorityQueue # essentially a binary heap
def dijkstra_p(start, start_wt, goal, G):
""" Uniform-cost search / dijkstra """
visited = set()
cost = {start: start_wt}
parent = {start: None}
todo = PriorityQueue()
todo.put((0, start))
while todo:
while not todo.empty():
_, vertex = todo.get() # finds lowest cost vertex
# loop until we get a fresh vertex
if vertex not in visited: break
else: # if todo ran out
break # quit main loop
visited.add(vertex)
if vertex == goal:
break
for distance, neighbor in G(vertex):
if neighbor in visited: continue # skip these to save time
old_cost = cost.get(neighbor, float('inf')) # default to infinity
new_cost = cost[vertex] + distance
if new_cost < old_cost:
todo.put((new_cost, neighbor))
cost[neighbor] = new_cost
parent[neighbor] = vertex
return parent, visisted
import heapq
def dijkstra(startNode, startWt, targetNode, nbrFn):
"""
:param startNode: should be hashable
:param startWt: starting weight: typically zero
:param targetNode: node to be reached
:param nbrFn(p): An fn returning neighbors of p [ (wt1, nb1), (wt2, nb2) ...]
:return: shortedRoute, solvedRoutes
"""
todo = []
heapq.heapify(todo)
heapq.heappush(todo, (startWt, startNode, None))
remaining = {}
remaining[startNode] = startWt
sptset = {}
while remaining:
#print ("remaining = ", len(remaining))
wt, p, parent = heapq.heappop(todo)
if p in sptset: continue
sptset[p] = (wt, parent)
if (callable(targetNode) and targetNode(p)) or p == targetNode:
break
del remaining[p]
for v, n in nbrFn(p):
t = v + wt
found = sptset.get(n)
#if found and found[0] > t:
# print ("oops", n, t, found)
if found: continue
nv = remaining.get(n)
if not nv or nv > t:
remaining[n] = t
heapq.heappush(todo, (t, n, p))
return wt, p, sptset[p], sptset
session_id = os.getenv("AOC_SESSIONID")
user_agent = os.getenv("AOC_USERAGENT")
_headers = {'User-Agent': user_agent}
def get_input(day, year):
uribase = r"https://adventofcode.com/" + str(year) + '/day/' + str(day)
uri = uribase + r"/input"
try:
inp = open(str(day) + r'.txt').read()
except Exception:
inp = requests.get(uri, cookies=dict(session=session_id), headers=_headers)
with open(str(day) + r'.txt', 'w') as f:
f.write(inp.text)
inp = inp.text
return inp
def submit_result(day, year, level, result):
if result is not None:
uribase = r"https://adventofcode.com/" + str(year) + '/day/' + str(day)
response = requests.post(uribase + "/answer", cookies={"session": SESSIONID},
headers=_headers,
data={"level": level, "answer": result})
if response.ok:
if "That's the right answer" in response.text:
print("Success!")
elif " low" in response.text:
print("Too low :-(")
elif " high" in response.text:
print("Too high :-(")
else:
print("Wrong answer :-(")
print(response.text)
else:
print("Response not OK")
print(response.text)