day04 finished

This commit is contained in:
Ruediger Ludwig 2023-01-29 15:12:26 +01:00
parent d7c85a75f6
commit 33eb92e9d1
10 changed files with 1184 additions and 55 deletions

View file

@ -13,13 +13,13 @@ impl DayTrait for Day {
DAY_NUMBER
}
fn part1(&self, lines: &str) -> anyhow::Result<ResultType> {
fn part1(&self, lines: &[String]) -> anyhow::Result<ResultType> {
let vector = Day::parse(lines)?;
let max = vector.iter().max().ok_or(CalorieError::Empty)?;
Ok(ResultType::IntResult(*max))
}
fn part2(&self, lines: &str) -> anyhow::Result<ResultType> {
fn part2(&self, lines: &[String]) -> anyhow::Result<ResultType> {
let vector = Day::parse(lines)?;
let sum = vector.iter().sorted_by(|a, b| Ord::cmp(b, a)).take(3).sum();
Ok(ResultType::IntResult(sum))
@ -27,9 +27,9 @@ impl DayTrait for Day {
}
impl Day {
fn parse(lines: &str) -> Result<Vec<i64>, CalorieError> {
fn parse(lines: &[String]) -> Result<Vec<i64>, CalorieError> {
Ok(lines
.split("\n")
.iter()
.batching(|it| {
let result = it
.take_while(|line| line.len() != 0)
@ -57,13 +57,13 @@ pub enum CalorieError {
#[cfg(test)]
mod test {
use super::*;
use crate::common::file::read_data;
use crate::common::file::read_lines;
use anyhow::Result;
#[test]
fn test_part1() -> Result<()> {
let day = Day {};
let lines = read_data(day.get_day_number(), "example01.txt")?;
let lines = read_lines(day.get_day_number(), "example01.txt")?;
let expected = ResultType::IntResult(24_000);
let result = day.part1(&lines)?;
assert_eq!(result, expected);
@ -74,7 +74,7 @@ mod test {
#[test]
fn test_part2() -> Result<()> {
let day = Day {};
let lines = read_data(day.get_day_number(), "example01.txt")?;
let lines = read_lines(day.get_day_number(), "example01.txt")?;
let expected = ResultType::IntResult(45_000);
let result = day.part2(&lines)?;
assert_eq!(result, expected);
@ -84,7 +84,7 @@ mod test {
#[test]
fn test_parse() -> Result<()> {
let lines = read_data(DAY_NUMBER, "example01.txt")?;
let lines = read_lines(DAY_NUMBER, "example01.txt")?;
let expected = vec![6000, 4000, 11000, 24000, 10000];
let result = Day::parse(&lines)?;
assert_eq!(result, expected);