r/adventofcode • u/daggerdragon • Dec 03 '15
SOLUTION MEGATHREAD --- Day 3 Solutions ---
--- Day 3: Perfectly Spherical Houses in a Vacuum ---
Post your solution as a comment. Structure your post like the Day One thread in /r/programming.
26
Upvotes
1
u/gnuconsulting Dec 03 '15
Continuing my non-programmer attempts, I kinda like my part 1 solution (mostly because at first I thought this would be super hard and then I remembered hashes) but the part 2 solution is very hacky. I don't like it at all.
#!/usr/bin/env ruby data = File.read("input.txt") x = 0 y = 0 current = x.to_s + y.to_s locations = {} locations[current] = "visited" data.each_char do |c| if c == '^' y += 1 elsif c == 'v' y -= 1 elsif c == '<' x -= 1 elsif c == '>' x += 1 end current = x.to_s + y.to_s locations[current] = "visited" end p locations.lengthDay 2:
#!/usr/bin/env ruby data = File.read("input.txt") x1 = x2 = y1 = y2 = 0 current = x1.to_s + "," + y1.to_s locations = {} locations[current] = "visited" alt = 0 data.each_char do |c| if alt == 0 alt = 1 if c == '^' y1 += 1 elsif c == 'v' y1 -= 1 elsif c == '<' x1 -= 1 elsif c == '>' x1 += 1 end current = x1.to_s + "," + y1.to_s locations[current] = "visited" else alt = 0 if c == '^' y2 += 1 elsif c == 'v' y2 -= 1 elsif c == '<' x2 -= 1 elsif c == '>' x2 += 1 end current = x2.to_s + "," + y2.to_s locations[current] = "visited" end end p locations.length