Compare commits
2 Commits
43767f7b62
...
bde0a5d622
Author | SHA1 | Date | |
---|---|---|---|
bde0a5d622 | |||
9ac4782157 |
83
2021/d15.py
Normal file
83
2021/d15.py
Normal file
@ -0,0 +1,83 @@
|
||||
from lib import get_data
|
||||
from lib import Grid2D
|
||||
import heapq
|
||||
|
||||
data = get_data(__file__)
|
||||
|
||||
|
||||
def new_data(data, repeat=5):
|
||||
new_lines = []
|
||||
for line in data.splitlines():
|
||||
new_line = str(line)
|
||||
for i in range(len(line) * repeat):
|
||||
ni = i - len(line)
|
||||
if ni < 0:
|
||||
continue
|
||||
nc = int(new_line[ni]) + 1
|
||||
if nc == 10:
|
||||
nc = 1
|
||||
new_line += str(nc)
|
||||
new_lines.append(new_line)
|
||||
|
||||
for i in range(len(data.splitlines()) * repeat):
|
||||
ni = i - len(data.splitlines())
|
||||
if ni < 0:
|
||||
continue
|
||||
line = list(new_lines[ni])
|
||||
for i in range(len(line)):
|
||||
nc = int(line[i]) + 1
|
||||
if nc == 10:
|
||||
nc = 1
|
||||
line[i] = str(nc)
|
||||
line = "".join(line)
|
||||
new_lines.append(line)
|
||||
|
||||
data = "\n".join(new_lines)
|
||||
return data
|
||||
|
||||
|
||||
big_data = new_data(data)
|
||||
|
||||
for data in [data, big_data]:
|
||||
g = Grid2D(data)
|
||||
|
||||
def dist(n1, n2):
|
||||
"""cost from node to node"""
|
||||
if n1 == 0:
|
||||
return 0
|
||||
return int(g[n2])
|
||||
|
||||
def h(node):
|
||||
"""heuristic function (never overestimate)"""
|
||||
return abs(g.n_rows - 1 - node[0]) + abs(g.n_cols - 1 - node[1])
|
||||
|
||||
def is_goal(node):
|
||||
return node == (g.n_rows - 1, g.n_cols - 1)
|
||||
|
||||
def neighbors(node):
|
||||
return list(g.neighbors_ort(node))
|
||||
|
||||
starts = [(0, 0)]
|
||||
open_set = []
|
||||
g_score = {}
|
||||
cost = None
|
||||
|
||||
for start in starts:
|
||||
heapq.heappush(open_set, (h(start), start))
|
||||
g_score[start] = dist(0, start)
|
||||
|
||||
while open_set:
|
||||
current_f_score, current = heapq.heappop(open_set)
|
||||
if is_goal(current):
|
||||
assert current_f_score == g_score[current]
|
||||
cost = g_score[current]
|
||||
break
|
||||
|
||||
for neighbor in neighbors(current):
|
||||
tentative_g_score = g_score[current] + dist(current, neighbor)
|
||||
if neighbor not in g_score or tentative_g_score < g_score[neighbor]:
|
||||
g_score[neighbor] = tentative_g_score
|
||||
f_score = g_score[neighbor] + h(neighbor)
|
||||
heapq.heappush(open_set, (f_score, neighbor))
|
||||
|
||||
print(cost)
|
85
2021/d16.py
Normal file
85
2021/d16.py
Normal file
@ -0,0 +1,85 @@
|
||||
from lib import get_data
|
||||
|
||||
|
||||
data = get_data(__file__)
|
||||
|
||||
bs = ""
|
||||
for c in data.strip():
|
||||
b = bin(int(c, 16))[2:]
|
||||
b = "0" * (4 - len(b)) + b
|
||||
bs += b
|
||||
|
||||
total_v = 0
|
||||
|
||||
|
||||
def parse(bs: str, i: int):
|
||||
v = int(bs[i : i + 3], 2)
|
||||
global total_v
|
||||
total_v += v
|
||||
i += 3
|
||||
t = int(bs[i : i + 3], 2)
|
||||
i += 3
|
||||
|
||||
# print(v, t)
|
||||
if t == 4:
|
||||
lv = ""
|
||||
while True:
|
||||
seg = bs[i : i + 5]
|
||||
lv += seg[1:]
|
||||
i += 5
|
||||
if seg[0] == "0":
|
||||
break
|
||||
lv = int(lv, 2)
|
||||
return i, lv
|
||||
|
||||
vs = []
|
||||
if bs[i] == "0":
|
||||
# total length of bits
|
||||
i += 1
|
||||
bits = int(bs[i : i + 15], 2)
|
||||
i += 15
|
||||
i_max = i + bits
|
||||
while True:
|
||||
i, v = parse(bs, i)
|
||||
vs.append(v)
|
||||
if i >= i_max:
|
||||
break
|
||||
elif bs[i] == "1":
|
||||
# number of sub-packets
|
||||
i += 1
|
||||
num_sub = int(bs[i : i + 11], 2)
|
||||
i += 11
|
||||
vs = []
|
||||
for _ in range(num_sub):
|
||||
i, v = parse(bs, i)
|
||||
vs.append(v)
|
||||
else:
|
||||
assert False
|
||||
|
||||
if t == 0:
|
||||
return i, sum(vs)
|
||||
elif t == 1:
|
||||
x = 1
|
||||
for v in vs:
|
||||
x *= v
|
||||
return i, x
|
||||
elif t == 2:
|
||||
return i, min(vs)
|
||||
elif t == 3:
|
||||
return i, max(vs)
|
||||
elif t == 5:
|
||||
assert len(vs) == 2
|
||||
return i, 1 if vs[0] > vs[1] else 0
|
||||
elif t == 6:
|
||||
assert len(vs) == 2
|
||||
return i, 1 if vs[0] < vs[1] else 0
|
||||
elif t == 7:
|
||||
assert len(vs) == 2
|
||||
return i, 1 if vs[0] == vs[1] else 0
|
||||
else:
|
||||
assert False
|
||||
|
||||
|
||||
p2 = parse(bs, 0)[1]
|
||||
print(total_v)
|
||||
print(p2)
|
@ -186,7 +186,9 @@ Solutions and utility script for Advent of Code challenges in Python.
|
||||
- Day 12: 19:11 (Okayish, but of course too slow for leaderboard.)
|
||||
- Day 13: 16:48 (Should have been way faster.)
|
||||
- Day 14: 25:52 (Not hard but just too slow.)
|
||||
- Day 15:
|
||||
- Day 15: 19:17 (Not that bad. Multiplying the thing threw me off.)
|
||||
- Day 16: 50:01 (Way too slow. Was non-trivial but fun. Much better was feasible.)
|
||||
- Day 17:
|
||||
|
||||
## AoC 2022
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user