Solved Day 14

main
s00ner 2022-07-18 12:42:21 -04:00
parent ab083f75b8
commit f8d6c51748
2 changed files with 64 additions and 0 deletions

9
day14/input Normal file
View File

@ -0,0 +1,9 @@
Vixen can fly 19 km/s for 7 seconds, but then must rest for 124 seconds.
Rudolph can fly 3 km/s for 15 seconds, but then must rest for 28 seconds.
Donner can fly 19 km/s for 9 seconds, but then must rest for 164 seconds.
Blitzen can fly 19 km/s for 9 seconds, but then must rest for 158 seconds.
Comet can fly 13 km/s for 7 seconds, but then must rest for 82 seconds.
Cupid can fly 25 km/s for 6 seconds, but then must rest for 145 seconds.
Dasher can fly 14 km/s for 3 seconds, but then must rest for 38 seconds.
Dancer can fly 3 km/s for 16 seconds, but then must rest for 37 seconds.
Prancer can fly 25 km/s for 6 seconds, but then must rest for 143 seconds.

55
day14/solution.rb Normal file
View File

@ -0,0 +1,55 @@
#!/bin/env ruby
require 'pry'
input = File.readlines('./input')
reindeer = input.to_h do |line|
line = line.split
[line[0].to_sym,
{ speed: line[3].to_i, fly_time: line[6].to_i, rest_time: line[13].to_i, flying: line[6].to_i, distance: 0 }]
end
final_time = 2503
final_time.times do |_tick|
reindeer.each do |_deer, stats|
stats[:flying] = stats[:fly_time] if stats[:flying] == 0
if stats[:flying] > 0
stats[:distance] += stats[:speed]
stats[:flying] -= 1
stats[:flying] = -stats[:rest_time] if stats[:flying] == 0
else
stats[:flying] += 1
end
end
end
winner = reindeer.keys.reduce { |memo, deer| reindeer[deer][:distance] > reindeer[memo][:distance] ? deer : memo }
puts "Part 1: #{winner} wins, flying #{reindeer[winner][:distance]}km."
# Part 2
reindeer.each_key do |key|
reindeer[key][:distance] = 0
reindeer[key][:flying] = 0
reindeer[key][:points] = 0
end
final_time.times do |_tick|
reindeer.each do |_deer, stats|
stats[:flying] = stats[:fly_time] if stats[:flying] == 0
if stats[:flying] > 0
stats[:distance] += stats[:speed]
stats[:flying] -= 1
stats[:flying] = -stats[:rest_time] if stats[:flying] == 0
else
stats[:flying] += 1
end
end
win_dist = reindeer.map { |_, stats| stats[:distance] }.max
reindeer.each_key { |deer| reindeer[deer][:points] += 1 if reindeer[deer][:distance] == win_dist }
end
winner = reindeer.keys.reduce { |memo, deer| reindeer[deer][:points] > reindeer[memo][:points] ? deer : memo }
puts "Part 2: #{winner} wins, with #{reindeer[winner][:points]} points."
# binding.pry