-
Notifications
You must be signed in to change notification settings - Fork 0
/
day24.py
111 lines (77 loc) · 3.23 KB
/
day24.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from util import get_input, print_answer, get_new_location
def adjacent(position, bugs):
return sum([1 for direction in range(4) if get_new_location(position, direction) in bugs])
def get_neighbours(position):
x, y, z = position
neighbours = [get_new_location((x, y), direction) for direction in range(4)]
neighbours = [(x_new, y_new, z) for (x_new, y_new) in neighbours if (x, y) != (2, 2)]
if (x, y) == (2, 1):
return neighbours + [(x_new, 0, z + 1) for x_new in range(5)]
if (x, y) == (3, 2):
return neighbours + [(4, y_new, z + 1) for y_new in range(5)]
if (x, y) == (2, 3):
return neighbours + [(x_new, 4, z + 1) for x_new in range(5)]
if (x, y) == (1, 2):
return neighbours + [(0, y_new, z + 1) for y_new in range(5)]
# Top edge
if y == 0:
neighbours.append((2, 1, z - 1))
# Right edge
if x == 4:
neighbours.append((3, 2, z - 1))
# Bottom edge
if y == 4:
neighbours.append((2, 3, z - 1))
# Left edge
if x == 0:
neighbours.append((1, 2, z - 1))
return neighbours
def adjacent3(position, bugs):
return sum([1 for neighbour in get_neighbours(position) if neighbour in bugs])
def print_bugs(bugs):
for z in range(-5, 6):
print("level", z)
for y in range(5):
for x in range(5):
if (x, y, z) in bugs:
print("#", end='')
else:
print(".", end='')
print()
def part1():
bugs = set((x, y) for x in range(5) for y in range(5) if input[y][x] == "#")
biodiversities = []
while True:
adjacent_1 = set((x, y) for x in range(5) for y in range(5) if adjacent((x, y), bugs) == 1)
adjacent_2 = set((x, y) for x in range(5) for y in range(5) if adjacent((x, y), bugs) in [1, 2])
bugs = set((x, y) for x in range(5) for y in range(5) if
(x, y) in bugs and (x, y) in adjacent_1 or
(x, y) not in bugs and (x, y) in adjacent_2)
biodiversity = sum([2**(5*y+x) for (x, y) in bugs])
if biodiversity in biodiversities:
return biodiversity
biodiversities.append(biodiversity)
def part2():
bugs = set((x, y, 0) for x in range(5) for y in range(5) if input[y][x] == "#")
for minute in range(200):
levels = range(-minute-1, minute+2)
adjacent_1 = set((x, y, z)
for x in range(5)
for y in range(5)
for z in levels
if adjacent3((x, y, z), bugs) == 1)
adjacent_2 = set((x, y, z)
for x in range(5)
for y in range(5)
for z in levels
if adjacent3((x, y, z), bugs) in [1, 2])
bugs = set((x, y, z) for x in range(5) for y in range(5) for z in levels if
(x, y, z) in bugs and (x, y, z) in adjacent_1 or
(x, y, z) not in bugs and (x, y, z) in adjacent_2)
return len(bugs)
if __name__ == "__main__":
part1_correct = 2130474
part2_correct = 1923
input = get_input("24")
print_answer(1, part1(), part1_correct)
print_answer(2, part2(), part2_correct)