From cb25f1c89ca4bc588cc3136e40641717fb53e005 Mon Sep 17 00:00:00 2001 From: TriMill Date: Thu, 1 Dec 2022 00:25:40 -0500 Subject: [PATCH] day 01 --- .gitignore | 2 ++ Cargo.lock | 7 +++++++ Cargo.toml | 8 ++++++++ README.md | 2 ++ examples/day01_1.rs | 16 ++++++++++++++++ examples/day01_2.rs | 18 ++++++++++++++++++ src/lib.rs | 0 7 files changed, 53 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 examples/day01_1.rs create mode 100644 examples/day01_2.rs create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3ceb93d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +/input diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..d37200f --- /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 = "aoc2022" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..c1cd145 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "aoc2022" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/README.md b/README.md index 10cf193..4bc6fea 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ # Advent of Code 2022 +WARNING: ADVENT OF CODE SPOILERS AHEAD +solutions located in the `examples` directory. run with `cargo r --example=day##_#.rs`. input is given via stdin. diff --git a/examples/day01_1.rs b/examples/day01_1.rs new file mode 100644 index 0000000..136d232 --- /dev/null +++ b/examples/day01_1.rs @@ -0,0 +1,16 @@ +fn main() -> Result<(), Box> { + let lines = std::io::stdin().lines(); + let mut max = 0; + let mut elf = 0; + for line in lines { + let line = line?; + if line.trim().is_empty() { + max = max.max(elf); + elf = 0; + } else { + elf += line.parse::()?; + } + } + println!("{}", max); + Ok(()) +} diff --git a/examples/day01_2.rs b/examples/day01_2.rs new file mode 100644 index 0000000..101e1b6 --- /dev/null +++ b/examples/day01_2.rs @@ -0,0 +1,18 @@ +fn main() -> Result<(), Box> { + let lines = std::io::stdin().lines(); + let mut elves = Vec::new(); + let mut elf = 0; + for line in lines { + let line = line?; + if line.trim().is_empty() { + elves.push(elf); + elf = 0; + } else { + elf += line.parse::()?; + } + } + elves.sort(); + elves.reverse(); + println!("{}", elves[0] + elves[1] + elves[2]); + Ok(()) +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..e69de29