This commit is contained in:
Adrian Groh 2024-12-01 21:30:28 +01:00
commit e55a2e38c4
Signed by: Gobidev
GPG Key ID: 3AA3153E98B0D771
4 changed files with 53 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*input.txt

7
day1/Cargo.lock generated Normal file
View File

@ -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"

6
day1/Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "day1"
version = "0.1.0"
edition = "2021"
[dependencies]

39
day1/src/main.rs Normal file
View File

@ -0,0 +1,39 @@
fn parse(input: &str) -> (Vec<u32>, Vec<u32>) {
input
.lines()
.map(|line| {
let tmp = line.split_once(' ');
(
tmp.unwrap().0.parse::<u32>().unwrap(),
tmp.unwrap().1.trim().parse::<u32>().unwrap(),
)
})
.collect()
}
fn part1(input: &(Vec<u32>, Vec<u32>)) -> 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<u32>, Vec<u32>)) -> 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));
}