-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday25.py
More file actions
88 lines (73 loc) · 2.59 KB
/
Copy pathday25.py
File metadata and controls
88 lines (73 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from __future__ import annotations
from rich.console import Console
from typing import List, Tuple, Optional, Dict
from dataclasses import dataclass
@dataclass(order=True, frozen=True)
class Coords:
x: int
y: int
def get_neighbours(self) -> List[Coords]:
return [
Coords(self.x - 1, self.y),
Coords(self.x, self.y - 1),
Coords(self.x + 1, self.y),
Coords(self.x, self.y + 1),
]
def read_map(lines: List[str]) -> Tuple[Dict[Coords, str], Dict[Coords, str], int, int]:
east_map = {}
south_map = {}
maxY = len(lines)
for y, line in enumerate(lines):
maxX = len(line.strip())
for x, char in enumerate(line.strip()):
if char == ">":
east_map[Coords(x, y)] = char
elif char == "v":
south_map[Coords(x, y)] = char
return east_map, south_map, maxX, maxY
def print_map(map: Tuple[Dict[Coords, str], Dict[Coords, str], int, int]):
east_map, south_map, maxX, maxY = map
for y in range(0, maxY):
for x in range(0, maxX):
coords = Coords(x, y)
if coords in east_map:
print(">", end="")
elif coords in south_map:
print("v", end="")
else:
print(".", end="")
print("")
print("")
def simulate_steps(map: Tuple[Dict[Coords, str], Dict[Coords, str], int, int]) -> int:
num_moved = 1
east_map, south_map, maxX, maxY = map
num_steps = 0
while num_moved > 0:
# print(num_steps)
# print_map((east_map, south_map, maxX, maxY))
num_moved = 0
new_east_map, new_south_map = {}, {}
for coord in east_map:
check_coord = Coords((coord.x + 1) % maxX, coord.y)
if check_coord not in east_map and check_coord not in south_map:
new_east_map[check_coord] = ">"
num_moved += 1
else:
new_east_map[coord] = ">"
for coord in south_map:
check_coord = Coords(coord.x, (coord.y + 1) % maxY)
if check_coord not in south_map and check_coord not in new_east_map:
new_south_map[check_coord] = "v"
num_moved += 1
else:
new_south_map[coord] = "v"
east_map = new_east_map
south_map = new_south_map
num_steps += 1
return num_steps
console = Console()
with open("day25.txt", "r") as file:
map = read_map(file.readlines())
console.print("[b yellow]Day 24[/b yellow]")
num_steps = simulate_steps(map)
console.print(num_steps)