Copy from github.com with solutions for days 1+2
This commit is contained in:
8
2022/day01-part1/Cargo.toml
Normal file
8
2022/day01-part1/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "day01-part1"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
36
2022/day01-part1/src/main.rs
Normal file
36
2022/day01-part1/src/main.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
fn main() {
|
||||
// Use command line arguments to specify the input filename.
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
if args.len() < 2 {
|
||||
panic!("Usage: ./main <input-file>\nNo input file provided. Exiting.");
|
||||
}
|
||||
|
||||
// Next, read the contents of the input file into a string for easier processing.
|
||||
let input = std::fs::read_to_string(&args[1]).expect("Error opening file");
|
||||
// Line-by-line processing is easiest.
|
||||
let mut input = input.lines();
|
||||
|
||||
// --- TASK BEGIN ---
|
||||
|
||||
// Keep track of the largest calorie-count and the "current" calorie-count.
|
||||
let mut max_cals = 0u32;
|
||||
let mut cals = 0u32;
|
||||
|
||||
// Iterate line-by-line.
|
||||
while let Some(line) = input.next() {
|
||||
// println!("Line is: {line}");
|
||||
match line.parse::<u32>() {
|
||||
Ok(num) => cals += num,
|
||||
Err(_) => {
|
||||
// println!("{cals}");
|
||||
max_cals = std::cmp::max(max_cals, cals);
|
||||
cals = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
// println!("{cals}");
|
||||
// Don't forget to check the very last block.
|
||||
max_cals = std::cmp::max(max_cals, cals);
|
||||
|
||||
println!("Maximum calories: {max_cals}");
|
||||
}
|
||||
Reference in New Issue
Block a user