-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-1.rs
56 lines (44 loc) · 1.2 KB
/
day-1.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
const INPUT: &str = include_str!("day-1.input");
fn main() {
part_1();
part_2();
}
fn part_1() {
println!("=[ part 1 ]=");
let mut sums = [-1; 2020];
for line in INPUT.lines() {
let i: i32 = line.parse().expect("expected i32");
let other = sums[i as usize];
if other >= 0 {
println!("{} + {} = {}", other, i, other + i);
println!("{} * {} = {}", other, i, other * i);
return;
}
sums[2020 - i as usize] = i;
}
println!("not found");
}
fn part_2() {
println!("\n=[ part 2 ]=");
let mut nums = Vec::<i32>::new();
for line in INPUT.lines() {
nums.push(line.parse().expect("expected i32"));
}
let mut sums = [None; 2020];
for &i in nums.iter() {
for &j in nums.iter() {
let ij = i + j;
if ij < 2020 {
sums[2020 - ij as usize] = Some((i, j));
}
}
}
for &i in nums.iter() {
if let Some((a, b)) = sums[i as usize] {
println!("{} + {} + {} = {}", a, b, i, a + b + i);
println!("{} * {} * {} = {}", a, b, i, a * b * i);
return;
}
}
println!("not found");
}