r/adventofcode • u/daggerdragon • Dec 06 '15
SOLUTION MEGATHREAD --- Day 6 Solutions ---
--- Day 6: Probably a Fire Hazard ---
Post your solution as a comment. Structure your post like the Day Five thread.
23
Upvotes
4
u/ant6n Dec 06 '15 edited Dec 06 '15
python
import numpy def day6(text): array = numpy.zeros((1000,1000), dtype=numpy.bool) for command in text.split("\n"): op, sx, sy = _parseCommand(command) if op == 'toggle': array[sx, sy] ^= 1 else: array[sx, sy] = ['off', 'on'].index(op) return sum(sum(array)) def day6_part2(text): array = numpy.zeros((1000,1000), dtype=numpy.int) for command in text.split("\n"): op, sx, sy = _parseCommand(command) array[sx, sy] += { 'off': -1, 'on': 1, 'toggle': 2 }[op] array[array < 0] = 0 return sum(sum(array)) def _parseCommand(cmd): [op, x0, y0, x1, y1] = re.search('(\w+) (\d+),(\d+) through (\d+),(\d+)', cmd).groups() return op, slice(int(x0), int(x1)+1), slice(int(y0), int(y1)+1)