-
Notifications
You must be signed in to change notification settings - Fork 0
/
day09.rs
95 lines (81 loc) · 1.8 KB
/
day09.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
use std::cmp::Ordering;
use std::fs;
fn parse(input: &str) -> Vec<usize> {
input.lines().flat_map(str::parse).collect()
}
fn find_invalid_number(data: &[usize], wlen: usize) -> Option<usize> {
'outer: for w in data.windows(wlen + 1) {
let target = w[wlen];
for i in 0..wlen {
for j in 0..wlen {
if w[i] + w[j] == target && i != j {
continue 'outer;
}
}
}
return Some(target);
}
None
}
fn find_enc_weakness(data: &[usize], target: usize) -> usize {
let mut lower = 0;
let mut upper = 1;
loop {
match data[lower..upper].iter().sum::<usize>().cmp(&target) {
Ordering::Less => upper += 1,
Ordering::Equal => break,
Ordering::Greater => lower += 1,
}
}
let (min, max) = data[lower..upper]
.iter()
.fold((usize::MAX, 0), |(min, max), &v| (v.min(min), v.max(max)));
min + max
}
struct Solution;
impl Solution {
fn part1(data: &[usize]) -> usize {
find_invalid_number(data, 25).unwrap()
}
fn part2(data: &[usize], target: usize) -> usize {
find_enc_weakness(data, target)
}
}
fn main() {
let input = fs::read_to_string("./input/day09.txt").expect("File not found!");
let data = parse(&input);
let invalid_number = Solution::part1(&data);
println!("p1: {}", invalid_number);
println!("p2: {}", Solution::part2(&data, invalid_number));
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_day09() {
let input = "\
35
20
15
25
47
40
62
55
65
95
102
117
150
182
127
219
299
277
309
576";
let data = parse(input);
assert_eq!(find_invalid_number(&data, 5), Some(127));
assert_eq!(find_enc_weakness(&data, 127), 62);
}
}