-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday01.rs
74 lines (63 loc) · 1.85 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use std::collections::HashMap;
use std::fs;
struct Solution;
impl Solution {
fn find_sum_pair(data: &[i32], target: i32) -> Option<(i32, i32)> {
let mut set = HashMap::new();
for v in data {
let comp = target - v;
match set.get(&comp) {
Some(val) => return Some((comp, *val)),
None => set.insert(*v, comp),
};
}
None
}
fn find_sum_triple(data: &[i32], target: i32) -> Option<(i32, i32, i32)> {
for v in data {
let comp = target - v;
if let Some((a, b)) = Self::find_sum_pair(data, comp) {
if a != b && a != *v && b != *v {
return Some((a, b, *v));
}
}
}
None
}
fn part1(data: &[i32], target: i32) -> i32 {
match Self::find_sum_pair(data, target) {
Some((a, b)) => a * b,
None => -1,
}
}
fn part2(data: &[i32], target: i32) -> i32 {
match Self::find_sum_triple(data, target) {
Some((a, b, c)) => a * b * c,
None => -1,
}
}
}
fn main() {
let input = fs::read_to_string("./input/day01.txt").expect("File not found!");
let values: Vec<i32> = input.lines().map(|x| x.parse::<i32>().unwrap()).collect();
println!("p1: {}", Solution::part1(&values, 2020));
println!("p2: {}", Solution::part2(&values, 2020));
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_day01_find_sum_pair() {
assert_eq!(
Solution::find_sum_pair(&[1721, 979, 366, 299, 675, 1456], 2020),
Some((1721, 299))
);
}
#[test]
fn test_day01_find_sum_triple() {
assert_eq!(
Solution::find_sum_triple(&[1721, 979, 366, 299, 675, 1456], 2020),
Some((366, 675, 979))
);
}
}