Day 1, puzzle 1

This commit is contained in:
jazzpi 2022-12-12 19:51:33 +01:00
commit 1a5b2ddfdd
4 changed files with 48 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

7
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 = "aoc23"
version = "0.1.0"

12
Cargo.toml Normal file
View File

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

28
src/d1p1.rs Normal file
View File

@ -0,0 +1,28 @@
use std::io;
fn main() {
println!("What's the calorie list?");
let lines = io::stdin().lines();
let mut was_empty = false;
let mut elves = vec![0];
for line in lines {
let line_s = line.unwrap();
if line_s.is_empty() {
if was_empty {
break;
}
elves.push(0);
was_empty = true;
continue;
} else {
was_empty = false;
}
let calories: u32 = line_s.parse().expect("Wanted a number");
let prev_calories = elves.pop().unwrap();
elves.push(prev_calories + calories);
}
let max = elves.iter().max().unwrap();
println!("The elf with the most calories is carrying {} cal", max);
}