-
Notifications
You must be signed in to change notification settings - Fork 0
/
01.rs
37 lines (31 loc) · 785 Bytes
/
01.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
#![feature(test)]
use std::collections::HashSet;
type Input = Vec<i32>;
fn setup(input: &str) -> Input {
input.lines().map(|i| i.parse().unwrap()).collect()
}
fn part1(input: &Input) -> i32 {
let mut seen: HashSet<i32> = HashSet::new();
for &x in input {
let y = 2020 - x;
if seen.contains(&y) {
return x * y;
}
seen.insert(x);
}
panic!();
}
fn part2(input: &Input) -> i32 {
for (i, &x) in input.iter().enumerate() {
let mut seen: HashSet<i32> = HashSet::new();
for &y in &input[i + 1..] {
let z = 2020 - x - y;
if seen.contains(&z) {
return x * y * z;
}
seen.insert(y);
}
}
panic!();
}
aoc::main!(2020, 1, ex: 1);