readying 2022 to merge
This commit is contained in:
7
2022/day1/Cargo.lock
generated
Normal file
7
2022/day1/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day1"
|
||||
version = "0.1.0"
|
||||
11
2022/day1/Cargo.toml
Normal file
11
2022/day1/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "day1"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
authors.workspace = true
|
||||
repository.workspace = true
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
itertools.workspace = true
|
||||
2240
2022/day1/input
Normal file
2240
2022/day1/input
Normal file
File diff suppressed because it is too large
Load Diff
77
2022/day1/src/main.rs
Normal file
77
2022/day1/src/main.rs
Normal file
@@ -0,0 +1,77 @@
|
||||
#![warn(clippy::all, clippy::pedantic)]
|
||||
|
||||
use std::fs;
|
||||
|
||||
use itertools::Itertools;
|
||||
fn part1(input: &[u64]) -> String {
|
||||
input.iter().max().unwrap().to_string()
|
||||
}
|
||||
|
||||
fn part2(input: &[u64]) -> String {
|
||||
input
|
||||
.iter()
|
||||
//order the elves since we don't care about position anymore
|
||||
.sorted_by(|a, b| b.cmp(a))
|
||||
.take(3)
|
||||
.copied()
|
||||
.sum::<u64>()
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn parse_input(input: &str) -> Vec<u64> {
|
||||
input.lines().fold(vec![0_u64], |mut acc, line| {
|
||||
//empty lines mean new elf
|
||||
if line.is_empty() {
|
||||
acc.push(0_u64);
|
||||
} else {
|
||||
// the first time through is an edge case preventing an else here
|
||||
let last = acc.last_mut().unwrap();
|
||||
*last += line.parse::<u64>().unwrap();
|
||||
}
|
||||
acc
|
||||
})
|
||||
}
|
||||
fn main() -> std::io::Result<()> {
|
||||
let file = fs::read_to_string("input")?;
|
||||
|
||||
let elves = parse_input(&file);
|
||||
|
||||
//part 1 is get the max
|
||||
println!("Part 1: {}", part1(&elves));
|
||||
|
||||
//Part 2 is get the sum of the largest 3
|
||||
println!("Part 2: {}", part2(&elves));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
const INPUT: &str = "2000
|
||||
3000
|
||||
|
||||
4000
|
||||
|
||||
5000
|
||||
6000
|
||||
|
||||
7000
|
||||
8000
|
||||
9000
|
||||
|
||||
10000";
|
||||
|
||||
#[test]
|
||||
fn part1_works() {
|
||||
let input = parse_input(INPUT);
|
||||
assert_eq!(part1(&input), "24000")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2_works() {
|
||||
let input = parse_input(INPUT);
|
||||
assert_eq!(part2(&input), "45000")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user