Cleaner
This commit is contained in:
parent
242f285231
commit
c5671587d5
|
@ -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<i64>,
|
||||
}
|
||||
|
||||
impl std::str::FromStr for HistorianNote {
|
||||
type Err = std::num::ParseIntError;
|
||||
|
||||
impl FromStr for HistorianNote {
|
||||
type Err = ParseIntError;
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let mut line_tokens = s.lines().map(|l| l.split_once(" "));
|
||||
let mut left = Vec::<i64>::new();
|
||||
let mut right = Vec::<i64>::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(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
||||
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<PathBuf, InputFileError> {
|
||||
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);
|
||||
|
|
Loading…
Reference in a new issue