clean up main parser

This commit is contained in:
Joe Ardent 2024-12-26 14:22:52 -08:00
parent d394c3211c
commit e5bb9f3673

View file

@ -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 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))
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);
for (row, line) in cells.iter().enumerate() {
for (col, cell) in line.iter().enumerate() {
if let Cell::Guard(d) = cell {