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

171 comments sorted by

View all comments

3

u/jlmcmchl Dec 06 '15

Python: Got #42 on the leaderboards for trying at midnight for the first time, I'll take it. Takes a second or two to run, probably for all the string stuff.

lights = [[0]*1000 for i in range(1000)]
with open('input.txt') as f:
	s = f.read()

def prepare(l):
	if 'on' in l:
		f = lambda x: 1    #1+x
		l = l.replace('turn on ', '')
	elif 'off' in l:
		f = lambda x: 0    #max(x-1, 0)
		l = l.replace('turn off ', '')
	elif 'toggle' in l:
		f = lambda x: x+1%2    #x + 2
		l = l.replace('toggle ', '')
	return l, f

def dorange(lights, start, end, fn):
	for i in range(start[0], end[0] + 1):
		for j in range(start[1], end[1] + 1):
			lights[i][j] = fn(lights[i][j])

def day6(lights, instr):
	for l in instr:
		f = None
		l, f = prepare(l)
		l = l.split(' through ')
		start = [i for i in map(int, l[0].split(','))]
		end = [i for i in map(int, l[1].split(','))]
		dorange(lights, start, end, f)

print(sum(map(sum, l)))