From 81decee6a9431b8a095f59f601aeab8d5d935b40 Mon Sep 17 00:00:00 2001 From: TuDatTr Date: Fri, 2 Dec 2022 00:58:50 +0100 Subject: [PATCH] Refactoring Signed-off-by: TuDatTr --- 2022/day-1_calorie_counting/README.md | 4 +- 2022/day-1_calorie_counting/src/bin.rs | 40 ++--------- 2022/day-1_calorie_counting/src/elf.rs | 30 ++++++++ 2022/day-1_calorie_counting/src/lib.rs | 98 +++++++++++++------------- 4 files changed, 87 insertions(+), 85 deletions(-) create mode 100644 2022/day-1_calorie_counting/src/elf.rs diff --git a/2022/day-1_calorie_counting/README.md b/2022/day-1_calorie_counting/README.md index d9e7d73..a34c99b 100644 --- a/2022/day-1_calorie_counting/README.md +++ b/2022/day-1_calorie_counting/README.md @@ -1,2 +1,4 @@ +# Calorie Counting -Usage: `cargo run --release -- --input ` diff --git a/2022/day-1_calorie_counting/src/bin.rs b/2022/day-1_calorie_counting/src/bin.rs index 3b70522..7dea839 100644 --- a/2022/day-1_calorie_counting/src/bin.rs +++ b/2022/day-1_calorie_counting/src/bin.rs @@ -1,46 +1,18 @@ -use calorie_counting_lib::*; -use std::path::PathBuf; -use std::collections::BinaryHeap; +use calorie_counting_lib::{task_1a, task_1b}; use clap::Parser; use tracing::{info, debug}; -use crate::cli::Cli; mod cli; - -/// Finds the Elf carrying the most Calories. -/// Outputs how many total Calories that Elf is carrying. -fn task_1a(input: &PathBuf) { - debug!("Running task 1a"); - let file_path = input; - let content = calorie_counting_lib::read_file(file_path).unwrap(); - let splits = content.split("\n\n"); - - println!("{}", splits.map(|s| Elf::new(s).total_calories()).max().unwrap()); -} - -/// Finds the top three Elves carrying the most Calories. -/// Outputs how many Calories those Elves are carrying in total. -fn task_1b(input: &PathBuf) { - debug!("Running task 1b"); - let file_path = input; - let content = calorie_counting_lib::read_file(file_path).unwrap(); - let splits = content.split("\n\n"); - let mut heap = splits.map(|s| Elf::new(s).total_calories()).collect::>(); - - let mut top_three = Vec::new(); - for _ in 0..3 { - if let Some(c) = heap.pop() { - top_three.push(c); - } - } - println!("{}", top_three.iter().sum::()); -} +use crate::cli::Cli; pub fn main() { tracing_subscriber::fmt::init(); - debug!("Running with debugging level logging"); + debug!("Running with DEBUG logging"); + info!("Running with INFO logging"); let args = Cli::parse(); + debug!("{:#?}", args); + task_1a(&args.input); task_1b(&args.input); } diff --git a/2022/day-1_calorie_counting/src/elf.rs b/2022/day-1_calorie_counting/src/elf.rs new file mode 100644 index 0000000..eb24a4a --- /dev/null +++ b/2022/day-1_calorie_counting/src/elf.rs @@ -0,0 +1,30 @@ +use std::fmt; + +#[derive(Debug, Clone)] +pub struct Elf { + _input: String, + calories: Vec, +} + +impl Elf { + pub fn new(input: &str) -> Self { + let cal: Vec = input + .split('\n') + .map(|i| i.parse::().unwrap()) + .collect(); + Self { + _input: input.to_string(), + calories: cal, + } + } + + pub fn total_calories(self) -> u64 { + self.calories.iter().sum() + } +} + +impl fmt::Display for Elf { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Elf: {:?}", self.calories) + } +} diff --git a/2022/day-1_calorie_counting/src/lib.rs b/2022/day-1_calorie_counting/src/lib.rs index b540a04..f89c5cd 100644 --- a/2022/day-1_calorie_counting/src/lib.rs +++ b/2022/day-1_calorie_counting/src/lib.rs @@ -1,52 +1,64 @@ use std::fs::File; -use std::io::{Write, Read}; -use std::path::Path; -use std::fmt; -use tracing::{info, debug}; +use std::io::{Read, Write}; +use std::path::{Path, PathBuf}; +use std::collections::BinaryHeap; +use tracing::{debug, info}; + +mod elf; +use crate::elf::Elf; + +#[allow(dead_code)] +fn create_file(path: &Path, content: String) { + info!("Creating file"); + debug!("Writing file from: {:?}\nWith:\n{}", path, content); -pub fn create_file(path: &Path, content: String) { - debug!("Creating file"); let mut file = File::create(path).unwrap(); let _ = file.write_all(content.as_bytes()); } -pub fn read_file(path: &Path) -> std::io::Result { - debug!("Reading file"); +#[allow(dead_code)] +fn read_file(path: &Path) -> std::io::Result { + info!("Reading file"); + debug!("Reading file from: {:?}", path); + let mut file = File::open(path)?; let mut contents = String::new(); let _ = file.read_to_string(&mut contents)?; Ok(contents.trim().to_string()) } -#[derive(Debug, Clone)] -pub struct Elf { - _input: String, - calories: Vec, +/// Finds the Elf carrying the most Calories. +/// Outputs how many total Calories that Elf is carrying. +#[allow(dead_code)] +pub fn task_1a(input: &PathBuf) { + debug!("Running task 1a"); + info!("Running task 1a with {:?}", input); + + let content = read_file(input).unwrap(); + let splits = content.split("\n\n"); + println!("Result (1a): {}", splits.map(|s| Elf::new(s).total_calories()).max().unwrap()); } -impl Elf { - pub fn new (input: &str) -> Self { - let cal: Vec = input.split('\n') - .map(|i| i.parse::().unwrap()) - .collect(); - Self { _input: input.to_string(), calories: cal } - } +/// Finds the top three Elves carrying the most Calories. +/// Outputs how many Calories those Elves are carrying in total. +#[allow(dead_code)] +pub fn task_1b(input: &PathBuf) { + debug!("Running task 1b"); + info!("Running task 1b with {:?}", input); + + let content = read_file(input).unwrap(); + let splits = content.split("\n\n"); + let mut heap = splits.map(|s| Elf::new(s).total_calories()).collect::>(); - pub fn total_calories(self) -> u64 { - self.calories.iter().sum() + let mut sum = 0u64; + for _ in 0..3 { + if let Some(c) = heap.pop() { + sum += c; + } } + println!("Result (1b): {}", sum); } -impl fmt::Display for Elf{ - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Elf: {:?}", self.calories) - } -} - -// pub fn get_calories() -> std::io::Result> { -// let content = read_file(Path::new("input.txt"))?; -// } - #[cfg(test)] mod tests { use super::*; @@ -54,7 +66,8 @@ mod tests { use std::str::from_utf8; const PATH: &str = "input.txt"; - const CONTENT: &[u8; 55] = b"1000\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000\n"; + const CONTENT: &[u8; 55] = + b"1000\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000\n"; fn create_test_setup() { let file_path = Path::new(PATH); @@ -63,29 +76,14 @@ mod tests { } fn cleanup_test_setup() { - let _ = remove_file(&Path::new(PATH)); + let _ = remove_file(Path::new(PATH)); } /// Test if we corretly read file input. #[test] fn file_io() { create_test_setup(); - let content = read_file(&Path::new(PATH)); - cleanup_test_setup(); - assert_eq!(from_utf8(CONTENT).unwrap(), content.unwrap().to_string()); - } - - /// Test if the calculation of calories each elf is carrying is correct. - #[test] - fn elf_calories() { - create_test_setup(); - let content = read_file(&Path::new(PATH)).unwrap(); - let file_path = Path::new("input.txt"); - let content = calorie_counting_lib::read_file(file_path).unwrap(); - let splits = content.split("\n\n"); - let result_vec = Vec::::new(); - for elf in splits.map(|s| Elf::new(s)) { - result_vec.push(elf.total_calories()); - } + let content = read_file(Path::new(PATH)); cleanup_test_setup(); + assert_eq!(from_utf8(CONTENT).unwrap(), content.unwrap()); } }