aoc2023/day_6/impl.rb

28 lines
630 B
Ruby

#prepare the input which is a string containing new lines
def parse(input)
data = []
input.each_line do |line|
data << line.split(":").last.split(" ").map(&:to_i)
end
data
end
# result should a single string or integer
def calculate(data)
result = []
times,distances = data
times.length.times do |i|
time = times[i]
distance = distances[i]
win_count = 0
(1..time).each do |duration|
speed = duration
time_left = time - duration
travel_distance = speed * time_left
win_count += 1 if travel_distance > distance
end
result << win_count
end
result.reduce(:*)
end