Skip to content

Commit

Permalink
2024, day 6, part 1 (StopIteration)
Browse files Browse the repository at this point in the history
  • Loading branch information
BioGeek committed Dec 6, 2024
1 parent e611567 commit 42305a3
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 16 deletions.
130 changes: 130 additions & 0 deletions 2024/data/day06.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
........................#........#................#.............#..#....#.#.#.........#........##..................#..#...........
.#........#......##........#.................................................#.#.........#......#.................................
...........#.#.##.#.........#.#..#............#.....................................................#.............#...............
..........#...............#......#...............#.#..............................#..........#........#..#...##.........#..#......
..........................................................#...........................................#...........................
................#.....#.............#...........................................................#.....#..........#...#...##.......
.....................#......#....................#..................#.....................#................................#......
......#........#.........#.....................................#......................#...........#................#.......#......
.........#.......#...#..................##.#...#...........#.......#............#...#....#...........#................#...........
..........................................#..........................#........#.....##............#.........................#.....
..........#...................##.......#.............#....##..............#.........#..............#........#..........#....#.....
.#....##........#.....................................#...#.............................#..............#.....#....................
....#..#................................................#.......#..........#.....#.....#..........................................
.............##...............#..........#...............................................#............#..............#..##....#...
......#...............#...............#.........................................................................#.#..............#
......#........................................................................................#.......#.....#................#...
.......#..#...#.............................#......#.....#........................................#.........................#..##.
.................................#..................#.......#......#...................#...................#.......#............#.
......#....................#.........................#........................................................#...#.......#..#..#.
..##....#...............................#.................#....#........#......#..........#....#........#..................#......
..................#..........#................................#.........#...........#..#......#.....................#.............
##.................................................#..............#....#...............#..........................................
....#..............#.............#..........................................................................#................#....
..................................................#...#..#.......#................................................#...........#...
.....#.......#...............................................................#......#.....#......................................#
....#....................##....................#............#................................#..#....#....#..........#............
.#.........................#.....................#..........................##............................................#.......
.......................#.........................................#................................................................
...............#........................................................................#..........#.............................#
.#..............................#..#....................#........................#......#.#......#.......#....................#..#
..........................#........#.........#.........#...#..#................#........#.........................#...............
..#.................................#.........#......................#............................................................
...........................#..............................................#.................#...............#.....................
..........#..................#.............................................#.......................#..............................
....................#............##..........................#...............#..........#.......................................#.
....................#....#.....................................................................#.................................#
....#.......#...................#....#.#....#..................................#.#....#..#.............................#.....##...
.............#.#...................................................................#........#..............................#......
.......................#.....................#.......#.........................................#..........#.......................
...............#..............#..................................#.............#..................................................
......................................................................................#........#......................#.....#.....
.........#.....................................................#................#..........................#........###...........
...................#..##...............#......................#...................................................................
.............#........................................#..................................#.#.#....................................
......#............#.........................#..........#..#.................................................................##...
............#...............#..................................#...........#...............................#...........#..........
................#.......#.......................#................#.............#................................#.................
.........................#.#.........#..#.....#.....................................#................................#............
..#...............#..#..........................#.#.....#....................#....................................................
................................................................................#..........#..............#.......................
............#.#......#......#........#..................#...#.....................................................................
....#......................#........................#..................#......#.....#.............................................
.#..................#...........................#..........#..........................................#....#......................
...............#......................#.......#.....#...#.................#...............................................#.......
#...#.......#..............................................................................................#...........#......#...
........................#......................#..........#...............................##......#...........................#...
....................#....#..............#.....#.....#................#............................................................
................#........................................................................................#........................
...##................#.............#..............................................................................................
.#....#........................................................................................................................##.
..#...................................#......#.#......#...................#........#.............................#....##..........
.......................................................#..#....#..#.................................................#.............
............#..................#......#.....#...........................................................#..#........#............#
..#................................#....................................................................#.............#......#....
..#..........#........#..............................................................................................#............
.....#.....................#..............#.................................#................................#...#................
........................#........#..................................................................................#.............
...#...........#.............................................#....................#....#..........#...............................
...................#...........#...................................................................#...........#..................
...........#............................#......................................................................................##.
#..............#.#.......................................#.......................#.....#...........#.......#...........#.##.......
..............................................................##.........................................#.............#.........#
...........................#.#..................................................#..................#..#...........................
.....................................#...............#.....#...................................#..................................
..#...................#.#....#...#......................................................#.#..................................#....
..........................................#......................................................................#................
.............#...................................................................#...#....#........#...................#..........
.......................#.......................................##...........#......#......#..............................#........
.......#..............#..............#..............................................................#............................#
...........................................#...#.......#......#...............................#.........#.........................
.......#...................................................#........................^..#..........................................
.#..........................................................................................#.......#.#..#........................
............................##........................................................................#..#........................
..........#.#..#....#.##........................#.................................................................................
......#.#...................................#...#...................#.............#.....#.........................................
.................#........................................................................................#..#.#..................
#....................#.......................................#..#..#.....#..............#......................................#..
..............#.#............................................................#.....#......................................#......#
....................#.......#.............................#..............#.........................................##.............
.....#................................#.#...........#...........#.......................................#...#....#................
...............#...#......................#............................#..................................#.......................
....#........##...............................................................#......#...#.........#.....#..................#.....
...............................#......................................................................#...........................
............................#.............................#........##...............#................#....#...#..............#....
.......................#................#.......#........................................................................#........
..................................................................................#......................#.......#.#..............
........#.........................#........#.........................#.......................#......................#......#......
.........................#..#...........#..#.......................#.#.................#..........................................
......#.................#......................................................................#........................#.........
......................................##..........#...........................#........#...........#..............................
.......................................#...........................#.........#....#................#...............#..............
...........#...........#.........................#.......#.................#....#.#...........#..................#.##...........#.
...#..................#..#...............#..#...................................#.......#................#...................#....
...............#............#.......#.............#............#......###.............#.............................#..........#..
.......................................#...........................................#........#...........#.........................
................#...........#.....#..#...........##..................##..........#....#.....#...#........#........................
...............................##.............................................................#..........#...#....#...............
............#.................#...##.........#.......................................................................#............
...........#.#............................#...............##...........................................#............#....#........
...............................................#...........................................#..........#......................#....
......................................#........................................#.....................##...........#.....#..#.#....
..............................#.......#...#.#...............................................................................##....
................##......#..#.......#...........#............#.#......#..#................#..#.....#..................##...........
#.....#...........................#....................#.........................................................................#
............#.............#........#........#.#...............#...........................#.........#.............#......#........
#...#.........#.....#...................................................#.........................................................
.....................#...........#....................#.....#............##..........#....#..........#......#..........#..........
...................................#.........#.....#.................................#..........................................#.
......................................................#.........#.................................................................
...............#..............#........................................................................##.......#.............##..
...............#.......#.........#.........................#.......................##.............................................
..................#....................................#......................................................##...#....#.......#.
..#........#.....#...............#...............................#.........................#.#........#....#......................
..#.............#.......##...#......#....................................................................................#........
............................#.......#...........#.............#.............#..........................#..........................
.............#........................#........................................................#.........#........................
.#............#.....#...........................................#..#............................#..............#..........#.......
................#.....................................#..............................#............................................
..................#........................................#.#.................................................................#..
.#............#.......#..................................#....#.................................................#..............#.#
40 changes: 24 additions & 16 deletions 2024/day06.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def parse(lines):
for x, content in enumerate(row)
if content == "#"
]
visited = 0
visited = set()
return Grid(current, heading, obstacles, visited)


Expand All @@ -56,17 +56,33 @@ def visualize(obstacles, nxt, heading):
def walk(grid):
"""Walk one step and simulate the new state of the grid"""
current, heading, obstacles, visited = grid
visited.add(current)
# visualize(obstacles, current, heading)
if next_step(current, heading) in obstacles:
print("turning")
heading = turn_right(heading)
nxt = next_step(current, heading)
visited += 1
print(nxt, heading, visited)
visualize(obstacles, nxt, heading)
print()
# print(current, nxt, heading, visited)
# print()
return Grid(nxt, heading, obstacles, visited)


def visit(map):
lines = [list(line) for line in map.strip().splitlines()]
max_y = len(lines)
max_x = len(lines[0])
grid = parse(lines)
while (0 <= grid.current[0] <= max_x) and (0 <= grid.current[1] <= max_y):
grid = walk(grid)
return len(grid.visited) - 1


def main(part: int = 1) -> int:
with open("2024/data/day05.txt") as f:
map = f.read()
return visit(map)


if __name__ == "__main__":
map = """
....#.....
Expand All @@ -80,14 +96,6 @@ def walk(grid):
#.........
......#..."""

# repeat(50, walk, parse(map))
lines = [list(line) for line in map.strip().splitlines()]
max_y = len(lines)
max_x = len(lines[0])
grid = parse(lines)
walking = True
while walking:
grid = walk(grid)
if not (0 <= grid.current[0] <= max_x) or not (0 <= grid.current[1] <= max_y):
walking = False
print(grid.visited)
assert visit(map) == 41

print(main())

0 comments on commit 42305a3

Please sign in to comment.