day 1 part 1 & 2

This commit is contained in:
Peter 2022-12-01 14:53:14 +08:00
parent 4e7b2ce48b
commit a6894ee3be
2 changed files with 21 additions and 2 deletions

2
.gitignore vendored
View File

@ -14,3 +14,5 @@ Cargo.lock
*.pdb
.vscode/
input

View File

@ -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<u32> = 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::<u32>().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]);
}