-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday15.rs
67 lines (56 loc) · 2.08 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use std::fs;
use regex::Regex;
use crate::utils::toolbox::parse_usize;
pub(crate) fn day15() {
println!("{}", part_a(fs::read_to_string("input/2016/day15/input.txt").unwrap()));
println!("{}", part_b(fs::read_to_string("input/2016/day15/input.txt").unwrap()));
}
fn part_a(input: String) -> usize {
let re = Regex::new(r"^Disc #[0-9]+ has ([0-9]+) positions; at time=0, it is at position ([0-9]+)\.$").unwrap();
let discs: Vec<(usize, usize)> = input.lines().map(|line| {
let caps = re.captures(line).unwrap();
(parse_usize(caps.get(1)), parse_usize(caps.get(2)))
}).collect();
let mut time_offset = 0;
'outer: loop {
for i in 0..discs.len() {
let time = i + 1 + time_offset;
let (positions, start) = discs[i];
let position = (start + time) % positions;
if position != 0 { time_offset += 1; continue 'outer }
}
return time_offset
}
}
fn part_b(input: String) -> usize {
let re = Regex::new(r"^Disc #[0-9]+ has ([0-9]+) positions; at time=0, it is at position ([0-9]+)\.$").unwrap();
let mut discs: Vec<(usize, usize)> = input.lines().map(|line| {
let caps = re.captures(line).unwrap();
(parse_usize(caps.get(1)), parse_usize(caps.get(2)))
}).collect();
discs.push((11, 0));
let mut time_offset = 0;
'outer: loop {
for i in 0..discs.len() {
let time = i + 1 + time_offset;
let (positions, start) = discs[i];
let position = (start + time) % positions;
if position != 0 { time_offset += 1; continue 'outer }
}
return time_offset
}
}
#[cfg(test)]
mod day15_tests {
use std::fs;
use crate::y2016::day15::{part_a, part_b};
#[test]
fn test_works() {
assert_eq!(5, part_a(fs::read_to_string("input/2016/day15/test.txt").unwrap()));
}
#[test]
fn input_works() {
assert_eq!(400589, part_a(fs::read_to_string("input/2016/day15/input.txt").unwrap()));
assert_eq!(3045959, part_b(fs::read_to_string("input/2016/day15/input.txt").unwrap()));
}
}