commit 6b5365fed6dc484ae3cb66175b7eacbf538a4662 (patch) parent e1dbb7ae0e8583634c92ea05d148cf43b183f889 Author: Alex Karle <alex@alexkarle.com> Date: Fri, 6 Dec 2024 15:14:54 +0100 2024: Add day 6 part 1 Diffstat:
A | 2024/06/1.py | | | 72 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | 2024/06/input.txt | | | 130 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | 2024/06/sample.txt | | | 10 | ++++++++++ |
3 files changed, 212 insertions(+), 0 deletions(-)
diff --git a/2024/06/1.py b/2024/06/1.py @@ -0,0 +1,72 @@ +#!/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("") + +if __name__ == '__main__': + G, guard = parse() + dir = "^" + + seen = set() + while guard[0] is not None: + oldguard = guard + seen.add(guard) + guard, dir = move(G, guard, dir) + + print(len(seen)) diff --git a/2024/06/input.txt b/2024/06/input.txt @@ -0,0 +1,130 @@ +.........#..................#.......#...............#..................................#.....................................#.... +..#....................................#...#......#....................#.......................................................... +..........................................................#..#.#................#............#....................#............... +...........................................................#......................................................#............... +............................#..........#................................................#............#.#....................#..... +...........#............................#............................................#...........................................# +......#..................#..#...........#.......................#..........#.....................#...........#...#......#.....#... +...#........................................................................................................#.............#....... +.......................#....................................#.....................#...#.#.....##.........................#........ +..............#........#..............................................................................#..........#........#....... +.#........................................................................#....#..#....#.....................................#.... +..........#.....................#....#.#.......................#................#.#.............#.......#.......#..........#...... +......#..#...............................#.###......................................................#...#......................... +...............................................#....................#...#.......................#...........#...#................. +....#...#..#.............#................................................................#....................................... +...............#...#............#....................#.............#................#.......#....#.......................#........ +.........#..........................#............#..............................................................#.#............... +..........................##..............................#....................................#.................................. +.......#..##.........................#..............#..#......#..............#...............................##................... +.............................................#........................................................................#....#...... +.........................#.......#.......#.......##.#.........#................................................................... +#......................................................................................#.......#......#.#................#......#. +...#.............#....#.............#.....................#................................................................#...... +...........#.....................#............................#..............................#.....#........#..................... +.................#....#.#............#.....#................#.....................#..#............................................ +..........................#......................#...............#.#...#..............................#.........#................. +...........................................................#......................................................#......#........ +........#.........................#.................................#.....................................#....................... +....................#........#.....................................#...............#................#.......................#..... +....#...............#............................................................................................................. +##..................................................................................#.....................................#..#.... +........................#..............#......#...................................#.........#.......#.....#.....#................. +.........#.............#......................#............#.......................................................#..........#... +.....#...............#......................................................................#.........#..................#........ +....................#...#..............#...............#.............#.............#......#..#......#................#......#..... +............#..........................................#.........................................#........#....................... +.......#.....#........#....................#...............#............................................................#......... +..................#..#.............................................#........#..........................................#.......... +...................................#...............................................................#.......#................#.#... +.......................#.....#.........#..................##........#............................................................. +.....#.................................................................................#............#........................#.... +....#..........#.........#................................#.....................................#..................##............. +.#.........#..............#.................#...............................................#...............................#..... +........................#.......................................................#.....#................#..............#........... +.........................#.............................#.......#..................##..........................................#... +...................#...................#............#......#......................##........................#...#.....#........... +......#......#....#......................................................................................#........................ +#............#........##............................#............................................#....#.......#.................#. +...........................................................#..............................................#.......#...#........... +...##...#............#.....#...............#..................#....................#..............#.............................#. +.....................................................................#................................#.......#.................#. +......#......#.......................................................#....#.................#.....................#...#.#....#.#.. +...................#.........#..............#.................................#...#.......#....................................... +........##.....#.......#............................#...................................................................#......... +...................................#.........................................................................#...#...........#.... +....#.....................................#......................................................#...#..........#.....#........... +..................................................................................................#........#....................#. +.........#....................##.................................#.......................#........#......#......................#. +.....#.....#....#............#..............#....#.........#............#.#...............#.......................#............... +...................................#.......................#..........................#.....................................#....# +..........#....#...........#.........#..............#.....#................................................#.........#............ +...#.........#.................................................................................................................... +.#.........................#........................................#.....#...................................#.............#..... +.......................#...#...#...#.......##..................##......................................#........................#. +............................................#....#..........................................##............................#....... +..#.................................................................#....#....#....##...........................................#. +.............#........#.#........#.....................##.....................................................#..#................ +............................................##.......................................................................##........... +.........#...................#.................................#....#.#.............................#......#.#.#.....#.........#.. +.........................#...........#...........#............................................#......#..................#......... +#.............................##...............#..........#...................#......................................#........#... +...................#.............................................................................................#.......#........ +..........................................#.#......................................#........#...........##.......................# +...................#........#.#......#....................................#.......#...........................#..................# +......#................................................................................................#.......................... +......................###.............#...................#.#...............................................#..................... +.....#......#......#.....#.#..................#.#...#.....................................................................#....... +.............................................#..............#....................##............................................... +.........................................#..#.............#................................................#...................#.# +........#............................#.................................................#..#....................................... +............#.............................#......................................#..........................##...#................ +...................#.........#..................................................................#.............#................... +..#..#..............#....................#...................................................................#.................... +............................#..................#.................................................................................. +.........................#.....................................................................................#...............#.. +.........#....................#.#.............##..................#...#........................................................... +........#..............................#..........................................#.#.........................#................... +.#........#....................##.............................................#............#...................................... +...#.....................................#......#.#..#.....#............................#.......................................#. +....................#.........................................#.#.................................#.........#................#.... +..............#..........#......#.........................#..#..................#..#...................#.................#...#.... +................#..........................................#.............................#......#..............#.#...##........... +........................#.....#.......................#...#................#.......#................#............................. +........#..#..........#....#..#.............................................#...................#........#.................#.#.... +...........................#.............................................................#..#................................#.#.. +...........#.............#........................................#.^.................................................#........... +..........#.#..#......#........................................................................................#....#............. +...............#......#...#....#..................................#.......#....................................................... +..........##...##....#.....................#...#................................................................#......#.......... +.............................................#................................................................................###. +........#.......#..........#....#....................#....................#....#.....#............................................ +......#.............................................................................#...........##...#......##.................... +.......#.............................#............#.......................#............#...#...................................... +..#........#.....#......................................................................................................#......... +.....#....#......................#.....................................................................#.......................... +.#.......#...........#....#.......#..............#...#..............#........#..........##.....#......#.................#......... +.............#.#....................................................#........#..............#.....#......................#...#.... +...........#.............................................#...........................#.........##................#..#.....#....... +.........................................#.................................................................#.......#.#............ +...............#....................#..........................#.............#......#....#...#..............#..................#.. +........#...............#......................................................................................................... +..............................#............................#.#...................................#........#................#...... +...........#........#.............................#...............................#................#.............#........#....... +.............................#...#..#...........#...........#.............................#..............................#........ +......................................................................................................................#........#.. +..............##...........#..........................................#...#..............#........................................ +..............#.................#......................................................#..#........#..........................#... +..............................................#.......................#........................................................#.. +.................#..#...................#..............................#.......................................#.#......#......... +...........#..........#................#.............#...................................................#.......#.....#.......... +...#...#...............................................................................................#................#..#.#.#.. +...#.....................................................................................#...............................#........ +..........................#............................#.#..#..................#.#.......................#.#................##...# +...............................................................................................#....##............................ +............................................#....................#....................................................#........... +...............#.......#...#.................................#.......................................#...#..#.#................... +.......#.....#.....#..................#..............#................#..........#........#....................................... +...#...#..........##....................#.......#.......#...........................#....................................#.....#.. +..................................................#...#.#...#.........#.#.#.......##.....##.................................#..... +...#..#..........................#..#...........................#...............#.....#.......#............................#...... diff --git a/2024/06/sample.txt b/2024/06/sample.txt @@ -0,0 +1,10 @@ +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#...