generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
06.rs
89 lines (81 loc) · 2.57 KB
/
06.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
advent_of_code::solution!(6);
fn get_lower_race_quadratic(time: u64, record: u64) -> u64 {
let delta = time.pow(2) - 4 * record;
let delta_f64: f64 = delta as f64;
let delta_sq = delta_f64.sqrt();
let delta_sq_u64 = delta_sq as u64;
let root = (time - delta_sq_u64) / 2;
// usually root + 1, but sometimes off
if root * (time - root) > record {
root
} else {
root + 1
}
}
fn extract_vec(s: &str) -> Vec<u64> {
s.split(':')
.last()
.expect("There should be a :")
.split_whitespace()
.map(|s| s.parse::<u64>().expect("Times should be numbers"))
.collect()
}
pub fn part_one(input: &str) -> Option<u64> {
let mut lines = input.lines();
let times: Vec<u64> = extract_vec(lines.next().expect("There should be a first line"));
let records: Vec<u64> = extract_vec(lines.next().expect("There should be a second line"));
times
.iter()
.zip(records.iter())
.map(|(&time, &record)| {
// naive approach - is actually slightly faster
// let mut x = 1;
// let winning = loop {
// if x * (time - x) > record {
// break x;
// }
// x += 1;
// };
let winning = get_lower_race_quadratic(time, record);
time - winning - winning + 1
})
.reduce(|acc, x| acc * x)
}
fn extract_num(s: &str) -> u64 {
s.split(':')
.last()
.expect("There should be a :")
.split_whitespace()
.fold(String::new(), |acc, s| acc + s)
.parse::<u64>()
.expect("Records should be numbers")
}
pub fn part_two(input: &str) -> Option<u64> {
let mut lines = input.lines();
let time: u64 = extract_num(lines.next().expect("There should be a first line"));
let record: u64 = extract_num(lines.next().expect("There should be a second line"));
// naive approach - is much slower
// let mut x = 1;
// let winning = loop {
// if x * (time - x) > record {
// break x;
// }
// x += 1;
// };
let winning = get_lower_race_quadratic(time, record);
Some(time - winning - winning + 1)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(288));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(71503));
}
}