generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01.rs
95 lines (77 loc) · 2.19 KB
/
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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use std::ops::Sub;
advent_of_code::solution!(1);
pub fn part_one(input: &str) -> Option<u32> {
let mut left = Vec::new();
let mut right = Vec::new();
for line in input.lines() {
let mut parts = line.split(";");
let first: u32 = parts.nth(0).unwrap().parse().unwrap();
left.push(first);
let second: u32 = parts.last().unwrap().parse().unwrap();
right.push(second);
}
let mut res = 0;
for i in 0..left.len() {
let mut min_left = 0;
let mut min_right = 0;
if let Some(&min) = left.iter().min() {
min_left = min;
if let Some(idx) = left.iter().position(|&el| el == min) {
left.remove(idx);
}
}
if let Some(&min) = right.iter().min() {
min_right = min;
if let Some(idx) = right.iter().position(|&el| el == min) {
right.remove(idx);
}
}
let diff = abs_diff(min_left, min_right);
res = res + diff;
}
Some(res)
}
fn abs_diff<T: PartialOrd + Sub<Output = T>>(a: T, b: T) -> T {
if a > b {
a - b
} else {
b - a
}
}
pub fn part_two(input: &str) -> Option<u32> {
let mut left = Vec::new();
let mut right = Vec::new();
for line in input.lines() {
let mut parts = line.split(";");
let first: u32 = parts.nth(0).unwrap().parse().unwrap();
left.push(first);
let second: u32 = parts.last().unwrap().parse().unwrap();
right.push(second);
}
let mut res = 0;
for num in left.iter() {
let occurences: u32 = right
.iter()
.filter(|&x| x == num)
.count()
.try_into()
.unwrap();
let score = num * occurences;
res = res + score;
}
Some(res)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(11));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(31));
}
}