add day01 soulution

This commit is contained in:
Malte 2024-12-01 13:34:34 +01:00
commit 7db0c7c6c4
5 changed files with 1062 additions and 0 deletions

7
2024/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 = "aoc2024"
version = "0.1.0"

6
2024/Cargo.toml Normal file
View file

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

1000
2024/input/01_input.txt Normal file

File diff suppressed because it is too large Load diff

47
2024/src/day01.rs Normal file
View file

@ -0,0 +1,47 @@
use std::fs::read_to_string;
fn read_as_vectors(input: &str) -> (Vec<i32>, Vec<i32>) {
let input = read_to_string(input).unwrap();
let (left, right): (Vec<_>, Vec<_>) = input
.lines()
.map(|x| {
let mut l = x.split_whitespace().map(|n| n.parse::<i32>().unwrap());
(l.next().unwrap(), l.next().unwrap())
})
.unzip();
(left, right)
}
pub fn stage1(input: &str) -> i32 {
let (mut left, mut right) = read_as_vectors(input);
left.sort();
right.sort();
let res: i32 = left
.iter()
.zip(right.iter())
.map(|(l, r)| (l - r).abs())
.sum();
res
}
pub fn stage2(input: &str) -> i32 {
let (left, right) = read_as_vectors(input);
left.iter()
.map(|l| right.iter().filter(|&f| *f == *l).count() as i32 * l)
.sum()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_stage1() {
assert_eq!(stage1("input/01_input.txt"), 1590491);
}
#[test]
fn test_stage2() {
assert_eq!(stage2("input/01_input.txt"), 22588371);
}
}

2
2024/src/lib.rs Normal file
View file

@ -0,0 +1,2 @@
#[allow(dead_code)]
mod day01;