AoC2015/day14/solution.rb

56 lines
1.6 KiB
Ruby

#!/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