-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday6.py
53 lines (48 loc) · 1.43 KB
/
day6.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
from helper import getInput
import re
def part1():
lights = set()
for l in parse():
mode = l[0]
for x in range(l[1][0], l[1][2] + 1):
for y in range(l[1][1], l[1][3] + 1):
p = (x,y)
if mode == 'on':
lights.add(p)
elif mode == 'off':
if p in lights:
lights.remove(p)
elif mode == 'toggle':
if p in lights:
lights.remove(p)
else:
lights.add(p)
return len(lights)
def part2():
lights = dict()
for l in parse():
mode = l[0]
for x in range(l[1][0], l[1][2] + 1):
for y in range(l[1][1], l[1][3] + 1):
p = (x, y)
cur = lights.get(p) if p in lights else 0
if mode == 'off':
lights.update({p : cur - 1 if not cur - 1 < 0 else 0})
else:
lights.update({p : cur + (1 if mode == 'on' else 2)})
return sum(lights.values())
def parse():
data = []
for i in input:
nums = list(map(int,re.findall('\d+', i)))
if ('turn on' in i):
mode = 'on'
elif ('turn off' in i):
mode = 'off'
else:
mode = 'toggle'
data.append((mode, nums))
return data
input = getInput(6)
print(part1())
print(part2())