Cleaner
This commit is contained in:
parent
242f285231
commit
c5671587d5
|
@ -1,10 +1,9 @@
|
||||||
mod prelude;
|
mod prelude;
|
||||||
pub use crate::prelude::*;
|
pub use crate::prelude::*;
|
||||||
|
|
||||||
fn main() -> EmptyResult {
|
fn main() {
|
||||||
let mut note: HistorianNote = day_input(1).parse()?;
|
let mut note: HistorianNote = day_input(1).parse().unwrap();
|
||||||
show_answers(note.smallests_distances(), note.similarity_score());
|
show_answers(note.smallests_distances(), note.similarity_score());
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct HistorianNote {
|
struct HistorianNote {
|
||||||
|
@ -12,16 +11,15 @@ struct HistorianNote {
|
||||||
right: Vec<i64>,
|
right: Vec<i64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::str::FromStr for HistorianNote {
|
impl FromStr for HistorianNote {
|
||||||
type Err = std::num::ParseIntError;
|
type Err = ParseIntError;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
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 left = Vec::<i64>::new();
|
||||||
let mut right = Vec::<i64>::new();
|
let mut right = Vec::<i64>::new();
|
||||||
for l in s.lines() {
|
while let Some((l, r)) = line_tokens.next().flatten() {
|
||||||
let mut tokens = l.split(" ");
|
left.push(l.parse()?);
|
||||||
left.push(tokens.next().unwrap_or("").parse()?);
|
right.push(r.parse()?);
|
||||||
right.push(tokens.next().unwrap_or("").parse()?);
|
|
||||||
}
|
}
|
||||||
Ok(Self { left, right })
|
Ok(Self { left, right })
|
||||||
}
|
}
|
||||||
|
@ -31,7 +29,7 @@ impl HistorianNote {
|
||||||
fn smallests_distances(&mut self) -> i64 {
|
fn smallests_distances(&mut self) -> i64 {
|
||||||
self.left.sort();
|
self.left.sort();
|
||||||
self.right.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 {
|
fn similarity_score(&self) -> i64 {
|
||||||
|
@ -50,10 +48,9 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test() -> EmptyResult {
|
fn test() {
|
||||||
let mut note: HistorianNote = "3 4\n4 3\n2 5\n1 3\n3 9\n3 3".parse()?;
|
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.smallests_distances(), 11);
|
||||||
assert_eq!(note.similarity_score(), 31);
|
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::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 AnyResult<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
||||||
pub type EmptyResult = AnyResult<()>;
|
pub type EmptyResult = AnyResult<()>;
|
||||||
|
@ -11,15 +14,15 @@ pub type EmptyResult = AnyResult<()>;
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum InputFileError {
|
enum InputFileError {
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
EnvVar(env::VarError),
|
EnvVar(std::env::VarError),
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
Io(io::Error),
|
Io(std::io::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ensures that the input file exists
|
/// Ensures that the input file exists
|
||||||
fn ensure_input_file(day: u8) -> Result<PathBuf, InputFileError> {
|
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));
|
.join(format!("./.cache/aoc2024/{0}.input", day));
|
||||||
if !path.exists() {
|
if !path.exists() {
|
||||||
eprintln!("Running input downloaded script with day arg {}...", day);
|
eprintln!("Running input downloaded script with day arg {}...", day);
|
||||||
|
|
Loading…
Reference in a new issue