From c51d28eb75528c8a48b0b3f73fca0ae51031eac3 Mon Sep 17 00:00:00 2001 From: StNicolay Date: Sun, 1 Dec 2024 15:10:20 +0300 Subject: [PATCH] Day 1 --- .gitignore | 2 ++ Cargo.lock | 7 +++++++ Cargo.toml | 13 +++++++++++++ src/main.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b08fc6c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +input.txt \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..aa37fc3 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "testing" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..cd969b7 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "testing" +version = "0.1.0" +edition = "2021" + +[profile.release] +debug = 1 + +[lints.clippy] +pedantic = "warn" +all = "warn" + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..5c85b13 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,42 @@ +use std::{ + collections::HashMap, + fs::File, + io::{BufRead, BufReader}, +}; + +#[allow(dead_code)] +fn day1_1() { + let reader = BufReader::new(File::open("input.txt").unwrap()); + let mut a = Vec::::new(); + let mut b = Vec::::new(); + for line in reader.lines() { + let line = line.unwrap(); + let (a1, b1) = line.trim_end().split_once(" ").unwrap(); + a.push(a1.parse().unwrap()); + b.push(b1.parse().unwrap()); + } + a.sort_unstable(); + b.sort_unstable(); + let result: i32 = a.into_iter().zip(b).map(|(a, b)| (a - b).abs()).sum(); + println!("{result}"); +} + +#[allow(dead_code)] +fn day1_2() { + let reader = BufReader::new(File::open("input.txt").unwrap()); + let mut a = Vec::::new(); + let mut b = HashMap::::new(); + for line in reader.lines() { + let line = line.unwrap(); + let (a1, b1) = line.trim_end().split_once(" ").unwrap(); + a.push(a1.parse().unwrap()); + *b.entry(b1.parse().unwrap()).or_insert(0) += 1; + } + let result: u32 = a.into_iter().map(|a| a * *b.get(&a).unwrap_or(&0)).sum(); + println!("{result}"); +} + +fn main() { + day1_1(); + day1_2(); +}