1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
use nom::{
branch::alt,
character::complete::{char, line_ending},
combinator::{map, value},
multi::{many1, separated_list1},
IResult,
};
use std::fs;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let input = fs::read_to_string("inputs/day_23.txt")?;
let parsed = ForestMap::parser(&input).unwrap().1;
dbg!(&parsed);
Ok(())
}
#[derive(Debug)]
struct ForestMap(Vec<Vec<ForestTile>>);
#[derive(Debug, Clone)]
enum ForestTile {
Wall,
Open,
SlopeUp,
SlopeDown,
SlopeLeft,
SlopeRight,
}
impl ForestMap {
fn parser(input: &str) -> IResult<&str, Self> {
map(
separated_list1(line_ending, many1(ForestTile::parser)),
ForestMap,
)(input)
}
}
impl ForestTile {
fn parser(input: &str) -> IResult<&str, Self> {
alt((
value(ForestTile::Wall, char('#')),
value(ForestTile::Open, char('.')),
value(ForestTile::SlopeUp, char('^')),
value(ForestTile::SlopeDown, char('v')),
value(ForestTile::SlopeLeft, char('<')),
value(ForestTile::SlopeRight, char('>')),
))(input)
}
}
|