-
Notifications
You must be signed in to change notification settings - Fork 0
/
day09.rs
63 lines (50 loc) · 1.08 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
use std::fs;
fn find_sum(ys: &[i64], v: i64) -> bool {
let mut xs: Vec<i64> = ys.iter().cloned().collect();
xs.sort();
let mut i = 0;
let mut j = xs.len()-1;
while i < j && xs[i]+xs[j] != v {
if xs[i]+xs[j] < v { i += 1 }
else { j -= 1 }
}
i<j && xs[i]+xs[j]==v
}
fn part1(xs: &Vec<i64>) -> i64 {
let n = xs.len();
for i in 25..n {
if ! find_sum(&xs[i-25..i], xs[i]) {
return xs[i]
}
}
-1
}
fn part2(xs: &Vec<i64>, v: i64) -> i64 {
// Two Pointers again
let n = xs.len();
let mut i = 0;
let mut j = 2;
let mut acc = xs[0]+xs[1];
while acc != v && (j < n || i+2 < j) {
if (acc < v && j < n) || i+2 >= j {
acc += xs[j];
j += 1;
} else {
acc -= xs[i];
i += 1;
}
}
let vs = &xs[i..j];
*vs.iter().max().unwrap()+*vs.iter().min().unwrap()
}
fn main() {
let xs: Vec<i64> =
fs::read_to_string("input/input09.txt")
.unwrap()
.lines()
.map(|x| x.parse().unwrap())
.collect();
let v = part1(&xs);
println!("Part 1: {}", v);
println!("Part 2: {}", part2(&xs, v));
}