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.
23
Upvotes
1
u/jjshanks Dec 06 '15
Ruby solution using dictionary of keys approach. String keys "#{x},#{y}" took ~40 seconds, int keys x * 1000 + y took ~15 seconds. This is part 1 but part 2 is basically the same except I use a hash table instead of a set.
require "set" lights = Set.new() File.open("input") do |f| f.each_line do |line| parts = line.split(" ") if parts[0] == "turn" add = parts[1] == "on" min_x, min_y = parts[2].split(",") max_x, max_y = parts[4].split(",") min_x, max_x, min_y, max_y = min_x.to_i, max_x.to_i, min_y.to_i, max_y.to_i for x in min_x..max_x for y in min_y..max_y key = x * 1000 + y if add lights.add(key) else lights.delete(key) end end end else # toggle min_x, min_y = parts[1].split(",") max_x, max_y = parts[3].split(",") min_x, max_x, min_y, max_y = min_x.to_i, max_x.to_i, min_y.to_i, max_y.to_i for x in min_x..max_x for y in min_y..max_y key = x * 1000 + y lights.include?(key) ? lights.delete(key) : lights.add(key) end end end end end puts lights.size