From a6894ee3be6f552120c0f824577d676d301e5f5d Mon Sep 17 00:00:00 2001 From: Peter Date: Thu, 1 Dec 2022 14:53:14 +0800 Subject: [PATCH] day 1 part 1 & 2 --- .gitignore | 4 +++- day_1/src/main.rs | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 99f32d5..c3d6ed8 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,6 @@ Cargo.lock # MSVC Windows builds of rustc generate these, which store debugging information *.pdb -.vscode/ \ No newline at end of file +.vscode/ + +input \ No newline at end of file diff --git a/day_1/src/main.rs b/day_1/src/main.rs index e7a11a9..cc5fd9c 100644 --- a/day_1/src/main.rs +++ b/day_1/src/main.rs @@ -1,3 +1,20 @@ +use std::fs; + +const PATH: &str = "src/input"; + fn main() { - println!("Hello, world!"); + let data = fs::read_to_string(PATH).expect("Error reading file"); + let mut cals_list: Vec = Vec::new(); + let mut cals: u32 = 0; + for line in data.split("\n") { + if line.len() == 0 { + cals_list.push(cals); + cals = 0; + } else { + cals += line.parse::().unwrap(); + } + } + cals_list.sort_by(|a, b| b.cmp(a)); + println!("PART 1: {}", cals_list[0]); + println!("PART 2: {}", cals_list[0] + cals_list[1] + cals_list[2]); }