Made everything a bit more rustic
This commit is contained in:
parent
7f5b6e03f9
commit
a5f19ecae1
22 changed files with 376 additions and 274 deletions
|
|
@ -3,6 +3,8 @@ use std::num::ParseIntError;
|
|||
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::common::file::split_lines;
|
||||
|
||||
use super::template::{DayTrait, ResultType};
|
||||
|
||||
pub struct Day;
|
||||
|
|
@ -13,13 +15,15 @@ impl DayTrait for Day {
|
|||
DAY_NUMBER
|
||||
}
|
||||
|
||||
fn part1(&self, lines: &[String]) -> anyhow::Result<ResultType> {
|
||||
fn part1(&self, lines: &str) -> anyhow::Result<ResultType> {
|
||||
let lines = split_lines(lines);
|
||||
let vector = Day::parse(lines)?;
|
||||
let max = vector.iter().max().ok_or(CalorieError::Empty)?;
|
||||
Ok(ResultType::Integer(*max))
|
||||
}
|
||||
|
||||
fn part2(&self, lines: &[String]) -> anyhow::Result<ResultType> {
|
||||
fn part2(&self, lines: &str) -> anyhow::Result<ResultType> {
|
||||
let lines = split_lines(lines);
|
||||
let vector = Day::parse(lines)?;
|
||||
let sum = vector.iter().sorted_by(|a, b| Ord::cmp(b, a)).take(3).sum();
|
||||
Ok(ResultType::Integer(sum))
|
||||
|
|
@ -27,9 +31,8 @@ impl DayTrait for Day {
|
|||
}
|
||||
|
||||
impl Day {
|
||||
fn parse(lines: &[String]) -> Result<Vec<i64>, CalorieError> {
|
||||
fn parse<'a>(lines: impl Iterator<Item = &'a str>) -> Result<Vec<i64>, CalorieError> {
|
||||
Ok(lines
|
||||
.iter()
|
||||
.batching(|it| {
|
||||
let result = it
|
||||
.take_while(|line| !line.is_empty())
|
||||
|
|
@ -57,13 +60,13 @@ pub enum CalorieError {
|
|||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::common::file::read_lines;
|
||||
use crate::common::file::read_string;
|
||||
use anyhow::Result;
|
||||
|
||||
#[test]
|
||||
fn test_part1() -> Result<()> {
|
||||
let day = Day {};
|
||||
let lines = read_lines(day.get_day_number(), "example01.txt")?;
|
||||
let lines = read_string(day.get_day_number(), "example01.txt")?;
|
||||
let expected = ResultType::Integer(24_000);
|
||||
let result = day.part1(&lines)?;
|
||||
assert_eq!(result, expected);
|
||||
|
|
@ -74,7 +77,7 @@ mod test {
|
|||
#[test]
|
||||
fn test_part2() -> Result<()> {
|
||||
let day = Day {};
|
||||
let lines = read_lines(day.get_day_number(), "example01.txt")?;
|
||||
let lines = read_string(day.get_day_number(), "example01.txt")?;
|
||||
let expected = ResultType::Integer(45_000);
|
||||
let result = day.part2(&lines)?;
|
||||
assert_eq!(result, expected);
|
||||
|
|
@ -84,9 +87,10 @@ mod test {
|
|||
|
||||
#[test]
|
||||
fn test_parse() -> Result<()> {
|
||||
let lines = read_lines(DAY_NUMBER, "example01.txt")?;
|
||||
let lines = read_string(DAY_NUMBER, "example01.txt")?;
|
||||
let lines = split_lines(&lines);
|
||||
let expected = vec![6000, 4000, 11000, 24000, 10000];
|
||||
let result = Day::parse(&lines)?;
|
||||
let result = Day::parse(lines)?;
|
||||
assert_eq!(result, expected);
|
||||
|
||||
Ok(())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue