From f8d6c517485e4cb02cf651fc54ce3eedb406211d Mon Sep 17 00:00:00 2001 From: s00ner Date: Mon, 18 Jul 2022 12:42:21 -0400 Subject: [PATCH] Solved Day 14 --- day14/input | 9 ++++++++ day14/solution.rb | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 day14/input create mode 100644 day14/solution.rb diff --git a/day14/input b/day14/input new file mode 100644 index 0000000..2af1170 --- /dev/null +++ b/day14/input @@ -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. diff --git a/day14/solution.rb b/day14/solution.rb new file mode 100644 index 0000000..fee82f4 --- /dev/null +++ b/day14/solution.rb @@ -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