Solution for Day 13

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

18
day_13/first.rb Normal file
View File

@ -0,0 +1,18 @@
# frozen_string_literal: true
input = File.readlines('./input')
$earliest = Integer(input.first)
$ids = input[1].split(',')
$ids.delete 'x'
$ids = $ids.map(&:to_i)
out = $ids.map do |id|
b = $earliest / id
nearest = b * id
nearest < $earliest ? nearest + id : nearest
end
busid = $ids[out.index(out.min)]
puts busid * (out.min - $earliest)

2
day_13/input Normal file
View File

@ -0,0 +1,2 @@
1000186
17,x,x,x,x,x,x,x,x,x,x,37,x,x,x,x,x,907,x,x,x,x,x,x,x,x,x,x,x,19,x,x,x,x,x,x,x,x,x,x,23,x,x,x,x,x,29,x,653,x,x,x,x,x,x,x,x,x,41,x,x,13

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

2
day_13/test Normal file
View File

@ -0,0 +1,2 @@
939
7,13,x,x,59,x,31,19