-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday20_task2.py
More file actions
109 lines (79 loc) · 3.13 KB
/
Copy pathday20_task2.py
File metadata and controls
109 lines (79 loc) · 3.13 KB
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
###############################################################################
# Day x, Task y #
###############################################################################
import aoc_util
day = 20
data_str = """..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..###..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#..#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#.....####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.......##..####..#...#.#.#...##..#.#..###..#####........#..####......#..#
#..#.
#....
##..#
..#..
..###"""
def set_lit(mapping, old, new, row, col, step):
count = 0
is_outside = step % 2 == 1 and mapping[0] == "#"
if row > 0:
if col > 0 and old[row - 1][col - 1] == "#":
count |= 0b100000000
if old[row - 1][col] == "#":
count |= 0b010000000
if col < len(old[0]) - 1 and old[row - 1][col + 1] == "#":
count |= 0b001000000
elif is_outside:
count |= 0b111000000
if col > 0:
if old[row][col - 1] == "#":
count |= 0b000100000
elif is_outside:
count |= 0b100100100
if old[row][col] == "#":
count |= 0b000010000
if col < len(old[0]) - 1:
if old[row][col + 1] == "#":
count |= 0b000001000
elif is_outside:
count |= 0b001001001
if row < len(old) - 1:
if col > 0 and old[row + 1][col - 1] == "#":
count |= 0b000000100
if old[row + 1][col] == "#":
count |= 0b000000010
if col < len(old[0]) - 1 and old[row + 1][col + 1] == "#":
count |= 0b000000001
elif is_outside:
count |= 0b000000111
new[row][col] = mapping[count]
def get_num_lit_pixels(grid):
num_lit = 0
for row in grid:
for v in row:
if v == "#":
num_lit += 1
return num_lit
def task(data_set: list[str]) -> int:
replacement = data_set[0]
num_steps = 50
old_grid = []
new_grid = []
for _ in range(num_steps):
old_grid.append(["."] * (len(data_set[2]) + 2 * num_steps))
new_grid.append(["."] * (len(data_set[2]) + 2 * num_steps))
for i in range(2, len(data_set)):
row = ["."] * num_steps
row += data_set[i]
row += ["."] * num_steps
old_grid.append(row)
new_grid.append(row.copy())
for _ in range(num_steps):
old_grid.append(["."] * (len(data_set[2]) + 2 * num_steps))
new_grid.append(["."] * (len(data_set[2]) + 2 * num_steps))
for i in range(num_steps):
for row in range(0, len(old_grid)):
for col in range(0, len(old_grid[0])):
set_lit(replacement, old_grid, new_grid, row, col, i)
tmp = old_grid
old_grid = new_grid
new_grid = tmp
return get_num_lit_pixels(old_grid)
aoc_util.run_with_data_str(task, data_str)
aoc_util.run_with_data_set(task, day)