-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathday15.rs
More file actions
40 lines (34 loc) · 1.43 KB
/
Copy pathday15.rs
File metadata and controls
40 lines (34 loc) · 1.43 KB
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
//! # Timing is Everything
//!
//! Part one is the [Chinese Remainder Theorem](https://en.wikipedia.org/wiki/Chinese_remainder_theorem).
//! The integers n₁, n₂, ... nₖ map to the disc sizes which happen to be prime. This satisfies the
//! requirement that the integers are [pairwise coprime](https://en.wikipedia.org/wiki/Coprime_integers#Coprimality_in_sets).
//!
//! For simplicity we use the "search by sieving" method. We start at zero with a step the size of
//! the first integer. Then we search for each subsequent integer located at the correct offset of
//! minutes and multiply the step by the new integer. This preserves the relative offset at each
//! step in the next search.
use crate::util::iter::*;
use crate::util::parse::*;
type Pair = [usize; 2];
pub fn parse(input: &str) -> Pair {
let disks: Vec<Pair> = input.iter_unsigned().skip(1).step_by(2).chunk::<2>().collect();
let (part1, step) = solve(&disks, 0, 0, 1);
let (part2, _step) = solve(&[[11, 0]], disks.len(), part1, step);
[part1, part2]
}
pub fn part1(input: &Pair) -> usize {
input[0]
}
pub fn part2(input: &Pair) -> usize {
input[1]
}
fn solve(discs: &[Pair], offset: usize, mut time: usize, mut step: usize) -> (usize, usize) {
for (o, &[size, position]) in discs.iter().enumerate() {
while !(time + offset + o + 1 + position).is_multiple_of(size) {
time += step;
}
step *= size;
}
(time, step)
}