mirror of
https://gitlab.com/MisterBiggs/aoc-2023-rust.git
synced 2025-06-15 14:36:50 +00:00
cleaned up code
This commit is contained in:
parent
a12c497551
commit
6fe29ebc99
18
Cargo.lock
generated
18
Cargo.lock
generated
@ -5,3 +5,21 @@ version = 3
|
||||
[[package]]
|
||||
name = "aoc_2022"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"itertools",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.10.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
|
@ -6,3 +6,4 @@ edition = "2021"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.10.5"
|
||||
|
16
src/day1.rs
16
src/day1.rs
@ -1,3 +1,4 @@
|
||||
use itertools::Itertools;
|
||||
use std::fs;
|
||||
|
||||
pub fn run() {
|
||||
@ -11,33 +12,30 @@ pub fn run() {
|
||||
fn part1(food_input: &str) -> usize {
|
||||
food_input
|
||||
.split("\n\n")
|
||||
.collect::<Vec<&str>>()
|
||||
.into_iter()
|
||||
.map(|elf| {
|
||||
elf.split_whitespace()
|
||||
.map(|food| food.parse::<usize>().unwrap())
|
||||
.sum()
|
||||
})
|
||||
.collect::<Vec<usize>>()
|
||||
.into_iter()
|
||||
.max()
|
||||
.unwrap()
|
||||
}
|
||||
fn part2(food_input: &str) -> usize {
|
||||
let mut elves_calories = food_input
|
||||
food_input
|
||||
.split("\n\n")
|
||||
.collect::<Vec<&str>>()
|
||||
.into_iter()
|
||||
.map(|elf| {
|
||||
elf.split_whitespace()
|
||||
.map(|food| food.parse::<usize>().unwrap())
|
||||
.sum()
|
||||
.sum::<usize>()
|
||||
})
|
||||
.collect::<Vec<usize>>();
|
||||
|
||||
elves_calories.sort_by(|l, r| r.cmp(l));
|
||||
|
||||
elves_calories[..3].iter().sum::<usize>()
|
||||
.sorted()
|
||||
.rev()
|
||||
.take(3)
|
||||
.sum()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
Loading…
x
Reference in New Issue
Block a user