generated from alvesvaren/AoC-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
11.py
92 lines (77 loc) · 2.1 KB
/
11.py
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
import aoc
from copy import copy
data = aoc.get_input(11).splitlines()
offsets = [
(1, 1),
(1, 0),
(1, -1),
(0, 1),
(0, -1),
(-1, 1),
(-1, 0),
(-1, -1)
]
seatmap = {}
maxx, maxy = 0, 0
for y, line in enumerate(data):
maxy = y + 1
for x, char in enumerate(line):
seatmap[x, y] = char
maxx = x + 1
prev_state = copy(seatmap)
def get_around(x: int, y: int) -> int:
adjacent_count = 0
for offset in offsets:
try:
if prev_state[x + offset[0], y + offset[1]] == "#":
adjacent_count += 1
except KeyError:
continue
return adjacent_count
def get_visible(x: int, y: int) -> int:
visible_count = 0
for offset in offsets:
for i in range(1, max(maxx, maxy)):
current_offset = offset[0] * i, offset[1] * i
try:
value = prev_state[
x + current_offset[0],
y + current_offset[1]
]
except KeyError:
break
if value == "#":
visible_count += 1
break
elif value == "L":
break
return visible_count
def step(visibility_method, maxcount) -> bool:
anything_changed = False
for y in range(maxy):
for x in range(maxx):
if prev_state[x, y] == "L" and visibility_method(x, y) == 0:
seatmap[x, y] = "#"
anything_changed = True
elif prev_state[x, y] == "#" and visibility_method(x, y) >= maxcount:
seatmap[x, y] = "L"
anything_changed = True
return anything_changed
seatmap_copy = copy(seatmap)
while step(get_around, 4):
prev_state = copy(seatmap)
count1 = 0
for y in range(maxy):
for x in range(maxx):
if seatmap[x, y] == "#":
count1 += 1
seatmap = seatmap_copy
while step(get_visible, 5):
prev_state = copy(seatmap)
count2 = 0
for y in range(maxy):
for x in range(maxx):
if seatmap[x, y] == "#":
count2 += 1
print("Part 1:", count1)
print("Part 2:", count2)