Started on aoc day 16 but it's pretty f'ing hard

This commit is contained in:
2023-01-12 18:09:23 +01:00
parent a72207c754
commit 572c74ce3f
5 changed files with 168 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
use nom::{
IResult,
multi::separated_list1,
sequence::{tuple, preceded},
character::complete::{self, multispace1},
bytes::complete::take,
bytes::streaming::tag,
branch::alt, Parser,
};
#[derive(Debug, PartialEq, PartialOrd, Clone)]
struct Valve {
id: String,
flow: u32,
tunnels: Vec<String>,
}
fn parse_input(input: &str) -> IResult<&str, Vec<(String, u32, Vec<String>)>> {
let (input, valve_tuples) = separated_list1(
multispace1,
tuple((
preceded(
tag("Valve "),
take(2 as usize).map(|s| String::from(s))),
preceded(
tag(" has flow rate="),
complete::u32),
alt((
preceded(
tag("; tunnels lead to valves "),
separated_list1(
tag(", "),
take(2 as usize).map(|s| String::from(s)))),
preceded(
tag("; tunnel leads to valve "),
take(2 as usize).map(|s| vec![String::from(s)])),
)),
))
)(input)?;
Ok((input, valve_tuples))
}
fn main() {
let input_text = include_str!("../test.txt");
let (_rest, valve_tuples) = parse_input(input_text).unwrap();
// Test the parsing
// for vt in &valve_tuples {
// print!("Valve {} has flow rate={}; tunnels lead to valves ", vt.0.as_str(), vt.1);
// for v in &vt.2 {
// print!("{}, ", v.as_str());
// }
// println!();
// }
let valves : Vec<Valve> = valve_tuples
.into_iter()
.map(|(i, f, t)| Valve {id: i, flow: f, tunnels: t})
.collect();
}