|
| 1 | +from enum import Enum |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +GRID_HEIGHT = 130 |
| 5 | +GRID_WIDTH = 130 |
| 6 | + |
| 7 | +input_file = Path(__file__).parent / "day06_input.txt" |
| 8 | +input = input_file.read_text() |
| 9 | +input_grid: list[list[str]] = [list(line) for line in input.splitlines()] |
| 10 | + |
| 11 | +visited_cells = [[False] * GRID_WIDTH] * GRID_HEIGHT |
| 12 | + |
| 13 | +print("grid height", len(input_grid)) |
| 14 | +print("grid width", len(input_grid[0])) |
| 15 | + |
| 16 | + |
| 17 | +class Direction(Enum): |
| 18 | + NORTH = 0 |
| 19 | + EAST = 1 |
| 20 | + SOUTH = 2 |
| 21 | + WEST = 3 |
| 22 | + |
| 23 | + |
| 24 | +def find_guard_coordinates(grid: list[list[str]]) -> tuple[int, int]: |
| 25 | + for i, row in enumerate(grid): |
| 26 | + for j, cell in enumerate(row): |
| 27 | + if cell == "^": |
| 28 | + return (i, j) |
| 29 | + return (-1, -1) |
| 30 | + |
| 31 | + |
| 32 | +guard_direction = Direction.NORTH |
| 33 | +guard_coordinates = find_guard_coordinates(input_grid) |
| 34 | + |
| 35 | +# override position of initial guard location with "." to allow for re-visiting |
| 36 | +input_grid[guard_coordinates[0]][guard_coordinates[1]] = "." |
| 37 | + |
| 38 | + |
| 39 | +while True: |
| 40 | + x, y = guard_coordinates |
| 41 | + if x < 0 or x >= GRID_HEIGHT or y < 0 or y >= GRID_WIDTH: |
| 42 | + break |
| 43 | + visited_cells[x][y] = True |
| 44 | + cell_ahead = None |
| 45 | + match guard_direction: |
| 46 | + case Direction.NORTH: |
| 47 | + cell_ahead = input_grid[x - 1][y] |
| 48 | + # if cell_ahead == "#": |
| 49 | + # print("turning east") |
| 50 | + # guard_direction = Direction.EAST |
| 51 | + # else: |
| 52 | + # print("moving north to", x - 1, y) |
| 53 | + # guard_coordinates = (x - 1, y) |
| 54 | + case Direction.EAST: |
| 55 | + cell_ahead = input_grid[x][y + 1] |
| 56 | + # if cell_ahead == "#": |
| 57 | + # print("turning south") |
| 58 | + # guard_direction = Direction.SOUTH |
| 59 | + # else: |
| 60 | + # print("moving east to", x, y + 1) |
| 61 | + # guard_coordinates = (x, y + 1) |
| 62 | + case Direction.SOUTH: |
| 63 | + cell_ahead = input_grid[x + 1][y] |
| 64 | + # if cell_ahead == "#": |
| 65 | + # print("turning west") |
| 66 | + # guard_direction = Direction.WEST |
| 67 | + # else: |
| 68 | + # print("moving south to", x + 1, y) |
| 69 | + # guard_coordinates = (x + 1, y) |
| 70 | + case Direction.WEST: |
| 71 | + cell_ahead = input_grid[x][y - 1] |
| 72 | + # if cell_ahead == "#": |
| 73 | + # print("turning north") |
| 74 | + # guard_direction = Direction.NORTH |
| 75 | + # else: |
| 76 | + # print("moving west to", x, y - 1) |
| 77 | + # guard_coordinates = (x, y - 1) |
| 78 | + match cell_ahead: |
| 79 | + case "#": |
| 80 | + match guard_direction: |
| 81 | + case Direction.NORTH: |
| 82 | + print("turning east") |
| 83 | + guard_direction = Direction.EAST |
| 84 | + case Direction.EAST: |
| 85 | + print("turning south") |
| 86 | + guard_direction = Direction.SOUTH |
| 87 | + case Direction.SOUTH: |
| 88 | + print("turning west") |
| 89 | + guard_direction = Direction.WEST |
| 90 | + case Direction.WEST: |
| 91 | + print("turning north") |
| 92 | + guard_direction = Direction.NORTH |
| 93 | + case ".": |
| 94 | + match guard_direction: |
| 95 | + case Direction.NORTH: |
| 96 | + print("moving north to", x - 1, y) |
| 97 | + guard_coordinates = (x - 1, y) |
| 98 | + case Direction.EAST: |
| 99 | + print("moving east to", x, y + 1) |
| 100 | + guard_coordinates = (x, y + 1) |
| 101 | + case Direction.SOUTH: |
| 102 | + print("moving south to", x + 1, y) |
| 103 | + guard_coordinates = (x + 1, y) |
| 104 | + case Direction.WEST: |
| 105 | + print("moving west to", x, y - 1) |
| 106 | + guard_coordinates = (x, y - 1) |
| 107 | + |
| 108 | +print("guard coordinates", guard_coordinates) |
| 109 | + |
| 110 | +visited_count = 0 |
| 111 | +visited_grid = input_grid.copy() |
| 112 | + |
| 113 | +for i, visited_row in enumerate(visited_cells): |
| 114 | + for j, visited_cell in enumerate(visited_row): |
| 115 | + if visited_cell is True: |
| 116 | + visited_grid[i][j] = "X" |
| 117 | + visited_count += 1 |
| 118 | + |
| 119 | +print("visited count", visited_count) |
| 120 | + |
| 121 | +visited_file = Path(__file__).parent / "day06_visited.txt" |
| 122 | +visited_file.write_text("\n".join(["".join(row) for row in visited_grid])) |
0 commit comments