Solved Day 3

main
s00ner 2022-07-12 14:52:10 -04:00
parent 77a6417a3c
commit 5970628517
3 changed files with 54 additions and 0 deletions

1
day03/input Normal file

File diff suppressed because one or more lines are too long

1
day03/input.testing Normal file
View File

@ -0,0 +1 @@
^v^v^v^v^v

52
day03/solution.rb Normal file
View File

@ -0,0 +1,52 @@
#!/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