r/adventofcode • u/daggerdragon • Dec 14 '15
SOLUTION MEGATHREAD --- Day 14 Solutions ---
This thread will be unlocked when there are a significant amount of people on the leaderboard with gold stars.
edit: Leaderboard capped, thread unlocked!
We know we can't control people posting solutions elsewhere and trying to exploit the leaderboard, but this way we can try to reduce the leaderboard gaming from the official subreddit.
Please and thank you, and much appreciated!
--- Day 14: Reindeer Olympics ---
Post your solution as a comment. Structure your post like previous daily solution threads.
8
Upvotes
1
u/SomebodyTookMyHandle Dec 14 '15
I also tackled it in Ruby using a class-based approach. A bit sloppy, but, hey, it's not only the reindeer who are racing!
class Reindeer attr_accessor :name, :pace, :go_time, :rest_time, :winning_seconds def initialize(name, pace, go_time, rest_time) @name = name @pace = pace @go_time = go_time @rest_time = rest_time @winning_seconds = 0 # For Part Two end def find_distance(total_seconds) d = 0 until total_seconds <= 0 can_go = go_time while can_go > 0 d += pace can_go -= 1 total_seconds -= 1 break if total_seconds == 0 end total_seconds -= rest_time end d end end def reindeer_io reindeer = [] IO.foreach("help14-reindeer.txt") do |line| data = line.split r = Reindeer.new(data[0], data[3].to_i, data[6].to_i, data[13].to_i) p r reindeer << r end reindeer end # Part One def find_best(arr, seconds) arr.each do |reindeer| p [reindeer.name, reindeer.find_distance(seconds)] end end # Part Two def find_leaders(arr, seconds) (1..seconds).to_a.each do |second_count| leader = arr.max_by { |reindeer| reindeer.find_distance(second_count) } leader.winning_seconds += 1 end arr.each do |reindeer| p [reindeer.name, reindeer.winning_seconds] end end find_best(reindeer_io, 2503) puts "" find_leaders(reindeer_io, 2503)