-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09.rs
76 lines (68 loc) · 2.33 KB
/
09.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
fn parse_encrypted(input: &str) -> Vec<u64> {
input
.lines()
.map(str::parse::<u64>)
.collect::<Result<Vec<_>, _>>()
.expect("Parsing failed!")
}
fn get_first_invalid(encrypted: &[u64]) -> Option<(usize, u64)> {
let preamble_size = 25;
for (step, num) in encrypted.iter().enumerate().skip(preamble_size) {
let leading_numbers = &encrypted[step - preamble_size..step];
if !leading_numbers
.iter()
.filter(|previous_number| *previous_number <= num)
.any(|previous_number| leading_numbers.contains(&(num - previous_number)))
{
return Some((step, *num));
}
}
None
}
fn solve_part_one(first_invalid: &Option<(usize, u64)>) {
match first_invalid {
Some((position, invalid)) => println!(
"The scheme is not fulfilled on position {} with value {}.",
position, invalid
),
None => println!("There is no invalid position."),
}
}
fn find_weakness(encrypted: &[u64], invalid: u64) -> Option<u64> {
let mut sequence: std::collections::VecDeque<u64> = std::collections::VecDeque::new();
for num in encrypted.iter() {
sequence.push_back(*num);
let mut sequence_sum = sequence.iter().sum();
while invalid < sequence_sum {
sequence.pop_front();
sequence_sum = sequence.iter().sum();
}
if invalid == sequence_sum {
return Some(
sequence.iter().min().expect("Empty sequence!")
+ sequence.iter().max().expect("Empty sequence!"),
);
}
}
None
}
fn solve_part_two(encrypted: &[u64], first_invalid: &Option<(usize, u64)>) {
let first_invalid = match first_invalid {
Some((_position, invalid_number)) => *invalid_number,
None => {
println!("There is no invalid position.");
return;
}
};
match find_weakness(encrypted, first_invalid) {
Some(weakness) => println!("The weakness is {}.", weakness),
None => println!("There is no weakness."),
}
}
fn main() {
let input = include_str!("09_data.enc");
let encrypted = parse_encrypted(input);
let first_invalid = get_first_invalid(&encrypted);
solve_part_one(&first_invalid);
solve_part_two(&encrypted, &first_invalid);
}