-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_11.py
81 lines (61 loc) · 1.88 KB
/
day_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
from copy import deepcopy
import aoc_helper
raw = aoc_helper.fetch(11, year=2020)
# print(raw)
# raw = """L.LL.LL.LL
# LLLLLLL.LL
# L.L.L..L..
# LLLL.LL.LL
# L.LL.LL.LL
# L.LLLLL.LL
# ..L.L.....
# LLLLLLLLLL
# L.LLLLLL.L
# L.LLLLL.LL"""
def parse_raw():
return [
["L", *["L" for _ in raw.splitlines()[0]], "L"],
*[["L", *[i for i in row], "L"] for row in raw.splitlines()],
["L", *["L" for _ in raw.splitlines()[0]], "L"],
]
data = parse_raw()
def life(state, visibility_fn, neighbour_constant):
next_state = deepcopy(state)
for y, row in enumerate(state[1:-1], start=1):
for x, cell in enumerate(row[1:-1], start=1):
visible = [
visibility_fn(dy, dx, state, y, x)
for dy in range(-1, 2)
for dx in range(-1, 2)
if not (dy == dx == 0)
]
if cell == "L" and sum(cell == "#" for cell in visible) == 0:
next_state[y][x] = "#"
elif (
cell == "#"
and sum(cell == "#" for cell in visible) >= neighbour_constant
):
next_state[y][x] = "L"
return next_state
def get_visible(dy, dx, state, y, x):
x += dx
y += dy
return state[y][x]
def get_visible_2(dy, dx, state, y, x):
x += dx
y += dy
while state[y][x] == ".":
x += dx
y += dy
return state[y][x]
def run_with(visibility_fn, neighbour_constant):
state = data
while (next_state := life(state, visibility_fn, neighbour_constant)) != state:
state = next_state
return "\n".join("".join(row[1:-1]) for row in state[1:-1]).count("#")
def part_one():
return run_with(get_visible, 4)
def part_two():
return run_with(get_visible_2, 5)
aoc_helper.lazy_submit(day=11, year=2020, solution=part_one)
aoc_helper.lazy_submit(day=11, year=2020, solution=part_two)