Make day 6 a little nicer

This commit is contained in:
2024-12-06 00:28:42 -05:00
parent fae61ae6db
commit 36f8f709e1

View File

@@ -4,52 +4,48 @@ from lib import Grid2D
data = get_data(__file__) data = get_data(__file__)
DIRS = { DIRS = [
"N": (-1, 0), (-1, 0),
"E": (0, 1), (0, 1),
"S": (1, 0), (1, 0),
"W": (0, -1), (0, -1),
} ]
DIRSL = "NESW"
g = Grid2D(data) g = Grid2D(data)
t = 0 t = 0
for rr in range(g.n_rows): for rr in range(g.n_rows):
for cc in range(g.n_cols): for cc in range(g.n_cols):
(p,) = g.find("^") (p,), dir = g.find("^"), 0
seen = set() seen = set()
dirl = "N"
dir = DIRS["N"]
loop = False loop = False
if g[(rr, cc)] == "#" or g[(rr, cc)] == "^": if g[(rr, cc)] == "#":
continue continue
g[(rr, cc)] = "#" if g[(rr, cc)] != "^":
g[(rr, cc)] = "#"
while True: while True:
if (p, dir) in seen: if (p, dir) in seen:
loop = True loop = True
break break
seen.add((p, dir)) seen.add((p, dir))
r, c = p[0] + dir[0], p[1] + dir[1] r, c = p[0] + DIRS[dir][0], p[1] + DIRS[dir][1]
if not (0 <= r < g.n_rows and 0 <= c < g.n_cols): if not (0 <= r < g.n_rows and 0 <= c < g.n_cols):
break break
if g[(r, c)] == "#": if g[(r, c)] == "#":
dirl = DIRSL[(DIRSL.index(dirl) + 1) % 4] dir = (dir + 1) % 4
dir = DIRS[dirl]
else: else:
p = (r, c) p = (r, c)
g[(rr, cc)] = "." if g[(rr, cc)] == "^":
print(len(set(pos for pos, _ in seen)))
else:
g[(rr, cc)] = "."
if loop: if loop:
t += 1 t += 1
print(len(set(pos for pos, _ in seen)))
print(t) print(t)