diff --git a/advent_of_code/2022/16/Cargo.lock b/advent_of_code/2022/16/Cargo.lock new file mode 100644 index 0000000..b8ae363 --- /dev/null +++ b/advent_of_code/2022/16/Cargo.lock @@ -0,0 +1,32 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "main" +version = "0.1.0" +dependencies = [ + "nom", +] + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "nom" +version = "7.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5507769c4919c998e69e49c839d9dc6e693ede4cc4290d6ad8b41d4f09c548c" +dependencies = [ + "memchr", + "minimal-lexical", +] diff --git a/advent_of_code/2022/16/Cargo.toml b/advent_of_code/2022/16/Cargo.toml new file mode 100644 index 0000000..200567e --- /dev/null +++ b/advent_of_code/2022/16/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "main" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +nom = "7.1.2" diff --git a/advent_of_code/2022/16/input.txt b/advent_of_code/2022/16/input.txt new file mode 100644 index 0000000..86d44b6 --- /dev/null +++ b/advent_of_code/2022/16/input.txt @@ -0,0 +1,54 @@ +Valve XG has flow rate=0; tunnels lead to valves CR, OH +Valve ZF has flow rate=7; tunnels lead to valves SC, BY, PM, LW, CJ +Valve RD has flow rate=13; tunnels lead to valves JS, VM +Valve XJ has flow rate=0; tunnels lead to valves JO, YO +Valve CJ has flow rate=0; tunnels lead to valves UA, ZF +Valve UA has flow rate=0; tunnels lead to valves ZP, CJ +Valve EQ has flow rate=0; tunnels lead to valves ZP, RP +Valve IU has flow rate=0; tunnels lead to valves EV, CN +Valve QM has flow rate=0; tunnels lead to valves XA, CN +Valve WC has flow rate=0; tunnels lead to valves JS, OH +Valve MU has flow rate=0; tunnels lead to valves AA, ZP +Valve MW has flow rate=11; tunnels lead to valves BM, AG, RG, NL +Valve XA has flow rate=0; tunnels lead to valves JO, QM +Valve OH has flow rate=12; tunnels lead to valves WC, YS, XG, KO +Valve QD has flow rate=20; tunnels lead to valves BY, KY, CR, RP +Valve OE has flow rate=0; tunnels lead to valves FB, BU +Valve CB has flow rate=0; tunnels lead to valves AA, FX +Valve TB has flow rate=23; tunnel leads to valve VM +Valve PM has flow rate=0; tunnels lead to valves ZF, AA +Valve YS has flow rate=0; tunnels lead to valves OH, RG +Valve KO has flow rate=0; tunnels lead to valves OH, VT +Valve AA has flow rate=0; tunnels lead to valves PM, MU, BM, AW, CB +Valve RG has flow rate=0; tunnels lead to valves YS, MW +Valve BU has flow rate=0; tunnels lead to valves OE, EV +Valve RK has flow rate=0; tunnels lead to valves KY, FX +Valve JO has flow rate=18; tunnels lead to valves NL, SX, XA, XJ +Valve AG has flow rate=0; tunnels lead to valves IS, MW +Valve AW has flow rate=0; tunnels lead to valves BS, AA +Valve ZP has flow rate=9; tunnels lead to valves UA, NG, DU, MU, EQ +Valve KY has flow rate=0; tunnels lead to valves QD, RK +Valve EV has flow rate=19; tunnels lead to valves VT, BU, IU, SX +Valve FB has flow rate=24; tunnel leads to valve OE +Valve DU has flow rate=0; tunnels lead to valves IS, ZP +Valve NG has flow rate=0; tunnels lead to valves FX, ZP +Valve HC has flow rate=0; tunnels lead to valves CN, HB +Valve SC has flow rate=0; tunnels lead to valves IS, ZF +Valve HB has flow rate=22; tunnel leads to valve HC +Valve VM has flow rate=0; tunnels lead to valves RD, TB +Valve LW has flow rate=0; tunnels lead to valves ZF, FX +Valve SX has flow rate=0; tunnels lead to valves EV, JO +Valve FX has flow rate=6; tunnels lead to valves FA, NG, RK, LW, CB +Valve JS has flow rate=0; tunnels lead to valves WC, RD +Valve BM has flow rate=0; tunnels lead to valves MW, AA +Valve FA has flow rate=0; tunnels lead to valves IS, FX +Valve RP has flow rate=0; tunnels lead to valves QD, EQ +Valve NL has flow rate=0; tunnels lead to valves MW, JO +Valve CN has flow rate=15; tunnels lead to valves HC, QM, IU +Valve BS has flow rate=0; tunnels lead to valves IS, AW +Valve KP has flow rate=25; tunnel leads to valve YO +Valve YO has flow rate=0; tunnels lead to valves XJ, KP +Valve CR has flow rate=0; tunnels lead to valves XG, QD +Valve BY has flow rate=0; tunnels lead to valves QD, ZF +Valve IS has flow rate=5; tunnels lead to valves DU, SC, AG, FA, BS +Valve VT has flow rate=0; tunnels lead to valves KO, EV \ No newline at end of file diff --git a/advent_of_code/2022/16/src/main.rs b/advent_of_code/2022/16/src/main.rs new file mode 100644 index 0000000..c533940 --- /dev/null +++ b/advent_of_code/2022/16/src/main.rs @@ -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, +} + +fn parse_input(input: &str) -> IResult<&str, Vec<(String, u32, Vec)>> { + 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_tuples + .into_iter() + .map(|(i, f, t)| Valve {id: i, flow: f, tunnels: t}) + .collect(); + + +} diff --git a/advent_of_code/2022/16/test.txt b/advent_of_code/2022/16/test.txt new file mode 100644 index 0000000..85fa5b0 --- /dev/null +++ b/advent_of_code/2022/16/test.txt @@ -0,0 +1,10 @@ +Valve AA has flow rate=0; tunnels lead to valves DD, II, BB +Valve BB has flow rate=13; tunnels lead to valves CC, AA +Valve CC has flow rate=2; tunnels lead to valves DD, BB +Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE +Valve EE has flow rate=3; tunnels lead to valves FF, DD +Valve FF has flow rate=0; tunnels lead to valves EE, GG +Valve GG has flow rate=0; tunnels lead to valves FF, HH +Valve HH has flow rate=22; tunnel leads to valve GG +Valve II has flow rate=0; tunnels lead to valves AA, JJ +Valve JJ has flow rate=21; tunnel leads to valve II \ No newline at end of file