Advent of Code solutions
1use advent_core::{day_stuff, ex_for_day, Day};
2use utils::misc::counts;
3
4pub struct Day1;
5
6impl Day for Day1 {
7 day_stuff!(1, "11", "31", (Vec<i32>, Vec<i32>));
8
9 fn part_1(input: Self::Input) -> Option<String> {
10 let (mut l, mut r) = input;
11 l.sort_unstable();
12 r.sort_unstable();
13
14 Some(
15 l.into_iter()
16 .zip(r)
17 .map(|(l, r)| (l - r).abs())
18 .sum::<i32>()
19 .to_string(),
20 )
21 }
22
23 fn part_2(input: Self::Input) -> Option<String> {
24 let (l, r) = input;
25 let apr = counts(r.into_iter());
26 Some(
27 l.into_iter()
28 .map(|l| l as u64 * apr.get(&l).unwrap_or(&0))
29 .sum::<u64>()
30 .to_string(),
31 )
32 }
33
34 fn parse_input(input: &str) -> Self::Input {
35 input
36 .split("\n")
37 .map(|line| {
38 let mut split = line.trim().split_ascii_whitespace();
39 let left = split.next().unwrap().parse::<i32>().unwrap();
40 let right = split.next().unwrap().parse::<i32>().unwrap();
41 (left, right)
42 })
43 .collect()
44 }
45}