Solve 2024 day 6

This commit is contained in:
felixm 2024-12-06 00:18:48 -05:00
parent 63166ddce8
commit fae61ae6db
2 changed files with 57 additions and 0 deletions

55
2024/d6.py Normal file
View File

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

View File

@ -296,3 +296,5 @@ and focus. Of course, learning more algorithms and techniques helps.
- Day 3: `00:01:48 165 0 00:02:40 56 45` - Day 3: `00:01:48 165 0 00:02:40 56 45`
- Day 4: `00:07:47 1101 0 00:24:07 2410 0` - Day 4: `00:07:47 1101 0 00:24:07 2410 0`
- Day 5: `00:07:07 679 0 00:23:02 1998 0` - Day 5: `00:07:07 679 0 00:23:02 1998 0`
- Day 6: `00:09:43 1082 0 00:16:29 464 0`