-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday15.rs
51 lines (39 loc) · 1.13 KB
/
day15.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
//! [Day 15: Timing is Everything](https://adventofcode.com/2016/day/15)
use regex::Regex;
pub fn main() {
let args = aoc::parse_args();
args.run(solve);
}
/// # Panics
#[must_use]
pub fn solve(data: &str) -> (usize, usize) {
let mut discs = Vec::new();
let mut positions = Vec::new();
let re = Regex::new(r"Disc #\d+ has (\d+) positions; at time=0, it is at position (\d+).").unwrap();
for line in data.lines() {
let caps = re.captures(line).unwrap();
discs.push(caps[2].parse::<usize>().unwrap());
positions.push(caps[1].parse::<usize>().unwrap());
}
// part 1
let mut part1 = 0;
let nb_discsc = discs.len();
for time in 0.. {
if (0..nb_discsc).all(|i| (discs[i] + i + time + 1) % positions[i] == 0) {
part1 = time;
break;
}
}
// part 2
let mut part2 = 0;
discs.push(0);
positions.push(11);
let nb_discsc = discs.len();
for time in 0.. {
if (0..nb_discsc).all(|i| (discs[i] + i + time + 1) % positions[i] == 0) {
part2 = time;
break;
}
}
(part1, part2)
}