From a745940ff8f2e5e9c78d3a2dad09ffa9b83b8fc9 Mon Sep 17 00:00:00 2001 From: MelKam Date: Fri, 2 Dec 2022 14:17:03 +0000 Subject: [PATCH] fix subrepo --- day_1/rust | 1 - day_1/rust/Cargo.toml | 8 ++++++ day_1/rust/src/main.rs | 54 ++++++++++++++++++++++++++++++++++++ day_1/rust/src/quick_sort.rs | 36 ++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 1 deletion(-) delete mode 160000 day_1/rust create mode 100644 day_1/rust/Cargo.toml create mode 100644 day_1/rust/src/main.rs create mode 100644 day_1/rust/src/quick_sort.rs diff --git a/day_1/rust b/day_1/rust deleted file mode 160000 index a22d230..0000000 --- a/day_1/rust +++ /dev/null @@ -1 +0,0 @@ -Subproject commit a22d2307fbe412829f3632f5b2e8f795df8ba27e diff --git a/day_1/rust/Cargo.toml b/day_1/rust/Cargo.toml new file mode 100644 index 0000000..802f515 --- /dev/null +++ b/day_1/rust/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day_1_rust" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day_1/rust/src/main.rs b/day_1/rust/src/main.rs new file mode 100644 index 0000000..21ffe3f --- /dev/null +++ b/day_1/rust/src/main.rs @@ -0,0 +1,54 @@ +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 { + let mut total_calories = Vec::::new(); + let mut temp_sum: i32 = 0; + + for line in calorie_list.lines() { + if line == "" { + total_calories.push(temp_sum); + temp_sum = 0; + continue; + } + + temp_sum += line.parse::().unwrap(); + } + + quick_sort(&mut total_calories); + + let length = total_calories.len(); + + return total_calories[length - 3] + total_calories[length - 2] + total_calories[length - 1]; +} + +fn main() { + let data = fs::read_to_string("../input.txt").expect("Error while trying to read input file"); + + let max_total = find_largest_calorie_size(&data); + let three_max_total = find_three_largest_calorie_size(&data); + + println!("max total: {max_total}"); + println!("three max total: {three_max_total}"); +} diff --git a/day_1/rust/src/quick_sort.rs b/day_1/rust/src/quick_sort.rs new file mode 100644 index 0000000..198fdef --- /dev/null +++ b/day_1/rust/src/quick_sort.rs @@ -0,0 +1,36 @@ +pub fn quick_sort(arr: &mut [T]) { + let len = arr.len(); + _quick_sort(arr, 0, (len - 1) as isize); +} + +fn _quick_sort(arr: &mut [T], low: isize, high: isize) { + if low < high { + let p = partition(arr, low, high); + _quick_sort(arr, low, p - 1); + _quick_sort(arr, p + 1, high); + } +} + +fn partition(arr: &mut [T], low: isize, high: isize) -> isize { + let pivot = high as usize; + let mut store_index = low - 1; + let mut last_index = high; + + loop { + store_index += 1; + while arr[store_index as usize] < arr[pivot] { + store_index += 1; + } + last_index -= 1; + while last_index >= 0 && arr[last_index as usize] > arr[pivot] { + last_index -= 1; + } + if store_index >= last_index { + break; + } else { + arr.swap(store_index as usize, last_index as usize); + } + } + arr.swap(store_index as usize, pivot as usize); + store_index +} \ No newline at end of file