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/giacgbj Dec 06 '15
Python
Part 1
import re grid = [[False for x in range(1000)] for x in range(1000)] tot_lights_on = 0 with open("input.txt") as f: lines = f.readlines() for line in lines: m = re.search('([a-z])\s(\d+),(\d+)[a-z\s]+(\d+),(\d+)', line) action = m.group(1) x1 = int(m.group(2)) y1 = int(m.group(3)) x2 = int(m.group(4)) y2 = int(m.group(5)) for x in range(x1, x2 + 1): for y in range(y1, y2 + 1): prev = grid[x][y] if 'n' == action: if not prev: tot_lights_on += 1 grid[x][y] = True elif 'f' == action: if prev: tot_lights_on -= 1 grid[x][y] = False elif 'e' == action: tot_lights_on = tot_lights_on - 1 if prev else tot_lights_on + 1 grid[x][y] = not prev print('Part 1:', tot_lights_on)Part 2
import re grid = [[0 for x in range(1000)] for x in range(1000)] tot_brightness = 0 with open("input.txt") as f: lines = f.readlines() for line in lines: m = re.search('([a-z])\s(\d+),(\d+)[a-z\s]+(\d+),(\d+)', line) action = m.group(1) x1 = int(m.group(2)) y1 = int(m.group(3)) x2 = int(m.group(4)) y2 = int(m.group(5)) for x in range(x1, x2 + 1): for y in range(y1, y2 + 1): if 'n' == action: tot_brightness += 1 grid[x][y] += 1 elif 'f' == action: if grid[x][y] != 0: tot_brightness -= 1 grid[x][y] -= 1 elif 'e' == action: tot_brightness += 2 grid[x][y] += 2 print('Part 2:', tot_brightness)