From c5671587d5f6607d31b12c48a3822cb13aea7199 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Mon, 2 Dec 2024 10:17:49 -0600 Subject: [PATCH] Cleaner --- 2024/rust/src/day1.rs | 25 +++++++++++-------------- 2024/rust/src/prelude.rs | 19 +++++++++++-------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/2024/rust/src/day1.rs b/2024/rust/src/day1.rs index fa244fa..98ed71c 100644 --- a/2024/rust/src/day1.rs +++ b/2024/rust/src/day1.rs @@ -1,10 +1,9 @@ mod prelude; pub use crate::prelude::*; -fn main() -> EmptyResult { - let mut note: HistorianNote = day_input(1).parse()?; +fn main() { + let mut note: HistorianNote = day_input(1).parse().unwrap(); show_answers(note.smallests_distances(), note.similarity_score()); - Ok(()) } struct HistorianNote { @@ -12,16 +11,15 @@ struct HistorianNote { right: Vec, } -impl std::str::FromStr for HistorianNote { - type Err = std::num::ParseIntError; - +impl FromStr for HistorianNote { + type Err = ParseIntError; fn from_str(s: &str) -> Result { + let mut line_tokens = s.lines().map(|l| l.split_once(" ")); let mut left = Vec::::new(); let mut right = Vec::::new(); - for l in s.lines() { - let mut tokens = l.split(" "); - left.push(tokens.next().unwrap_or("").parse()?); - right.push(tokens.next().unwrap_or("").parse()?); + while let Some((l, r)) = line_tokens.next().flatten() { + left.push(l.parse()?); + right.push(r.parse()?); } Ok(Self { left, right }) } @@ -31,7 +29,7 @@ impl HistorianNote { fn smallests_distances(&mut self) -> i64 { self.left.sort(); self.right.sort(); - std::iter::zip(&self.left, &self.right).fold(0, |sum, (l, r)| sum + (l - r).abs()) + zip(&self.left, &self.right).fold(0, |sum, (l, r)| sum + (l - r).abs()) } fn similarity_score(&self) -> i64 { @@ -50,10 +48,9 @@ mod tests { use super::*; #[test] - fn test() -> EmptyResult { - let mut note: HistorianNote = "3 4\n4 3\n2 5\n1 3\n3 9\n3 3".parse()?; + fn test() { + let mut note: HistorianNote = "3 4\n4 3\n2 5\n1 3\n3 9\n3 3".parse().unwrap(); assert_eq!(note.smallests_distances(), 11); assert_eq!(note.similarity_score(), 31); - Ok(()) } } diff --git a/2024/rust/src/prelude.rs b/2024/rust/src/prelude.rs index b31ef9e..53521ba 100644 --- a/2024/rust/src/prelude.rs +++ b/2024/rust/src/prelude.rs @@ -1,9 +1,12 @@ -pub use std::collections::HashMap; -pub use std::fs::File; -pub use std::io::Read; - use std::path::{Path, PathBuf}; -use std::{env, io}; +pub use std::{ + collections::HashMap, + fs::File, + io::Read, + iter::zip, + num::{ParseFloatError, ParseIntError}, + str::FromStr, +}; pub type AnyResult = std::result::Result>; pub type EmptyResult = AnyResult<()>; @@ -11,15 +14,15 @@ pub type EmptyResult = AnyResult<()>; #[derive(Debug)] enum InputFileError { #[allow(dead_code)] - EnvVar(env::VarError), + EnvVar(std::env::VarError), #[allow(dead_code)] - Io(io::Error), + Io(std::io::Error), } /// Ensures that the input file exists fn ensure_input_file(day: u8) -> Result { - let path = Path::new(&env::var("HOME").map_err(InputFileError::EnvVar)?) + let path = Path::new(&std::env::var("HOME").map_err(InputFileError::EnvVar)?) .join(format!("./.cache/aoc2024/{0}.input", day)); if !path.exists() { eprintln!("Running input downloaded script with day arg {}...", day);