Advent of Code solutions
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

2024 Day 1

Ben C 35b43512 90d639e1

+36 -8
+1 -1
advent_core/src/parser.rs
··· 1 1 use std::env::args; 2 - use std::io::{stdin, Read}; 3 2 use std::fs; 3 + use std::io::{stdin, Read}; 4 4 5 5 #[derive(Clone, Debug)] 6 6 pub enum Selection {
+23 -5
years/2024/src/day_1.rs
··· 1 + 2 + use std::collections::HashMap; 1 3 2 4 use advent_core::{Day, day_stuff, ex_for_day}; 3 5 ··· 5 7 6 8 impl Day for Day1 { 7 9 8 - day_stuff!(1, "", ""); 10 + day_stuff!(1, "11", "31", (Vec<i32>, Vec<i32>)); 9 11 10 - fn part_1(_input: Self::Input) -> Option<String> { 11 - None 12 + fn part_1(input: Self::Input) -> Option<String> { 13 + let (mut l, mut r) = input; 14 + l.sort_unstable(); 15 + r.sort_unstable(); 16 + Some(l.into_iter().zip(r.into_iter()).map(|(l, r)| (l - r).abs()).sum::<i32>().to_string()) 12 17 } 13 18 14 - fn part_2(_input: Self::Input) -> Option<String> { 15 - None 19 + fn part_2(input: Self::Input) -> Option<String> { 20 + let (l, r) = input; 21 + let apr = r.into_iter().fold(HashMap::new(), |mut a, curr| { 22 + a.entry(curr).and_modify(|x| { *x += 1 }).or_insert(1); 23 + a 24 + }); 25 + Some(l.into_iter().map(|l| l * apr.get(&l).unwrap_or(&0)).sum::<i32>().to_string()) 16 26 } 27 + 28 + fn parse_input(input: &str) -> Self::Input { 29 + input.split("\n").map(|l| { 30 + let mut l = l.trim().split_ascii_whitespace(); 31 + (l.next().map(|x| x.parse::<i32>().unwrap()).unwrap(), l.next().map(|x| x.parse::<i32>().unwrap()).unwrap()) 32 + }).collect::<(Vec<_>, Vec<_>)>() 33 + } 34 + 17 35 }
+6
years/2024/src/examples/day_1/1.txt
··· 1 + 3 4 2 + 4 3 3 + 2 5 4 + 1 3 5 + 3 9 6 + 3 3
+6
years/2024/src/examples/day_1/2.txt
··· 1 + 3 4 2 + 4 3 3 + 2 5 4 + 1 3 5 + 3 9 6 + 3 3
-1
years/2024/src/lib.rs
··· 1 - 2 1 use macros::year; 3 2 4 3 year!(2024);
-1
years/2024/src/main.rs
··· 1 - 2 1 use macros::year_runner; 3 2 4 3 year_runner!(2024);