-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday11.py
149 lines (122 loc) · 4.94 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
import pytest
import numpy as np
from copy import copy
import progressbar
from typing import List
@pytest.mark.parametrize('layout, row, col, expected',
[
(['.......#.',
'...#.....',
'.#.......',
'.........',
'..#L....#',
'....#....',
'.........',
'#........',
'...#.....'],
4, 3, 8),
(['.............',
'.L.L.#.#.#.#.',
'.............'],
1, 1, 0),
(['.##.##.',
'#.#.#.#',
'##...##',
'...L...',
'##...##',
'#.#.#.#',
'.##.##.'],
3, 3, 0),
])
def test_get_visible_occupied(layout: List[str], row: int, col: int, expected: int):
# arrange
grid, height, width = grid_from_layout(layout)
# act and assert
assert get_visible_occupied(grid, row, col, height, width) == expected
def grid_from_layout(layout: List[str]) -> (np.array, int, int):
height = len(layout)
width = len(layout[0])
grid = np.zeros((height, width))
for row in range(height):
for col in range(width):
if layout[row][col] == 'L':
grid[row][col] = 1
if layout[row][col] == '#':
grid[row][col] = 2
return grid, height, width
def parse_input(filename: str) -> (np.array, int, int):
lines = [line.strip() for line in open(filename).readlines()]
return grid_from_layout(lines)
def get_adjacent_occupied(grid: np.array, row: int, col: int, height: int, width: int) -> int:
min_row, min_col = max(0, row - 1), max(0, col - 1)
max_row, max_col = min(row + 1, height - 1), min(col + 1, width - 1)
sum_occupied = 0
for r in range(min_row, max_row + 1):
for c in range(min_col, max_col + 1):
if r == row and c == col:
continue
if grid[r][c] == 2:
sum_occupied += 1
return sum_occupied
def get_visible_occupied(grid: np.array, row: int, col: int, height: int, width: int) -> int:
sum_occupied = 0
for dx in range(-1, 2):
for dy in range(-1, 2):
if dx == 0 and dy == 0:
continue
x, y = col + dx, row + dy
while 0 <= x < width and 0 <= y < height:
if grid[y][x] == 1:
break
if grid[y][x] == 2:
sum_occupied += 1
break
x, y = x + dx, y + dy
return sum_occupied
def iterate_adjacent(grid: np.array, height: int, width: int) -> bool:
old_grid = copy(grid)
changed = False
for row in range(height):
for col in range(width):
if old_grid[row][col] == 1 and get_adjacent_occupied(old_grid, row, col, height, width) == 0:
grid[row][col] = 2
changed = True
if old_grid[row][col] == 2 and get_adjacent_occupied(old_grid, row, col, height, width) >= 4:
grid[row][col] = 1
changed = True
return changed
def iterate_visible(grid: np.array, height: int, width: int) -> bool:
old_grid = copy(grid)
changed = False
for row in range(height):
for col in range(width):
if old_grid[row][col] == 1 and get_visible_occupied(old_grid, row, col, height, width) == 0:
grid[row][col] = 2
changed = True
if old_grid[row][col] == 2 and get_visible_occupied(old_grid, row, col, height, width) >= 5:
grid[row][col] = 1
changed = True
return changed
def count_occupied(grid: np.array) -> int:
return len(grid[np.where(grid == 2)])
def part1(grid: np.array, height: int, width: int) -> int:
iteration = 0
with progressbar.ProgressBar(max_value=117, redirect_stdout=True) as p:
while iterate_adjacent(grid, height, width):
iteration += 1
p.update(iteration)
return count_occupied(grid)
def part2(grid: np.array, height: int, width: int) -> int:
iteration = 0
with progressbar.ProgressBar(max_value=85, redirect_stdout=True) as p:
while iterate_visible(grid, height, width):
iteration += 1
p.update(iteration)
return count_occupied(grid)
def main():
grid, height, width = parse_input('input/day11.txt')
print(f'Part 1: {part1(grid, height, width)}')
grid, height, width = parse_input('input/day11.txt')
print(f'Part 2: {part2(grid, height, width)}')
if __name__ == "__main__":
main()