diff options
author | Brian Chu <brianmchu42@gmail.com> | 2021-12-30 15:11:21 -0800 |
---|---|---|
committer | Brian Chu <brianmchu42@gmail.com> | 2021-12-30 15:11:21 -0800 |
commit | e7085453864431ace3ad8f3123b259ed0829ae74 (patch) | |
tree | 2ef1fbb0e9d02fc934b5e09d96dd187f3e371ea6 /day6.py | |
download | AdventOfCode2015-e7085453864431ace3ad8f3123b259ed0829ae74.tar.gz |
all solutions for 2015 main
Diffstat (limited to 'day6.py')
-rw-r--r-- | day6.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/day6.py b/day6.py new file mode 100644 index 0000000..86c4abc --- /dev/null +++ b/day6.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python + +import numpy as np +import re + +num_parse = re.compile(r'(toggle|off|on) (\d+,\d+) through (\d+,\d+)$') + +with open('day6.txt') as data: + instructions = [num_parse.search(line).groups((1, 2, 3)) for line in data] + +lights = np.zeros((1000, 1000), dtype=bool) +for inst in instructions: + x1, y1 = map(int, inst[1].split(',')) + x2, y2 = map(int, inst[2].split(',')) + match inst[0]: + case 'toggle': + lights[x1:x2+1, y1:y2+1] = np.logical_not(lights[x1:x2+1, y1:y2+1]) + case 'on': + lights[x1:x2+1, y1:y2+1] = True + case 'off': + lights[x1:x2+1, y1:y2+1] = False + +print(np.count_nonzero(lights)) + +lights = np.zeros((1000, 1000), dtype=int) +for inst in instructions: + x1, y1 = map(int, inst[1].split(',')) + x2, y2 = map(int, inst[2].split(',')) + match inst[0]: + case 'toggle': + lights[x1:x2+1, y1:y2+1] += 2 + case 'on': + lights[x1:x2+1, y1:y2+1] += 1 + case 'off': + region = lights[x1:x2+1, y1:y2+1] + region -= 1 + region[region < 0] = 0 + +print(np.sum(lights)) |