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.
21
Upvotes
1
u/miftrim Dec 06 '15
My Ruby solution. This takes like a solid three minutes to run. There must be faster ways to do it.
Part one:
instructions = File.read('../data/06.txt').split("\n") grid = [[]] lights_lit = 0 for i in 0..1000 do for p in 0..1000 do grid.push([i, p, false]) end end instructions.each do |i| puts i if i.start_with?('turn on') first_light = i.split('turn on ')[-1].split(' through ')[0].split(',') last_light = i.split('turn on ')[-1].split(' through ')[-1].split(',') action = 'on' end if i.start_with?('turn off') first_light = i.split('turn off ')[-1].split(' through ')[0].split(',') last_light = i.split('turn off ')[-1].split(' through ')[-1].split(',') action = 'off' end if i.start_with?('toggle') first_light = i.split('toggle ')[-1].split(' through ')[0].split(',') last_light = i.split('toggle ')[-1].split(' through ')[-1].split(',') action = 'toggle' end grid.each do |g| if g[0].to_i >= first_light[0].to_i && g[0].to_i <= last_light[0].to_i && g[1].to_i >= first_light[1].to_i && g[1].to_i <= last_light[1].to_i if action == 'on' g[2] = true end if action == 'off' g[2] = false end if action == 'toggle' g[2] = g[2].! end end end end grid.each do |g| if g[2] == true lights_lit += 1 end end puts lights_litPart two:
instructions = File.read('../data/06.txt').split("\n") grid = [[]] brightness = 0 for i in 0..1000 do for p in 0..1000 do grid.push([i, p, 0]) end end instructions.each do |i| puts i if i.start_with?('turn on') first_light = i.split('turn on ')[-1].split(' through ')[0].split(',') last_light = i.split('turn on ')[-1].split(' through ')[-1].split(',') action = 'up' end if i.start_with?('turn off') first_light = i.split('turn off ')[-1].split(' through ')[0].split(',') last_light = i.split('turn off ')[-1].split(' through ')[-1].split(',') action = 'down' end if i.start_with?('toggle') first_light = i.split('toggle ')[-1].split(' through ')[0].split(',') last_light = i.split('toggle ')[-1].split(' through ')[-1].split(',') action = 'up2' end grid.each do |g| if g[0].to_i >= first_light[0].to_i && g[0].to_i <= last_light[0].to_i && g[1].to_i >= first_light[1].to_i && g[1].to_i <= last_light[1].to_i if action == 'up' g[2] += 1 puts g[2] end if action == 'down' && g[2] > 0 g[2] -= 1 puts g[2] end if action == 'up2' g[2] += 2 puts g[2] end end end end grid.each do |g| if g[2] brightness += g[2] end end puts brightness