switch to nom

This commit is contained in:
Rüdiger Ludwig 2023-07-30 16:42:17 +02:00
parent a5f19ecae1
commit b00835c25e
7 changed files with 288 additions and 181 deletions

View file

@ -1,3 +1,5 @@
use std::str::FromStr;
use crate::common::file::split_lines;
use super::template::{DayTrait, ResultType};
@ -14,13 +16,13 @@ impl DayTrait for Day {
}
fn part1(&self, lines: &str) -> anyhow::Result<ResultType> {
let forest = Forest::try_from(lines)?;
let forest: Forest = lines.parse()?;
let result = forest.count_visible();
Ok(ResultType::Integer(result))
}
fn part2(&self, lines: &str) -> anyhow::Result<ResultType> {
let forest = Forest::try_from(lines)?;
let forest: Forest = lines.parse()?;
let result = forest.best_score();
Ok(ResultType::Integer(result))
}
@ -181,10 +183,10 @@ impl Forest {
}
}
impl TryFrom<&str> for Forest {
type Error = ForestError;
impl FromStr for Forest {
type Err = ForestError;
fn try_from(lines: &str) -> Result<Self, Self::Error> {
fn from_str(lines: &str) -> Result<Self, Self::Err> {
split_lines(lines)
.map(|line| {
line.chars()
@ -206,7 +208,7 @@ mod test {
fn test_parse() -> Result<()> {
let day = Day {};
let lines = read_string(day.get_day_number(), "example01.txt")?;
let forest = Forest::try_from(lines.as_str())?;
let forest: Forest = lines.parse()?;
assert_eq!(forest.width, 5);
assert_eq!(forest.height, 5);