diff --git a/2022/.gitignore b/2022/.gitignore new file mode 100644 index 0000000..ca9ec71 --- /dev/null +++ b/2022/.gitignore @@ -0,0 +1,2 @@ +/input +/target diff --git a/2022/Cargo.lock b/2022/Cargo.lock new file mode 100644 index 0000000..91987e2 --- /dev/null +++ b/2022/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "anyhow" +version = "1.0.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "216261ddc8289130e551ddcd5ce8a064710c0d064a4d2895c67151c92b5443f6" + +[[package]] +name = "aoc2022" +version = "0.1.0" +dependencies = [ + "anyhow", +] diff --git a/2022/Cargo.toml b/2022/Cargo.toml new file mode 100644 index 0000000..94bd1a2 --- /dev/null +++ b/2022/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "aoc2022" +version = "0.1.0" +edition = "2021" + +[dependencies] +anyhow = "1.0.66" diff --git a/2022/src/bin/day01.rs b/2022/src/bin/day01.rs new file mode 100644 index 0000000..df8cacf --- /dev/null +++ b/2022/src/bin/day01.rs @@ -0,0 +1,53 @@ +const INPUT: &str = include_str!("../../input/day01.txt"); + +fn main() { + println!("{}", part1(INPUT).unwrap()); + println!("{}", part2(INPUT).unwrap()); +} + +#[derive(Debug, Clone, Default)] +struct Elf { + fruits: Vec, +} + +impl Elf { + fn total(&self) -> u32 { + self.fruits.iter().sum() + } +} + +fn part1(input: &str) -> anyhow::Result { + let mut elves: Vec = vec![]; + + for section in input.split("\n\n") { + let mut elf = Elf::default(); + + for fruit in section.split('\n').filter(|line| !line.is_empty()) { + let cals: u32 = fruit.parse()?; + elf.fruits.push(cals); + } + + elves.push(elf); + } + + let most_food = elves.iter().cloned().max_by_key(Elf::total).unwrap(); + Ok(most_food.total()) +} + +fn part2(input: &str) -> anyhow::Result { + let mut elves: Vec = vec![]; + + for section in input.split("\n\n") { + let mut elf = Elf::default(); + + for fruit in section.split('\n').filter(|line| !line.is_empty()) { + let cals: u32 = fruit.parse()?; + elf.fruits.push(cals); + } + + elves.push(elf); + } + + elves[..].sort_by_key(|e| std::cmp::Reverse(e.total())); + Ok(elves[0..3].iter().map(Elf::total).sum()) +} diff --git a/README.md b/README.md index 8c31bae..13d16ff 100644 --- a/README.md +++ b/README.md @@ -3,12 +3,13 @@ I like solving the [Advent of Code](https://adventofcode.com/) challenges every - 2015: TODO - 2016: TODO - 2017: Python -- 2018: Rust +- 2018: Rust (v1.30, 2018 edition) - 2019: Ruby - 2020: Elixir - 2021: Go +- 2022: Rust (Again! v1.65, 2021 edition) -Languages I'd like to use for the TODO years: +Languages I'd like to use for the future or TODO years: - J (or other APL-like) - Haskell - Zig