-
Notifications
You must be signed in to change notification settings - Fork 0
/
01.rs
67 lines (54 loc) · 1.54 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
use std::collections::HashMap;
use advent_of_code::ws;
use nom::{character::complete::u32, sequence::pair, IResult};
advent_of_code::solution!(1);
fn parse_line(line: &str) -> (u32, u32) {
let result: IResult<_, _> = pair(ws(u32), ws(u32))(line);
result.expect("😎").1
}
pub fn part_one(input: &str) -> Option<u32> {
let (mut left, mut right): (Vec<_>, Vec<_>) =
input.lines().map(parse_line).unzip();
left.sort();
right.sort();
Some(
left.into_iter()
.zip(right)
.map(|(l, r)| l.abs_diff(r))
.sum(),
)
}
pub fn part_two(input: &str) -> Option<u32> {
let (left, right): (Vec<_>, Vec<_>) = input.lines().map(parse_line).unzip();
let mut right_counts = HashMap::<u32, u32>::new();
for v in right.into_iter() {
right_counts
.entry(v)
.and_modify(|existing| *existing += 1)
.or_insert(1);
}
let result = left
.into_iter()
.map(|left| {
(right_counts.get(&left).copied().unwrap_or_default(), left)
})
.map(|(left, right)| left * right)
.sum();
Some(result)
}
#[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));
}
}