From 1a5b2ddfdd7a173b66d3eb0ec5780106a5af4836 Mon Sep 17 00:00:00 2001 From: jazzpi Date: Mon, 12 Dec 2022 19:51:33 +0100 Subject: [PATCH] Day 1, puzzle 1 --- .gitignore | 1 + Cargo.lock | 7 +++++++ Cargo.toml | 12 ++++++++++++ src/d1p1.rs | 28 ++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/d1p1.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..6da112f --- /dev/null +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..1ce86a9 --- /dev/null +++ b/Cargo.toml @@ -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" diff --git a/src/d1p1.rs b/src/d1p1.rs new file mode 100644 index 0000000..c285b36 --- /dev/null +++ b/src/d1p1.rs @@ -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); +}