From e5bb9f367314900f90d6b773e7b4f2d66d77b2c9 Mon Sep 17 00:00:00 2001 From: Joe Ardent <code@ardent.nebcorp.com> Date: Thu, 26 Dec 2024 14:22:52 -0800 Subject: [PATCH] clean up main parser --- day06/src/main.rs | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/day06/src/main.rs b/day06/src/main.rs index 503d433..8456a49 100644 --- a/day06/src/main.rs +++ b/day06/src/main.rs @@ -5,8 +5,8 @@ use std::{ use winnow::{ PResult, Parser, - ascii::{newline, till_line_ending}, - combinator::{alt, opt, repeat, separated}, + ascii::newline, + combinator::{alt, opt, repeat, separated, seq}, }; fn main() { @@ -18,7 +18,12 @@ fn main() { fn pt1(input: &str) -> usize { let mut board = Board::new(input); - board.run() + let mut locs = HashSet::new(); + locs.insert(board.guard.0); + while let Some(loc) = board.step().map(|l| l.0) { + locs.insert(loc); + } + locs.len() } fn pt2(input: &str) -> usize { @@ -184,15 +189,6 @@ impl Board { None } } - - fn run(&mut self) -> usize { - let mut locs = HashSet::new(); - locs.insert(self.guard.0); - while let Some(loc) = self.step().map(|l| l.0) { - locs.insert(loc); - } - locs.len() - } } fn parse_cell(input: &mut &str) -> PResult<Cell> { @@ -217,25 +213,21 @@ fn parse_floor(input: &mut &str) -> PResult<Cell> { match g { '.' => Ok(Cell::Empty), '#' => Ok(Cell::Obstacle), - 'X' => Ok(Cell::Visited), _ => unreachable!(), } } fn parse_line(input: &mut &str) -> PResult<Vec<Cell>> { - let mut line = till_line_ending.parse_next(input)?; - repeat(1.., parse_cell).parse_next(&mut line) + repeat(1.., parse_cell).parse_next(input) } fn parse(input: &str) -> PResult<Board> { + let (cells, _): (Vec<Vec<Cell>>, _) = seq!(separated(1.., parse_line, newline), opt(newline)) + .parse(input) + .unwrap(); + let mut gdir = Dir::N; let mut loc = Loc(0, 0); - - let (cells, _): (Vec<Vec<Cell>>, _) = - winnow::combinator::seq!(separated(1.., parse_line, newline), opt(newline)) - .parse(input) - .unwrap(); - for (row, line) in cells.iter().enumerate() { for (col, cell) in line.iter().enumerate() { if let Cell::Guard(d) = cell {