-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day9.rs
41 lines (34 loc) · 1.09 KB
/
Day9.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
use std::io::prelude::*;
use std::io::stdin;
fn main() {
const PREAMBLE_SIZE: usize = 25;
const FAILURE: &str = "failed to find weakness";
let stdin = stdin();
let numbers: Vec<_> = stdin.lock()
.lines()
.map(|line| line.expect("failed to read from stdin").parse::<i64>().unwrap())
.collect();
let first = (PREAMBLE_SIZE..numbers.len() - 1)
.find(|&i| {
let range = numbers.get(i - PREAMBLE_SIZE..i).expect(FAILURE);
return range.iter().all(|&j| range.iter().all(|&k| j + k != numbers[i]));
})
.map(|i| numbers[i])
.expect(FAILURE);
let mut i = 0;
let mut j = 0;
let mut sum: i64 = 0;
while sum != first && j != numbers.len() {
while sum < first {
sum += numbers[j];
j += 1;
}
if sum > first {
sum -= numbers[i];
i += 1;
}
}
let range = numbers.get(i..j - 1).expect(FAILURE);
let second = range.iter().max().expect(FAILURE) + range.iter().min().expect(FAILURE);
println!("{} {}", first, second);
}