aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

commit 8d65bf401ae1870e22179b8fdcad61fb21eadaec (patch)
parent 6b5365fed6dc484ae3cb66175b7eacbf538a4662
Author: Alex Karle <alex@alexkarle.com>
Date:   Sat,  7 Dec 2024 02:50:30 +0100

2024: Day 6 pt 2

I still feel like there's a smarter answer here, but this works!

Diffstat:
M2024/06/1.py | 1-
A2024/06/2.py | 88+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 88 insertions(+), 1 deletion(-)

diff --git a/2024/06/1.py b/2024/06/1.py @@ -65,7 +65,6 @@ if __name__ == '__main__': seen = set() while guard[0] is not None: - oldguard = guard seen.add(guard) guard, dir = move(G, guard, dir) diff --git a/2024/06/2.py b/2024/06/2.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python3 +import sys + +def parse(): + G = [] + guard = (None, None) + r = 0 + for l in sys.stdin: + G.append([x for x in l.strip()]) + if "^" in l: + guard = (r, l.index("^")) + r += 1 + + if not guard[0]: + raise Exception("Unable to find Guard") + + return G, guard + +def turn(dir): + if dir == "^": + return ">" + elif dir == ">": + return "v" + elif dir == "v": + return "<" + elif dir == "<": + return "^" + else: + raise Exception(f"Bad direction {dir}") + +def move(G, guard, dir): + if dir == "^": + next = (guard[0] - 1, guard[1]) + elif dir == "v": + next = (guard[0] + 1, guard[1]) + elif dir == ">": + next = (guard[0], guard[1] + 1) + elif dir == "<": + next = (guard[0], guard[1] - 1) + else: + raise Exception(f"Bad direction {dir}") + + if next[0] >= len(G) or next[0] < 0 or next[1] >= len(G[0]) or next[1] < 0: + # off the map + return ((None, None), None) + + if G[next[0]][next[1]] == "#": + dir = turn(dir) + return (guard, dir) + + return (next, dir) + +def pprint(G, guard, dir): + for i in range(len(G)): + for j in range(len(G[0])): + if (i, j) == guard: + print(dir, end='') + else: + print(G[i][j], end='') + print("") + +def check(G, guard, dir): + seen = set() + while guard[0] is not None: + seen.add((guard, dir)) + guard, dir = move(G, guard, dir) + if dir is None: + return seen + if (guard, dir) in seen: + return None + +if __name__ == '__main__': + G, guard = parse() + dir = "^" + + regseen = check(G, guard, dir) + sq2chk = { x[0] for x in regseen if x[0] != guard } + + c = 0 + for sq in sq2chk: + print(sq) + i, j = sq + G[i][j] = '#' + if check(G, guard, dir) is None: + c += 1 + G[i][j] = '.' + + print(c)