Skip to content

Instantly share code, notes, and snippets.

@wconrad
Created December 15, 2015 00:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wconrad/0917bbbe06f57aa17294 to your computer and use it in GitHub Desktop.
Save wconrad/0917bbbe06f57aa17294 to your computer and use it in GitHub Desktop.
Advent of Code, day 14
class Raindeer
attr_reader :name
def initialize(name:, speed:, sprint_time:, rest_time:)
@name = name
@speed = speed
@sprint_time = sprint_time
@rest_time = rest_time
end
def distance_after(time)
cycle_number, seconds_into_cycle = time.divmod(cycle_time)
distance_for_full_cycles = cycle_number * distance_per_full_cycle
flight_time_for_current_cycle = [seconds_into_cycle, @sprint_time].min
distance_for_current_cycle = flight_time_for_current_cycle * @speed
distance_for_full_cycles + distance_for_current_cycle
end
private
def cycle_time
@sprint_time + @rest_time
end
def distance_per_full_cycle
@sprint_time * @speed
end
end
def parse(file)
file.each_line.map do |line|
raise unless line =~ /^(\w+)\D+(\d+)\D+(\d+)\D+(\d+)/
Raindeer.new(
name: $1,
speed: Integer($2),
sprint_time: Integer($3),
rest_time: Integer($4),
)
end
end
def best_distance_after(time)
@raindeers.map do |raindeer|
raindeer.distance_after(time)
end.max
end
RACE_TIME = 2503
@raindeers = File.open("input", "r") { |file| parse(file) }
# part 1
puts best_distance_after(RACE_TIME)
# part 2
scores = {}
(1..RACE_TIME).each do |time|
best_distance = best_distance_after(time)
@raindeers.each do |raindeer|
if raindeer.distance_after(time) == best_distance
scores[raindeer.name] ||= 0
scores[raindeer.name] += 1
end
end
end
puts scores.values.max
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment