From 02044a65fb94689a71c56d96cb2179b8724f42ad Mon Sep 17 00:00:00 2001 From: traxys Date: Sun, 1 Dec 2024 09:44:59 +0100 Subject: [PATCH] Day1 --- Cargo.lock | 16 ++++++++++ Cargo.toml | 1 + src/bin/day1.rs | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 src/bin/day1.rs diff --git a/Cargo.lock b/Cargo.lock index 11f2676..c6b55b2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -74,6 +74,7 @@ dependencies = [ "clap", "color-eyre", "humantime", + "itertools", ] [[package]] @@ -190,6 +191,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" +[[package]] +name = "either" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" + [[package]] name = "eyre" version = "0.6.12" @@ -230,6 +237,15 @@ version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" +[[package]] +name = "itertools" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +dependencies = [ + "either", +] + [[package]] name = "lazy_static" version = "1.5.0" diff --git a/Cargo.toml b/Cargo.toml index 1ad6e01..2765d75 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,4 @@ bstr = "1.11.0" clap = { version = "4.5.21", features = ["derive"] } color-eyre = "0.6.3" humantime = "2.1.0" +itertools = "0.13.0" diff --git a/src/bin/day1.rs b/src/bin/day1.rs new file mode 100644 index 0000000..d61321d --- /dev/null +++ b/src/bin/day1.rs @@ -0,0 +1,78 @@ +use std::{collections::HashMap, time::Instant}; + +use aoc_2024::{load, print_res}; +use bstr::BString; +use color_eyre::eyre::{Context, ContextCompat}; +use itertools::Itertools; + +type Parsed = (Vec, Vec); + +pub fn parsing(input: &BString) -> color_eyre::Result { + let (first, second) = core::str::from_utf8(input) + .unwrap() + .lines() + .map(|l| -> color_eyre::Result<_> { + let mut nums = l.split_ascii_whitespace(); + let first: u64 = nums + .next() + .with_context(|| "missing first number")? + .parse() + .with_context(|| "parsing first number")?; + let second: u64 = nums + .next() + .with_context(|| "missing second number")? + .parse() + .with_context(|| "parsing second number")?; + + Ok((first, second)) + }) + .process_results(|iter| iter.unzip())?; + Ok((first, second)) +} + +pub fn part1((mut first, mut second): Parsed) { + first.sort_unstable(); + second.sort_unstable(); + + let total_distance: u64 = first.iter().zip(second).map(|(a, b)| a.abs_diff(b)).sum(); + print_res!("Total distance: {total_distance}"); +} + +pub fn part2((left, right): Parsed) { + let mut similarities = HashMap::new(); + let mut total_similarity = 0; + + for n in left { + match similarities.get(&n) { + None => { + let similarity = n * right.iter().filter(|&&x| x == n).count() as u64; + similarities.insert(n, similarity); + total_similarity += similarity; + } + Some(v) => total_similarity += v, + } + } + + print_res!("Total similarity: {total_similarity}"); +} + +pub fn main() -> color_eyre::Result<()> { + let context = load()?; + + let start = Instant::now(); + let parsed = parsing(&context.input)?; + let elapsed = humantime::format_duration(start.elapsed()); + + let start = Instant::now(); + if context.part == 1 { + part1(parsed); + } else { + part2(parsed); + } + let elapsed_part = humantime::format_duration(start.elapsed()); + + println!(" Parsing: {elapsed}"); + println!(" Solving: {elapsed_part}"); + + Ok(()) +}