diff options
author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-11-30 22:46:45 -0700 |
---|---|---|
committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-11-30 22:46:45 -0700 |
commit | 3d57434c04a669610d5f15bd2a7713e6928cdef7 (patch) | |
tree | a0f1f04a335bbc808369d6492f4fee2ff06a0bdb /aoc_2022/day-11 | |
parent | 59966ade163a39fc03f07a9d905e0bd87a98d60c (diff) | |
download | aoc-3d57434c04a669610d5f15bd2a7713e6928cdef7.tar.gz aoc-3d57434c04a669610d5f15bd2a7713e6928cdef7.zip |
add aoc2023
Diffstat (limited to 'aoc_2022/day-11')
-rw-r--r-- | aoc_2022/day-11/.gitkeep | 0 | ||||
-rw-r--r-- | aoc_2022/day-11/sol.py | 67 |
2 files changed, 67 insertions, 0 deletions
diff --git a/aoc_2022/day-11/.gitkeep b/aoc_2022/day-11/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/aoc_2022/day-11/.gitkeep diff --git a/aoc_2022/day-11/sol.py b/aoc_2022/day-11/sol.py new file mode 100644 index 0000000..9da8cec --- /dev/null +++ b/aoc_2022/day-11/sol.py @@ -0,0 +1,67 @@ + +def get_neighbors(grid, current): + neighbors = [] + y, x = current + cur_height = ord(grid[y][x]) + if (grid[y][x] == "S"): + cur_height = ord("a") + for i in range(-1, 2): + for j in range(-1, 2): + if i == 0 and j == 0 or (i != 0 and j != 0): + continue + if (y + i) < 0 or (y + i) >= len(grid): + continue + if (x + j) < 0 or (x + j) >= len(grid[y + i]): + continue + new_height = ord(grid[y + i][x + j]) + if (grid[y + i][x + j] == "E"): + new_height = ord("z") + if abs(new_height - cur_height) <= 1 or new_height <= cur_height: + neighbors.append((y + i, x + j)) + return neighbors + +def bfs(grid, start, end): + queue = [] + queue.append(start) + visited = {} + visited[start] = 0 + while queue: + current = queue.pop(0) + if current == end: + return visited[current] + for neighbor in get_neighbors(grid, current): + if neighbor not in visited: + queue.append(neighbor) + visited[neighbor] = visited[current] + 1 + return False + +def main(): + file = open("input", "r") + grid = file.readlines() + file.close() + + start = (0, 0) + end = (0, 0) + + for i in range(len(grid)): + for j in range(len(grid[i])): + if grid[i][j] == "S": + start = (i, j) + elif grid[i][j] == "E": + end = (i, j) + + best = bfs(grid, start, end) + print(best) + + for i in range(len(grid)): + for j in range(len(grid[i])): + if grid[i][j] == "a": + this_as_start = bfs(grid, (i, j), end) + if this_as_start and this_as_start < best: + best = this_as_start + + print(best) + + +if __name__ == "__main__": + main() |