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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use crate::input::Input;
#[derive(Copy, Clone)]
struct ExtendedEuclidResult {
gcd: i128,
x: i128,
y: i128,
}
fn extended_euclid(a: i128, b: i128) -> ExtendedEuclidResult {
if b == 0 {
ExtendedEuclidResult { gcd: a, x: 1, y: 0 }
} else {
let next = extended_euclid(b, a % b);
ExtendedEuclidResult {
gcd: next.gcd,
x: next.y,
y: next.x - (a / b) * next.y,
}
}
}
fn modular_multiplicative_inverse(a: i128, m: i128) -> Option<i128> {
let ExtendedEuclidResult { gcd, x, .. } = extended_euclid(a, m);
if gcd == 1 {
if x > 0 {
Some(x)
} else {
Some(x + m)
}
} else {
None
}
}
fn chinese_remainder(remainders: &[i128], divisors: &[i128]) -> Option<i128> {
let all_divisors_multiplied = divisors.iter().product::<i128>();
let mut sum = 0;
for (&remainder, &divisor) in remainders.iter().zip(divisors) {
let other_divisors_multiplied = all_divisors_multiplied / divisor;
let value_with_one_as_remainder = other_divisors_multiplied
* modular_multiplicative_inverse(other_divisors_multiplied, divisor)?;
let fulfilling_value = remainder * value_with_one_as_remainder;
sum += fulfilling_value;
}
Some(sum % all_divisors_multiplied)
}
pub fn solve(input: &mut Input) -> Result<i128, String> {
let mut lines = input.text.lines();
let not_until = lines
.next()
.ok_or("Not two lines")?
.parse::<u32>()
.map_err(|error| format!("Line 1: Cannot parse number - {}", error))?;
let bus_ids = lines
.next()
.ok_or("Not two lines")?
.split(',')
.enumerate()
.filter_map(|(offset, entry)| {
if entry == "x" {
None
} else {
match entry.parse::<u32>() {
Ok(value) => Some(Ok((offset, value))),
Err(error) => Some(Err(format!("Line 2: Invalid entry - {}", error))),
}
}
})
.collect::<Result<Vec<_>, _>>()?;
if input.is_part_one() {
let (bus_id, wait_time) = bus_ids
.iter()
.map(|(_offset, bus_id)| (bus_id, (bus_id - not_until % bus_id) % bus_id))
.min_by(|&a, &b| a.1.cmp(&b.1))
.ok_or("No bus ID:s")?;
Ok(i128::from(bus_id * wait_time))
} else {
let remainders = bus_ids
.iter()
.map(|&(offset, bus_id)| i128::from(bus_id) - offset as i128)
.collect::<Vec<_>>();
let divisors = bus_ids
.iter()
.map(|&(_offset, bus_id)| i128::from(bus_id))
.collect::<Vec<_>>();
chinese_remainder(&remainders, &divisors)
.ok_or_else(|| "Bus id:s not pairwise coprime".to_string())
}
}
#[test]
pub fn tests() {
use crate::{test_part_one, test_part_two};
let example = "939\n7,13,x,x,59,x,31,19";
test_part_one!(example => 295);
test_part_two!(example => 1_068_781);
test_part_one!("100\n10" => 0);
let real_input = include_str!("day13_input.txt");
test_part_one!(real_input => 4722);
test_part_two!(real_input => 825_305_207_525_452);
}