-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11.py
162 lines (133 loc) · 3.69 KB
/
day11.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# vi: set shiftwidth=4 tabstop=4 expandtab:
import datetime
import os
import itertools
import collections
top_dir = os.path.dirname(os.path.abspath(__file__)) + "/../../"
RUN_LONG_TESTS = False
def string_to_seat_layout(string):
return {
(i, j): s
for i, line in enumerate(string.splitlines())
for j, s in enumerate(line)
}
def seat_layout_to_string(seats):
m = max(i for i, j in seats.keys())
n = max(j for i, j in seats.keys())
return "\n".join("".join(seats[(i, j)] for j in range(n + 1)) for i in range(m + 1))
def get_seat_layout_from_file(file_path=top_dir + "resources/year2020_day11_input.txt"):
with open(file_path) as f:
return string_to_seat_layout(f.read())
directions = [pos for pos in itertools.product((-1, 0, +1), repeat=2) if pos != (0, 0)]
def get_new_seat_value_1(seat, nb_neigh):
if seat == "L":
return "#" if nb_neigh == 0 else seat
if seat == "#":
return "L" if nb_neigh >= 4 else seat
return seat
def get_new_seats1(seats):
neighbours_count = collections.Counter(
(x + dx, y + dy)
for (x, y), seat in seats.items()
if seat == "#"
for dx, dy in directions
)
return {
pos: get_new_seat_value_1(seat, neighbours_count[pos])
for pos, seat in seats.items()
}
def get_new_seat_value_rule2(seat, nb_visible):
if seat == "L":
return "#" if nb_visible == 0 else seat
if seat == "#":
return "L" if nb_visible >= 5 else seat
return seat
def get_new_seats2(seats):
visible_count = collections.Counter()
for (x, y), seat in seats.items():
if seat == "#":
for dx, dy in directions:
for d in itertools.count(start=1):
pos = x + (d * dx), y + (d * dy)
seat = seats.get(pos, None)
if seat != ".":
visible_count[pos] += 1
break
return {
pos: get_new_seat_value_rule2(seat, visible_count[pos])
for pos, seat in seats.items()
}
def get_nb_seat_on_fixedpoint(seats, func):
while True:
new_seats = func(seats)
if new_seats == seats:
break
seats = new_seats
return sum(seat == "#" for seat in seats.values())
def run_tests():
example1 = string_to_seat_layout(
"""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"""
)
example2 = string_to_seat_layout(
"""#.##.##.##
#######.##
#.#.#..#..
####.##.##
#.##.##.##
#.#####.##
..#.#.....
##########
#.######.#
#.#####.##"""
)
example3a = string_to_seat_layout(
"""#.LL.L#.##
#LLLLLL.L#
L.L.L..L..
#LLL.LL.L#
#.LL.LL.LL
#.LLLL#.##
..L.L.....
#LLLLLLLL#
#.LLLLLL.L
#.#LLLL.##"""
)
example3b = string_to_seat_layout(
"""#.LL.LL.L#
#LLLLLL.LL
L.L.L..L..
LLLL.LL.LL
L.LL.LL.LL
L.LLLLL.LL
..L.L.....
LLLLLLLLL#
#.LLLLLL.L
#.LLLLL.L#"""
)
assert example2 == get_new_seats1(example1)
assert example3a == get_new_seats1(example2)
assert get_nb_seat_on_fixedpoint(example1, get_new_seats1) == 37
assert example2 == get_new_seats2(example1)
assert example3b == get_new_seats2(example2)
assert get_nb_seat_on_fixedpoint(example1, get_new_seats2) == 26
def get_solutions():
seat_layout = get_seat_layout_from_file()
print(get_nb_seat_on_fixedpoint(seat_layout, get_new_seats1) == 2204)
if RUN_LONG_TESTS:
print(get_nb_seat_on_fixedpoint(seat_layout, get_new_seats2) == 1986)
if __name__ == "__main__":
RUN_LONG_TESTS = True
begin = datetime.datetime.now()
run_tests()
get_solutions()
end = datetime.datetime.now()
print(end - begin)