AoC2015/day03/solution.rb

53 lines
1.7 KiB
Ruby

#!/bin/env ruby
directions = File.read('./input')
# Part 1, How many houses receive at least one present?
locations = []
locations[0] = [0, 0]
directions.each_char do |direction|
locations.push(locations[-1].dup)
case direction
when '<'
locations[-1][0] -= 1
when '>'
locations[-1][0] += 1
when '^'
locations[-1][1] += 1
when 'v'
locations[-1][1] -= 1
end
end
# I initially misread the prompt and made it too complicated
# location_count = Hash.new(0)
# locations.each { |location| location_count.store(location.to_s.to_sym, location_count[location.to_s.to_sym] + 1)}
# puts "Part 1 Solution: " + location_count.count.to_s
# puts "Part 1 Solution: " + location_count.select { |location, visits| visits > 1 }.count.to_s
puts 'Part 1 Solution: ' + locations.uniq.count.to_s
# Part 2, The next year, to speed up the process, Santa creates a robot version of himself, Robo-Santa, to deliver presents with him.
# Santa and Robo-Santa start at the same location (delivering two presents to the same starting house), then take turns moving based on instructions from the elf, who is eggnoggedly reading from the same script as the previous year.
# This year, how many houses receive at least one present?
santa_locations = []
robo_locations = []
santa_locations[0] = [0, 0]
robo_locations[0] = [0, 0]
locations = [santa_locations, robo_locations].cycle
directions.each_char do |direction|
locations.peek.push(locations.peek[-1].dup)
case direction
when '<'
locations.next[-1][0] -= 1
when '>'
locations.next[-1][0] += 1
when '^'
locations.next[-1][1] += 1
when 'v'
locations.next[-1][1] -= 1
end
end
puts 'Part 2 Solution: ' + (robo_locations + santa_locations).uniq.count.to_s