Made everything a bit more rustic

This commit is contained in:
Rüdiger Ludwig 2023-07-29 17:02:25 +02:00
parent 7f5b6e03f9
commit a5f19ecae1
22 changed files with 376 additions and 274 deletions

View file

@ -1,5 +1,7 @@
use std::cmp::Ordering;
use crate::common::file::split_lines;
use super::template::{DayTrait, ResultType};
use itertools::Itertools;
use thiserror::Error;
@ -13,7 +15,7 @@ impl DayTrait for Day {
DAY_NUMBER
}
fn part1(&self, lines: &[String]) -> anyhow::Result<ResultType> {
fn part1(&self, lines: &str) -> anyhow::Result<ResultType> {
let packets = Packets::parse_all(lines)?;
let result: i64 = packets
.iter()
@ -30,7 +32,7 @@ impl DayTrait for Day {
Ok(ResultType::Integer(result))
}
fn part2(&self, lines: &[String]) -> anyhow::Result<ResultType> {
fn part2(&self, lines: &str) -> anyhow::Result<ResultType> {
let small = Packets::parse("[[2]]")?;
let large = Packets::parse("[[6]]")?;
let mut pos_small = 1;
@ -144,9 +146,8 @@ impl Packets {
Err(PacketError::PrematureEndOfInput)
}
pub fn parse_all(lines: &[String]) -> Result<Vec<Packets>, PacketError> {
lines
.iter()
pub fn parse_all(lines: &str) -> Result<Vec<Packets>, PacketError> {
split_lines(lines)
.filter(|line| !line.is_empty())
.map(|line| Packets::parse(line))
.collect::<Result<Vec<_>, _>>()
@ -156,7 +157,7 @@ impl Packets {
#[cfg(test)]
mod test {
use super::*;
use crate::common::file::read_lines;
use crate::common::file::read_string;
use anyhow::Result;
#[test]
@ -174,7 +175,7 @@ mod test {
#[test]
fn test_parse_all() -> 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 result = Packets::parse_all(&lines)?;
assert_eq!(result.len(), 16);
Ok(())
@ -183,7 +184,7 @@ mod test {
#[test]
fn test_compare_all() -> 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 = vec![true, true, false, true, false, true, false, false];
let result = Packets::parse_all(&lines)?;
let compare = result
@ -199,7 +200,7 @@ mod test {
#[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(13);
let result = day.part1(&lines)?;
assert_eq!(result, expected);
@ -210,7 +211,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(140);
let result = day.part2(&lines)?;
assert_eq!(result, expected);