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

@ -2,6 +2,8 @@ use itertools::Itertools;
use std::{collections::HashSet, num::ParseIntError};
use thiserror::Error;
use crate::common::file::split_lines;
use super::template::{DayTrait, ResultType};
const DAY_NUMBER: usize = 18;
@ -13,13 +15,13 @@ impl DayTrait for Day {
DAY_NUMBER
}
fn part1(&self, lines: &[String]) -> anyhow::Result<ResultType> {
let blob = Blob::parse(lines)?;
fn part1(&self, lines: &str) -> anyhow::Result<ResultType> {
let blob = Blob::try_from(lines)?;
Ok(ResultType::Integer(blob.count_sides()))
}
fn part2(&self, lines: &[String]) -> anyhow::Result<ResultType> {
let blob = Blob::parse(lines)?;
fn part2(&self, lines: &str) -> anyhow::Result<ResultType> {
let blob = Blob::try_from(lines)?;
Ok(ResultType::Integer(blob.count_outside()))
}
}
@ -131,15 +133,18 @@ struct Blob {
droplets: HashSet<Droplet>,
}
impl Blob {
fn parse(lines: &[String]) -> Result<Self, DropletError> {
let droplets = lines
.iter()
impl TryFrom<&str> for Blob {
type Error = DropletError;
fn try_from(lines: &str) -> Result<Self, Self::Error> {
let droplets = split_lines(lines)
.map(|line| Droplet::parse(line))
.try_collect()?;
Ok(Blob { droplets })
}
}
impl Blob {
fn extent(&self) -> Option<Ranges> {
if self.droplets.is_empty() {
return None;
@ -198,13 +203,13 @@ impl Blob {
#[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(64);
let result = day.part1(&lines)?;
assert_eq!(result, expected);
@ -215,7 +220,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(58);
let result = day.part2(&lines)?;
assert_eq!(result, expected);