Day 1

This commit is contained in:
LeMoonStar 2024-12-01 06:37:02 +01:00
parent 73760fbec4
commit 49c441a277
4 changed files with 38 additions and 8 deletions

2
.gitignore vendored
View file

@ -1,2 +1,2 @@
/target /target
.aoc23_cache .aoc24_cache

View file

@ -3,8 +3,8 @@
[![About](https://img.shields.io/badge/Advent%20of%20Code-2024-brightgreen?style=flat-square)](https://adventofcode.com/2024/about) [![About](https://img.shields.io/badge/Advent%20of%20Code-2024-brightgreen?style=flat-square)](https://adventofcode.com/2024/about)
[![Language: Rust](https://img.shields.io/badge/Language-Rust-orange.svg?style=flat-square)](https://en.wikipedia.org/wiki/Rust_(programming_language)) [![Language: Rust](https://img.shields.io/badge/Language-Rust-orange.svg?style=flat-square)](https://en.wikipedia.org/wiki/Rust_(programming_language))
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square)](https://mit-license.org/) [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square)](https://mit-license.org/)
![Days completed](https://img.shields.io/badge/Days%20completed-0-red?style=flat-square) ![Days completed](https://img.shields.io/badge/Days%20completed-1-red?style=flat-square)
![Stars](https://img.shields.io/badge/Stars-0-yellow?style=flat-square) ![Stars](https://img.shields.io/badge/Stars-2-yellow?style=flat-square)
> ⚠️ This README is copied from my previous years solution. It is not fully adopted to 2024 yet. > ⚠️ This README is copied from my previous years solution. It is not fully adopted to 2024 yet.

View file

@ -2,7 +2,7 @@ use super::{Answer, Day, DayImpl};
const CURRENT_DAY: u8 = 1; const CURRENT_DAY: u8 = 1;
type Data = Vec<u64>; type Data = Vec<[u64; 2]>;
impl DayImpl<Data> for Day<CURRENT_DAY> { impl DayImpl<Data> for Day<CURRENT_DAY> {
fn init_test() -> (Self, Data) { fn init_test() -> (Self, Data) {
Self::init(include_str!("test_inputs/test01.txt")) Self::init(include_str!("test_inputs/test01.txt"))
@ -17,16 +17,40 @@ impl DayImpl<Data> for Day<CURRENT_DAY> {
Self {}, Self {},
input input
.lines() .lines()
.map(|v| v.parse::<u64>().expect("error while parsing input.")) .map(|v| {
.collect(), v.split_whitespace()
.map(|v| v.trim().parse::<u64>().expect("Error during parsing"))
.collect::<Vec<u64>>()
.try_into()
.expect("Error during parsing")
})
.collect::<Vec<[u64; 2]>>(),
) )
} }
fn one(&self, data: &mut Data) -> Answer { fn one(&self, data: &mut Data) -> Answer {
Answer::Number(data.len() as u64) let mut left = data.iter().map(|v| v[0]).collect::<Vec<u64>>();
let mut right = data.iter().map(|v| v[1]).collect::<Vec<u64>>();
left.sort();
right.sort();
Answer::Number(
left.into_iter()
.zip(right.into_iter())
.map(|(a, b)| a.abs_diff(b))
.sum(),
)
} }
fn two(&self, data: &mut Data) -> Answer { fn two(&self, data: &mut Data) -> Answer {
Answer::Number(data.len() as u64) let mut left = data.iter().map(|v| v[0]).collect::<Vec<u64>>();
let mut right = data.iter().map(|v| v[1]).collect::<Vec<u64>>();
Answer::Number(
left.iter()
.map(|v| v * right.iter().filter(|n| *n == v).count() as u64)
.sum(),
)
} }
} }

View file

@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3