Solution for Day 13

This commit is contained in:
2020-12-13 15:23:36 +01:00
parent 6b560dfd87
commit c2523739c7
4 changed files with 46 additions and 0 deletions

24
day_13/second.rb Normal file
View File

@ -0,0 +1,24 @@
# frozen_string_literal: true
input = File.readlines('./input')
$ids = input[1].split(',')
$ids = $ids.map(&:to_i)
$first = 0
$period = 1
$ids.each_with_index do |bus_period, id|
next if bus_period == 0
0.step do |i|
cand = $first + $period * i
if (cand + id) % bus_period == 0
$first = cand
$period = $period.lcm bus_period
break
end
end
end
puts $first