-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday11.py
More file actions
108 lines (88 loc) · 2.98 KB
/
Copy pathday11.py
File metadata and controls
108 lines (88 loc) · 2.98 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from __future__ import annotations
import time
from dataclasses import dataclass
from typing import Dict, List, Tuple
from rich.console import Console
from rich.live import Live
from rich.table import Table
console = Console()
@dataclass(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),
Coords(self.x - 1, self.y - 1),
Coords(self.x - 1, self.y + 1),
Coords(self.x + 1, self.y + 1),
Coords(self.x + 1, self.y - 1),
]
def render_flashed_map(
map: Dict[Coords, int], just_flashed: List[Coords], iteration: int
) -> Table:
table = Table(show_header=False)
table.add_column()
flashed_set = set(just_flashed)
x = 0
y = 0
done = False
currRow = ""
while not done:
if (val := map.get(Coords(x, y))) is not None:
if Coords(x, y) in flashed_set:
currRow += f"[yellow]{val}[/yellow]"
else:
currRow += f"[grey50]{val}[/grey50]"
x += 1
elif x != 0:
y += 1
x = 0
table.add_row(currRow)
currRow = ""
else:
done = True
table.add_row(f"Iter: {iteration}")
return table
def read_input(lines: List[str]) -> Dict[Coords, int]:
map = {}
for y, line in enumerate(lines):
for x, char in enumerate(line.strip()):
map[Coords(x, y)] = int(char)
return map
def run_flash_iteration(map: Dict[Coords, int]) -> Tuple[int, List[Coords]]:
flashed: List[Coords] = []
increase_energy: List[Coords] = map.keys()
while len(increase_energy) > 0:
next_increase_energy: List[Coords] = []
for coords in increase_energy:
map[coords] += 1
if map[coords] == 10:
flashed.append(coords)
for neighbour in coords.get_neighbours():
if neighbour in map:
next_increase_energy.append(neighbour)
increase_energy = next_increase_energy
for flashed_coords in flashed:
map[flashed_coords] = 0
return len(flashed), flashed
with open("day11.txt", "r") as file:
map = read_input(file.readlines())
console.print("[b yellow]Day 01[/b yellow]")
flashes = 0
latest_flashcount = 0
iteration = 0
with Live(render_flashed_map(map, [], iteration), refresh_per_second=10) as live:
while latest_flashcount < 100:
latest_flashcount, just_flashed = run_flash_iteration(map)
live.update(render_flashed_map(map, just_flashed, iteration))
if iteration < 100:
flashes += latest_flashcount
iteration += 1
time.sleep(0.1)
console.print(
f"{flashes} after 100 iterations. It took {iteration} iterations for all to flash"
)