From 93f92a3650a046baa018a06b15f2dd9d043a2c3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emma=20B=C3=B6cker?= Date: Fri, 1 Dec 2023 07:04:25 +0100 Subject: [PATCH] rust fmt --- src/bin/create-day.rs | 11 +++++---- src/bin/day01.rs | 56 +++++++++++++++++++++++-------------------- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/src/bin/create-day.rs b/src/bin/create-day.rs index 18b612b..553df4a 100644 --- a/src/bin/create-day.rs +++ b/src/bin/create-day.rs @@ -12,10 +12,13 @@ macro_rules! create_and_write { fn main() -> Result<(), Box> { // Read command line argument for the day and parse it into a number leading zeros - let day = format!("{:02}", std::env::args() - .nth(1) - .ok_or("Missing arguments")? - .parse::()?); + let day = format!( + "{:02}", + std::env::args() + .nth(1) + .ok_or("Missing arguments")? + .parse::()? + ); // Create the file if it doesn't exist create_and_write!("input/day{day}.txt", "")?; diff --git a/src/bin/day01.rs b/src/bin/day01.rs index c123609..5ab76ff 100644 --- a/src/bin/day01.rs +++ b/src/bin/day01.rs @@ -1,42 +1,43 @@ use std::collections::HashMap; fn main() { - let mut input = include_str!("../../input/day01.txt").to_owned(); + let input = include_str!("../../input/day01.txt"); - // println!("{}", find_numbers(&input)); + println!("Part 1: {}", find_numbers(input, false)); - println!("{}", find_numbers(&input, true)); + println!("Part 2: {}", find_numbers(input, true)); } fn find_numbers(input: &str, written: bool) -> u32 { - let output = input.lines().map(|line| { - let line = line.to_owned(); - println!("{}", line); + let output = input + .lines() + .map(|line| { + let valid = find_valid(line, written); + format!("{}{}", valid.first().unwrap(), valid.last().unwrap()) + }) + .collect::>(); - let valid = find_valid(&line, written); - - println!("{:?}", valid); - - format!("{}{}", valid.first().unwrap(), valid.last().unwrap()) - }).collect::>(); - - output.iter().map(|string| string.parse::().unwrap()).sum::() + output + .iter() + .map(|string| string.parse::().unwrap()) + .sum::() } fn find_valid(input: &str, written: bool) -> Vec { let mut valid = vec![]; if written { - let mut written_numbers = HashMap::new(); - written_numbers.insert("one", 1); - written_numbers.insert("two", 2); - written_numbers.insert("three", 3); - written_numbers.insert("four", 4); - written_numbers.insert("five", 5); - written_numbers.insert("six", 6); - written_numbers.insert("seven", 7); - written_numbers.insert("eight", 8); - written_numbers.insert("nine", 9); + let written_numbers = HashMap::from([ + ("one", 1), + ("two", 2), + ("three", 3), + ("four", 4), + ("five", 5), + ("six", 6), + ("seven", 7), + ("eight", 8), + ("nine", 9), + ]); for (written, value) in written_numbers { let indices = input.match_indices(written); @@ -57,5 +58,8 @@ fn find_valid(input: &str, written: bool) -> Vec { } valid.sort_by(|a, b| a.0.cmp(&b.0)); - valid.into_iter().map(|(_, value)| value).collect::>() -} \ No newline at end of file + valid + .into_iter() + .map(|(_, value)| value) + .collect::>() +}