-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday06p2.py
More file actions
executable file
·36 lines (25 loc) · 957 Bytes
/
day06p2.py
File metadata and controls
executable file
·36 lines (25 loc) · 957 Bytes
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
#!/usr/bin/env python
import re
def lights_set(task, start, end):
for row in xrange(start[0], end[0]+1):
for col in xrange(start[1], end[1]+1):
if task == 'turn on':
light_grid[row][col] += 1
elif task == 'turn off':
if light_grid[row][col] > 0:
light_grid[row][col] -= 1
else: # toggle
light_grid[row][col] += 2
with open('input/day06.txt') as fh:
data = fh.read().rstrip('\n').split('\n')
light_grid = [[0 for x in xrange(1000)] for x in xrange(1000)]
pat = re.compile(r'(turn on|turn off|toggle) (\d+),(\d+) through (\d+),(\d+)')
for line in data:
m = re.match(pat, line)
if m is None:
raise RuntimeError('Bad line!')
task = m.group(1)
start = (int(m.group(2)), int(m.group(3)))
end = (int(m.group(4)), int(m.group(5)))
lights_set(task, start, end)
print sum([sum(x) for x in light_grid])