-
Notifications
You must be signed in to change notification settings - Fork 0
/
day13.rs
139 lines (114 loc) · 3.2 KB
/
day13.rs
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
//! [Day 13: Shuttle Search](https://adventofcode.com/2020/day/13)
#[allow(clippy::many_single_char_names)]
fn egcd(a: i64, b: i64) -> (i64, i64, i64) {
if a == 0 {
(b, 0, 1)
} else {
let (g, x, y) = egcd(b % a, a);
(g, y - (b / a) * x, x)
}
}
fn mod_inv(x: i64, n: i64) -> Option<i64> {
let (g, x, _) = egcd(x, n);
if g == 1 {
Some((x % n + n) % n)
} else {
None
}
}
fn chinese_remainder(residues: &[i64], modulii: &[i64]) -> Option<i64> {
let prod = modulii.iter().product::<i64>();
let mut sum = 0;
for (&residue, &modulus) in residues.iter().zip(modulii) {
let p = prod / modulus;
sum += residue * mod_inv(p, modulus)? * p;
}
Some(sum.rem_euclid(prod))
}
struct Puzzle {
depart: u32,
buses: Vec<String>,
}
impl Puzzle {
fn new() -> Puzzle {
Puzzle {
depart: 0,
buses: vec![],
}
}
/// Get the puzzle input.
fn configure(&mut self, path: &str) {
let data = std::fs::read_to_string(path).unwrap();
let mut lines = data.lines();
self.depart = lines.next().unwrap().parse().unwrap();
self.set_buses(lines.next().unwrap());
}
fn set_buses(&mut self, buses: &str) {
self.buses = buses.split(',').map(str::to_string).collect();
}
/// Solve part one.
fn part1(&self) -> u32 {
let (minutes, id) = self
.buses
.iter()
.filter(|&id| id != "x")
.map(|id| {
let id = id.parse::<u32>().unwrap();
(((self.depart / id) + 1) * id - self.depart, id)
})
.min()
.unwrap();
minutes * id
}
/// Solve part two.
fn part2(&self) -> i64 {
let mut residues = vec![];
let mut modulii = vec![];
for (id, t) in self.buses.iter().zip(0..) {
if id != "x" {
let id: i64 = id.parse().unwrap();
modulii.push(id);
residues.push(id - t);
}
}
chinese_remainder(&residues, &modulii).unwrap()
}
}
fn main() {
let args = aoc::parse_args();
let mut puzzle = Puzzle::new();
puzzle.configure(args.path.as_str());
println!("{}", puzzle.part1());
println!("{}", puzzle.part2());
}
/// Test from puzzle input
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test01() {
let mut puzzle = Puzzle::new();
puzzle.configure("test.txt");
assert_eq!(puzzle.part1(), 295);
}
#[test]
fn test02() {
let mut puzzle = Puzzle::new();
puzzle.configure("test.txt");
assert_eq!(puzzle.part2(), 1_068_781);
}
#[test]
fn test03() {
let mut puzzle = Puzzle::new();
puzzle.set_buses("17,x,13,19");
assert_eq!(puzzle.part2(), 3417);
puzzle.set_buses("67,7,59,61");
assert_eq!(puzzle.part2(), 754_018);
puzzle.set_buses("67,x,7,59,61");
assert_eq!(puzzle.part2(), 779_210);
puzzle.set_buses("67,7,x,59,61");
assert_eq!(puzzle.part2(), 1_261_476);
puzzle.set_buses("1789,37,47,1889");
assert_eq!(puzzle.part2(), 1_202_161_486);
}
}