Add leading 0s to the single digit day crates

This commit is contained in:
Eryn Wells 2022-12-17 19:56:03 -08:00
parent a81545a012
commit 51d2ee871c
33 changed files with 0 additions and 0 deletions

7
2022/day01/Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day1"
version = "0.1.0"

8
2022/day01/Cargo.toml Normal file
View file

@ -0,0 +1,8 @@
[package]
name = "day1"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

2234
2022/day01/input.txt Normal file

File diff suppressed because it is too large Load diff

44
2022/day01/src/main.rs Normal file
View file

@ -0,0 +1,44 @@
use std::env;
use std::fs;
use std::str::Lines;
fn get_calorie_totals(lines: Lines) -> Vec<u32> {
let mut elves: Vec<u32> = Vec::new();
let mut current_calorie_count: u32 = 0;
for line in lines {
if line.is_empty() {
elves.push(current_calorie_count);
current_calorie_count = 0;
continue;
}
let u32_value = u32::from_str_radix(&line, 10)
.expect(format!("Couldn't read u32 value from string: {}", line).as_str());
current_calorie_count += u32_value;
}
elves.sort_unstable_by(|a, b| b.cmp(a));
elves
}
fn main() {
let args: Vec<String> = env::args().collect();
let filename = args.get(1).expect("Missing filename argument");
let file_contents = fs::read_to_string(&filename).expect("Unable to read file");
let lines = file_contents.lines();
let elves = get_calorie_totals(lines);
println!(
"Part 1: Elf with highest calorie count in knapsack: {}",
elves[0]
);
let sum_of_top_three = &elves[0] + &elves[1] + &elves[2];
println!(
"Part 2: Elves with top 3 highest calorie counts in their knapsacks: {}, {}, {} = {}",
&elves[0], &elves[1], &elves[2], sum_of_top_three
);
}