-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.rb
62 lines (49 loc) · 1.59 KB
/
solution.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# frozen_string_literal: true
require_relative '../solution'
module Day13
class Solution < Solution
def solve_part01
estimate_timestamp = input[0].to_i
result = input[1].gsub('x,', '').split(',').map do |bus_id|
{ bus_id: bus_id.to_i, wait_minutes: bus_id.to_i - (estimate_timestamp % bus_id.to_i) }
end.min_by { |hash| hash[:wait_minutes] }
result[:bus_id] * result[:wait_minutes]
end
def solve_part02
buses = load_buses
mods = buses.keys.map(&:to_i)
remainders = buses.values.map(&:to_i)
chinese_remainder mods, remainders
end
private
def load_buses
input[1].split(',').map.with_index do |bus_id, t|
next {} if bus_id == 'x'
{ bus_id => -t }
end.reduce(&:merge)
end
# Source: https://rosettacode.org/wiki/Chinese_remainder_theorem#Ruby
def chinese_remainder(mods, remainders)
max = mods.inject(:*)
series = remainders.zip(mods).map { |r, m| (r * max * invmod(max / m, m) / m) }
series.inject(:+) % max
end
def extended_gcd(a, b)
last_remainder, remainder = a.abs, b.abs
x, last_x, y, last_y = 0, 1, 1, 0
while remainder != 0
last_remainder, (quotient, remainder) = remainder, last_remainder.divmod(remainder)
x, last_x = last_x - quotient * x, x
y, last_y = last_y - quotient * y, y
end
[last_remainder, last_x * (a < 0 ? -1 : 1)]
end
def invmod(e, et)
g, x = extended_gcd(e, et)
if g != 1
raise 'Multiplicative inverse modulo does not exist!'
end
x % et
end
end
end