commit e55a2e38c405e963ac17584a77bea7c80136a39b Author: Adrian Groh Date: Sun Dec 1 21:30:28 2024 +0100 Add day1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9d426e5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*input.txt diff --git a/day1/Cargo.lock b/day1/Cargo.lock new file mode 100644 index 0000000..ae5bfe8 --- /dev/null +++ b/day1/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day1" +version = "0.1.0" diff --git a/day1/Cargo.toml b/day1/Cargo.toml new file mode 100644 index 0000000..fb6b7ec --- /dev/null +++ b/day1/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day1" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/day1/src/main.rs b/day1/src/main.rs new file mode 100644 index 0000000..7dcd794 --- /dev/null +++ b/day1/src/main.rs @@ -0,0 +1,39 @@ +fn parse(input: &str) -> (Vec, Vec) { + input + .lines() + .map(|line| { + let tmp = line.split_once(' '); + ( + tmp.unwrap().0.parse::().unwrap(), + tmp.unwrap().1.trim().parse::().unwrap(), + ) + }) + .collect() +} + +fn part1(input: &(Vec, Vec)) -> u32 { + let mut firsts = input.0.clone(); + let mut seconds = input.1.clone(); + firsts.sort_unstable(); + seconds.sort_unstable(); + firsts + .iter() + .zip(seconds.iter()) + .map(|(f, s)| u32::abs_diff(*f, *s)) + .sum() +} + +fn part2(input: &(Vec, Vec)) -> u32 { + input + .0 + .iter() + .map(|num| num * input.1.iter().filter(|x| x == &num).count() as u32) + .sum() +} + +fn main() { + let input = include_str!("../input.txt"); + let parsed_input = parse(input); + println!("{}", part1(&parsed_input)); + println!("{}", part2(&parsed_input)); +}