Skip to content

Commit

Permalink
refactor day_1
Browse files Browse the repository at this point in the history
  • Loading branch information
MellKam committed Dec 3, 2022
1 parent a745940 commit 043433e
Showing 1 changed file with 15 additions and 29 deletions.
44 changes: 15 additions & 29 deletions day_1/rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<i32>().unwrap();
}

return max;
}

// Part two
fn find_three_largest_calorie_size(calorie_list: &String) -> i32 {
fn get_total_calories(calorie_list: &String) -> Vec<i32> {
let mut total_calories = Vec::<i32>::new();
let mut temp_sum: i32 = 0;

Expand All @@ -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:#?}");
}

0 comments on commit 043433e

Please sign in to comment.