day1 made testable
This commit is contained in:
3
Cargo.lock
generated
3
Cargo.lock
generated
@@ -5,6 +5,9 @@ version = 3
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "day1"
|
name = "day1"
|
||||||
version = "2022.0.0"
|
version = "2022.0.0"
|
||||||
|
dependencies = [
|
||||||
|
"itertools",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "day10"
|
name = "day10"
|
||||||
|
|||||||
@@ -8,3 +8,4 @@ repository.workspace = true
|
|||||||
# 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.workspace = true
|
||||||
|
|||||||
@@ -1,14 +1,25 @@
|
|||||||
#![warn(clippy::all, clippy::pedantic)]
|
#![warn(clippy::all, clippy::pedantic)]
|
||||||
|
|
||||||
use std::fs::File;
|
use std::fs;
|
||||||
use std::io::{prelude::*, BufReader};
|
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
use itertools::Itertools;
|
||||||
let file = File::open("input")?;
|
fn part1(input: &[u64]) -> String {
|
||||||
let reader = BufReader::new(file);
|
input.iter().max().unwrap().to_string()
|
||||||
|
}
|
||||||
|
|
||||||
let mut elves = reader.lines().fold(vec![0_u64], |mut acc, line| {
|
fn part2(input: &[u64]) -> String {
|
||||||
let line = line.unwrap();
|
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
|
//empty lines mean new elf
|
||||||
if line.is_empty() {
|
if line.is_empty() {
|
||||||
acc.push(0_u64);
|
acc.push(0_u64);
|
||||||
@@ -18,18 +29,49 @@ fn main() -> std::io::Result<()> {
|
|||||||
*last += line.parse::<u64>().unwrap();
|
*last += line.parse::<u64>().unwrap();
|
||||||
}
|
}
|
||||||
acc
|
acc
|
||||||
});
|
})
|
||||||
|
}
|
||||||
|
fn main() -> std::io::Result<()> {
|
||||||
|
let file = fs::read_to_string("input")?;
|
||||||
|
|
||||||
//order the elves since we don't care about position anymore
|
let elves = parse_input(&file);
|
||||||
elves.sort_by(|a, b| b.cmp(a));
|
|
||||||
let max = *elves.first().expect("faliure");
|
|
||||||
let counts = elves.iter().take(3).sum::<u64>();
|
|
||||||
|
|
||||||
//part 1 is get the max
|
//part 1 is get the max
|
||||||
println!("Part 1: {max}");
|
println!("Part 1: {}", part1(&elves));
|
||||||
|
|
||||||
//Part 2 is get the sum of the largest 3
|
//Part 2 is get the sum of the largest 3
|
||||||
println!("Part 2: {counts}");
|
println!("Part 2: {}", part2(&elves));
|
||||||
|
|
||||||
Ok(())
|
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