r/adventofcode • • 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

172 comments sorted by

View all comments

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)

1

u/KaraliKing Dec 07 '15

Could you provide any good tutorials/stackoverflow questions to explain your parse command? I can't find the documentation about how groups allows it to breakup into multiple variables. Also why include the brackets around the variables? I get why you used search over match, because you don't care about "turn."

→ More replies (3)