-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday18p1.py
More file actions
executable file
·61 lines (45 loc) · 1.35 KB
/
Copy pathday18p1.py
File metadata and controls
executable file
·61 lines (45 loc) · 1.35 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
#!/usr/bin/env python
def neighbor_count(grid, light_pos, state):
row, col = light_pos
lights_on = 0
for x in [-1, 0, 1]:
for y in [-1, 0, 1]:
if x == 0 and y == 0:
continue # Don't count light itself
x2 = x + row
y2 = y + col
if (x2 < 0 or x2 > 99 or y2 < 0 or y2 > 99):
continue # Out of bounds
lights_on += grid[x2][y2]
if state:
if lights_on == 2 or lights_on == 3:
return state
else:
return state ^ 1
else:
if lights_on == 3:
return state ^ 1
else:
return state
def update_grid(grid):
new_grid = [[0 for y in xrange(100)] for x in xrange(100)]
for x in xrange(100):
for y in xrange(100):
new_grid[x][y] = neighbor_count(grid, (x, y), grid[x][y])
return new_grid
with open('input/day18.txt') as fh:
data = fh.read().rstrip('\n').split('\n')
light_grid = list()
for line in data:
light_line = list()
for char in line:
if char == '.':
light_line.append(0)
elif char == '#':
light_line.append(1)
else:
raise RuntimeError('Bad line!')
light_grid.append(light_line)
for x in xrange(100):
light_grid = update_grid(light_grid)
print sum(sum(light_grid, []))