Skip to content

Commit 7572b10

Browse files
committed
Add memoisation so tiles aren't checked repeatedly
1 parent 0c9de97 commit 7572b10

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

Diff for: 2020/day_24.py

+9
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,18 @@
3636

3737
for _ in range(100):
3838
new_tiles = {}
39+
checked = set()
3940
for coords in tiles:
4041
y, x, z = coords
4142
black_adjacent = 0
4243
for next_to in directions.values():
4344
next_to_coords = (next_to[0] + y, next_to[1] + x, next_to[2] + z)
4445
if next_to_coords in tiles:
4546
black_adjacent += 1
47+
48+
elif next_to_coords in checked:
49+
pass
50+
4651
else:
4752
double_black_adjacent = 0
4853
for double_next_to in directions.values():
@@ -58,9 +63,13 @@
5863
if double_black_adjacent == 2:
5964
new_tiles[next_to_coords] = 1
6065

66+
checked.add(next_to_coords)
67+
6168
if black_adjacent in (1, 2):
6269
new_tiles[coords] = 1
6370

71+
checked.add(coords)
72+
6473
tiles = new_tiles
6574

6675
# Answer Two

0 commit comments

Comments
 (0)