-
Notifications
You must be signed in to change notification settings - Fork 0
/
day01.rs
44 lines (37 loc) · 845 Bytes
/
day01.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
use std::fs;
use std::io;
fn main() -> io::Result<()> {
let mut xs: Vec<i32> =
fs::read_to_string("input/input01.txt")?
.split_whitespace()
.map(|s: &str| s.parse().unwrap())
.collect();
xs.sort();
// Solve both parts using two-pointers trick
// in O(n) and O(n^2) respectively.
let target = 2020;
let mut i = 0;
let mut j = xs.len()-1;
while i < j && xs[i]+xs[j] != target {
if xs[i]+xs[j] < target { i += 1; }
else { j -= 1; }
}
println!("Part One: {}", xs[i]*xs[j]);
i = 0;
j = 1;
let mut k = xs.len()-1;
while j < k && xs[i]+xs[j]+xs[k] != target {
if xs[i]+xs[j]+xs[k] < target {
j += 1;
if j == k {
i += 1;
j = i+1;
k = xs.len()-1;
}
} else {
k -= 1;
}
}
println!("Part Two: {}", xs[i]*xs[j]*xs[k]);
Ok(())
}