From 49c441a277a52ab4faa12e00f01f64bb4025ed58 Mon Sep 17 00:00:00 2001 From: LeMoonStar Date: Sun, 1 Dec 2024 06:37:02 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Day=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- README.md | 4 ++-- src/days/d01.rs | 34 ++++++++++++++++++++++++++++----- src/days/test_inputs/test01.txt | 6 ++++++ 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index a8a44e6..ea6a73a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ /target -.aoc23_cache \ No newline at end of file +.aoc24_cache \ No newline at end of file diff --git a/README.md b/README.md index d321dcf..e2511d5 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,8 @@ [![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)) [![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) -![Stars](https://img.shields.io/badge/Stars-0-yellow?style=flat-square) +![Days completed](https://img.shields.io/badge/Days%20completed-1-red?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. diff --git a/src/days/d01.rs b/src/days/d01.rs index f857a01..0923bd9 100644 --- a/src/days/d01.rs +++ b/src/days/d01.rs @@ -2,7 +2,7 @@ use super::{Answer, Day, DayImpl}; const CURRENT_DAY: u8 = 1; -type Data = Vec; +type Data = Vec<[u64; 2]>; impl DayImpl for Day { fn init_test() -> (Self, Data) { Self::init(include_str!("test_inputs/test01.txt")) @@ -17,16 +17,40 @@ impl DayImpl for Day { Self {}, input .lines() - .map(|v| v.parse::().expect("error while parsing input.")) - .collect(), + .map(|v| { + v.split_whitespace() + .map(|v| v.trim().parse::().expect("Error during parsing")) + .collect::>() + .try_into() + .expect("Error during parsing") + }) + .collect::>(), ) } fn one(&self, data: &mut Data) -> Answer { - Answer::Number(data.len() as u64) + let mut left = data.iter().map(|v| v[0]).collect::>(); + let mut right = data.iter().map(|v| v[1]).collect::>(); + + 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 { - Answer::Number(data.len() as u64) + let mut left = data.iter().map(|v| v[0]).collect::>(); + let mut right = data.iter().map(|v| v[1]).collect::>(); + + Answer::Number( + left.iter() + .map(|v| v * right.iter().filter(|n| *n == v).count() as u64) + .sum(), + ) } } diff --git a/src/days/test_inputs/test01.txt b/src/days/test_inputs/test01.txt index e69de29..dfca0b1 100644 --- a/src/days/test_inputs/test01.txt +++ b/src/days/test_inputs/test01.txt @@ -0,0 +1,6 @@ +3 4 +4 3 +2 5 +1 3 +3 9 +3 3 \ No newline at end of file