-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
67 lines (60 loc) · 1.73 KB
/
main.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
use std::collections::BTreeMap;
fn part1(inputs: &Vec<u32>) -> usize {
let mut jolts = inputs.clone();
jolts.push(0);
jolts.sort();
let diffs = jolts[..]
.windows(2)
.map(|c| c[1] as i32 - c[0] as i32)
.collect::<Vec<i32>>();
let one_jolts = diffs.iter().filter(|x| **x == 1).count();
let three_jolts = diffs.iter().filter(|x| **x == 3).count() + 1;
println!(
"Part 1: {} * {} = {}",
one_jolts,
three_jolts,
one_jolts * three_jolts
);
one_jolts * three_jolts
}
fn part2(inputs: &Vec<u32>) -> i64 {
let mut jolts = inputs.clone();
jolts.sort();
let mut combinations: BTreeMap<i64, i64> = BTreeMap::new();
let mut last: i64 = 0;
combinations.insert(0, 1);
for num in jolts.iter() {
let n = *num as i64;
let c1 = combinations.get(&(n - 1)).unwrap_or(&0);
let c2 = combinations.get(&(n - 2)).unwrap_or(&0);
let c3 = combinations.get(&(n - 3)).unwrap_or(&0);
last = c1 + c2 + c3;
combinations.insert(n, last);
}
println!("Part 2: {}", last);
last
}
fn parse(s: &str) -> Vec<u32> {
s.split('\n')
.map(|s| s.parse::<u32>().unwrap())
.collect::<Vec<_>>()
}
fn main() {
let inputs = parse(include_str!("../in.txt"));
part1(&inputs);
part2(&inputs);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_example1() {
assert_eq!(part1(&parse(include_str!("../test1.txt"))), 35);
assert_eq!(part2(&parse(include_str!("../test1.txt"))), 8);
}
#[test]
fn test_example2() {
assert_eq!(part1(&parse(include_str!("../test2.txt"))), 220);
assert_eq!(part2(&parse(include_str!("../test2.txt"))), 19208);
}
}