mirror of
https://gitlab.com/MisterBiggs/aoc_2022-rust.git
synced 2025-06-16 15:17:15 +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]]
|
[[package]]
|
||||||
name = "aoc_2022"
|
name = "aoc_2022"
|
||||||
version = "0.1.0"
|
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
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
itertools = "0.10.5"
|
||||||
|
16
src/day1.rs
16
src/day1.rs
@ -1,3 +1,4 @@
|
|||||||
|
use itertools::Itertools;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
pub fn run() {
|
pub fn run() {
|
||||||
@ -11,33 +12,30 @@ pub fn run() {
|
|||||||
fn part1(food_input: &str) -> usize {
|
fn part1(food_input: &str) -> usize {
|
||||||
food_input
|
food_input
|
||||||
.split("\n\n")
|
.split("\n\n")
|
||||||
.collect::<Vec<&str>>()
|
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|elf| {
|
.map(|elf| {
|
||||||
elf.split_whitespace()
|
elf.split_whitespace()
|
||||||
.map(|food| food.parse::<usize>().unwrap())
|
.map(|food| food.parse::<usize>().unwrap())
|
||||||
.sum()
|
.sum()
|
||||||
})
|
})
|
||||||
.collect::<Vec<usize>>()
|
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.max()
|
.max()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
fn part2(food_input: &str) -> usize {
|
fn part2(food_input: &str) -> usize {
|
||||||
let mut elves_calories = food_input
|
food_input
|
||||||
.split("\n\n")
|
.split("\n\n")
|
||||||
.collect::<Vec<&str>>()
|
.collect::<Vec<&str>>()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|elf| {
|
.map(|elf| {
|
||||||
elf.split_whitespace()
|
elf.split_whitespace()
|
||||||
.map(|food| food.parse::<usize>().unwrap())
|
.map(|food| food.parse::<usize>().unwrap())
|
||||||
.sum()
|
.sum::<usize>()
|
||||||
})
|
})
|
||||||
.collect::<Vec<usize>>();
|
.sorted()
|
||||||
|
.rev()
|
||||||
elves_calories.sort_by(|l, r| r.cmp(l));
|
.take(3)
|
||||||
|
.sum()
|
||||||
elves_calories[..3].iter().sum::<usize>()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user