diff options
author | Marvin Borner | 2024-12-06 20:04:11 +0100 |
---|---|---|
committer | Marvin Borner | 2024-12-06 20:04:11 +0100 |
commit | b688bcf91df43653c6d39f0e58a7667f46c58165 (patch) | |
tree | 938021ae8a749883f3740477b12fb9bbfc740800 | |
parent | 016c9244ac904ebc19d05b02adbced07a669a1d4 (diff) |
idc
-rwxr-xr-x | 2024/06/solve.py | 65 |
1 files changed, 49 insertions, 16 deletions
diff --git a/2024/06/solve.py b/2024/06/solve.py index 9239aa9..36c1198 100755 --- a/2024/06/solve.py +++ b/2024/06/solve.py @@ -1,7 +1,7 @@ #!/bin/env python -L = [l.strip() for l in open("input").readlines()] +L = [list(l.strip()) for l in open("input").readlines()] UP = 0 RIGHT = 1 @@ -12,12 +12,8 @@ YLEN = len(L) XLEN = len(L[0]) -def start(): - for y, row in enumerate(L): - for x, col in enumerate(row): - if col == "^": - L[y] = "".join(map(lambda c: "." if c == "^" else c, L[y])) - return (y, x, UP) +def put(y, x, c): + L[y][x] = c def get(y, x): @@ -26,6 +22,14 @@ def get(y, x): return "#" +def start(): + for y, row in enumerate(L): + for x, col in enumerate(row): + if col == "^": + L[y][x] = "." + return y, x + + def move(y, x, d): if d == UP and get(y - 1, x) == ".": return (y - 1, x, UP) @@ -40,20 +44,49 @@ def move(y, x, d): return (y, x, (d + 1) % 4) -def part1(): - visits = set() - y, x, d = start() - visits.add((y, x)) - while not ( +def outside(y, x, d): + return ( (d == UP and y == 0) or (d == RIGHT and x == XLEN - 1) or (d == DOWN and y == YLEN - 1) or (d == LEFT and x == 0) - ): - y, x, d = move(y, x, d) - visits.add((y, x)) + ) + +def part1(y, x): + visits = set() + visits.add((y, x)) + p = (y, x, UP) + while not outside(*p): + p = move(*p) + visits.add((p[0], p[1])) print(len(visits)) -part1() +def part2(ys, xs): + res = 0 + + def loopy(y, x): + visits = set() + p = (y, x, UP) + while not outside(*p): + if p in visits: + return True + visits.add(p) + p = move(*p) + return False + + for y in range(YLEN): + for x in range(XLEN): + if get(y, x) == ".": + put(y, x, "#") + loops = loopy(ys, xs) + put(y, x, ".") + if loops: + res += 1 + print(res) + + +s = start() +part1(*s) +part2(*s) |