From 043433e935fe87e08782292e584b7fa79209e3da Mon Sep 17 00:00:00 2001 From: MelKam Date: Sat, 3 Dec 2022 10:23:22 +0000 Subject: [PATCH] refactor day_1 --- day_1/rust/src/main.rs | 44 ++++++++++++++---------------------------- 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/day_1/rust/src/main.rs b/day_1/rust/src/main.rs index 21ffe3f..a172ef8 100644 --- a/day_1/rust/src/main.rs +++ b/day_1/rust/src/main.rs @@ -3,26 +3,7 @@ mod quick_sort; use quick_sort::quick_sort; use std::fs; -// Part one -fn find_largest_calorie_size(calorie_list: &String) -> i32 { - let mut max: i32 = 0; - let mut temp_sum: i32 = 0; - for line in calorie_list.lines() { - if line == "" { - if temp_sum > max { - max = temp_sum - }; - temp_sum = 0; - continue; - } - temp_sum += line.parse::().unwrap(); - } - - return max; -} - -// Part two -fn find_three_largest_calorie_size(calorie_list: &String) -> i32 { +fn get_total_calories(calorie_list: &String) -> Vec { let mut total_calories = Vec::::new(); let mut temp_sum: i32 = 0; @@ -37,18 +18,23 @@ fn find_three_largest_calorie_size(calorie_list: &String) -> i32 { } quick_sort(&mut total_calories); - - let length = total_calories.len(); - - return total_calories[length - 3] + total_calories[length - 2] + total_calories[length - 1]; + return total_calories; } fn main() { - let data = fs::read_to_string("../input.txt").expect("Error while trying to read input file"); + let data = + fs::read_to_string("./day_1/input.txt").expect("Error while trying to read input file"); + + let total_calories = get_total_calories(&data); + let length = total_calories.len(); - let max_total = find_largest_calorie_size(&data); - let three_max_total = find_three_largest_calorie_size(&data); + let max = total_calories[length - 1]; + let three_max = [ + total_calories[length - 3], + total_calories[length - 2], + total_calories[length - 1], + ]; - println!("max total: {max_total}"); - println!("three max total: {three_max_total}"); + println!("max total: {max}"); + println!("three max total: {three_max:#?}"); }