-
Notifications
You must be signed in to change notification settings - Fork 0
/
day21.py
77 lines (61 loc) · 1.82 KB
/
day21.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
# vi: set shiftwidth=4 tabstop=4 expandtab:
import datetime
import os
top_dir = os.path.dirname(os.path.abspath(__file__)) + "/../../"
def get_garden_from_lines(string):
grid = set()
start = None
for i, row in enumerate(string.splitlines()):
for j, val in enumerate(row):
if val != "#":
pos = (i, j)
grid.add(pos)
if val == "S":
start = pos
return grid, start
def get_garden_from_file(file_path=top_dir + "resources/year2023_day21_input.txt"):
with open(file_path) as f:
return get_garden_from_lines(f.read())
neighbours = [(-1, 0), (+1, 0), (0, -1), (0, +1)]
def get_nb_plots(garden_plot, nb_steps):
grid, start = garden_plot
# TODO: Future optimisation, consider only
# non-visited plots and check parity of manhattan distance
positions = set([start])
for i in range(1, nb_steps + 1):
new_positions = set()
for i, j in positions:
for di, dj in neighbours:
pos2 = (i + di, j + dj)
if pos2 in grid:
new_positions.add(pos2)
positions = new_positions
# print(len(positions))
return len(positions)
def run_tests():
garden = get_garden_from_lines(
"""...........
.....###.#.
.###.##..#.
..#.#...#..
....#.#....
.##..S####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
..........."""
)
assert get_nb_plots(garden, 1) == 2
assert get_nb_plots(garden, 2) == 4
assert get_nb_plots(garden, 3) == 6
assert get_nb_plots(garden, 6) == 16
def get_solutions():
garden = get_garden_from_file()
print(get_nb_plots(garden, 64) == 3671)
if __name__ == "__main__":
begin = datetime.datetime.now()
run_tests()
get_solutions()
end = datetime.datetime.now()
print(end - begin)