From 4a876daa71368ebb20d5f3ed8f1bdaf53bf4575e Mon Sep 17 00:00:00 2001 From: Niccolo Borgioli Date: Sun, 19 Nov 2023 16:51:29 +0100 Subject: [PATCH] refactor --- 2022/01/rust/main.rs | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/2022/01/rust/main.rs b/2022/01/rust/main.rs index 7f2c82b..af4212b 100644 --- a/2022/01/rust/main.rs +++ b/2022/01/rust/main.rs @@ -9,37 +9,45 @@ mod tests { #[bench] fn bench_a(b: &mut Bencher) { - b.iter(|| part_a()); + b.iter(|| part_a(INPUT)); } #[bench] fn bench_b(b: &mut Bencher) { - b.iter(|| part_b()); + b.iter(|| part_b(INPUT)); } } -fn part_a() { - let result: u32 = include_str!("../input.txt") +const INPUT: &str = include_str!("../input.txt"); +const TEST: &str = include_str!("../test.txt"); + +fn part_a(input: &str) { + let result: u32 = input .trim() .split("\n\n") .map(|x| x.lines().map(|x| x.parse::().unwrap()).sum()) .max() .unwrap(); - println!("A: {}", result); + println!("{}", result); } -fn part_b() { - let mut result = include_str!("../input.txt") +fn part_b(input: &str) { + let mut result = input .trim() .split("\n\n") .map(|x| x.lines().map(|x| x.parse::().unwrap()).sum::()) .collect::>(); result.sort_unstable(); let total = result.into_iter().rev().take(3).sum::(); - println!("B: {}", total); + println!("{}", total); } fn main() { - part_a(); - part_b(); + println!("Part A:"); + part_a(TEST); + part_a(INPUT); + + println!("\nPart B:"); + part_b(TEST); + part_b(INPUT); }