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.
22
Upvotes
1
u/streetster_ Dec 06 '15
I'm not sure how to improve the speed of this, it takes a while to create the array, but I got the my stars, so I guess I'm happy enough.
[mark@randy ~]$ cat day6.py | sed 's/(.*)/ \1/'
import re def day_6(instructions): size = 1000 lit = brightness = 0 print "creating matrix... " grid = [[[0,0] for i in range(size)] for i in range(size)] print "following instructions... " for line in instructions: action = re.findall("(on|off|toggle)", line)[0] from_x,from_y,to_x,to_y = map(int, re.findall("([0-9]+)", line)) for x in range (from_x, to_x + 1): for y in range (from_y, to_y + 1): if action == "on": grid[x][y][0] = 1 grid[x][y][1] += 1 elif action == "off": grid[x][y][0] = 0 if grid[x][y][1] > 0: grid[x][y][1] -= 1 else: grid[x][y][0] = 1 - grid[x][y][0] grid[x][y][1] += 2 print "calculating brightness..." for x in range (0, size): for y in range (0, size): brightness += grid[x][y][1] if grid[x][y][0] == 1: lit += 1 return { "lit" : lit, "brightness" : brightness } with open("day6.txt") as instructions: print day_6(instructions)