2022 Day 1
This commit is contained in:
parent
f0c4966624
commit
4dfa87c796
5 changed files with 81 additions and 2 deletions
2
2022/.gitignore
vendored
Normal file
2
2022/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
/input
|
||||
/target
|
||||
16
2022/Cargo.lock
generated
Normal file
16
2022/Cargo.lock
generated
Normal file
|
|
@ -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",
|
||||
]
|
||||
7
2022/Cargo.toml
Normal file
7
2022/Cargo.toml
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
[package]
|
||||
name = "aoc2022"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.66"
|
||||
53
2022/src/bin/day01.rs
Normal file
53
2022/src/bin/day01.rs
Normal file
|
|
@ -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<u32>,
|
||||
}
|
||||
|
||||
impl Elf {
|
||||
fn total(&self) -> u32 {
|
||||
self.fruits.iter().sum()
|
||||
}
|
||||
}
|
||||
|
||||
fn part1(input: &str) -> anyhow::Result<u32> {
|
||||
let mut elves: Vec<Elf> = 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<u32> {
|
||||
let mut elves: Vec<Elf> = 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())
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue