From 11a2064d5eb2dba0c95e55a9dd75e5950f84a7a6 Mon Sep 17 00:00:00 2001 From: Dustin Swan Date: Wed, 28 Dec 2022 12:56:05 -0700 Subject: [PATCH] Day 16. Didn't finish. Actually barely started. See you next year! --- day16/Cargo.lock | 42 +++++++++++++++++++++++++++++++++ day16/Cargo.toml | 9 +++++++ day16/data.txt | 60 +++++++++++++++++++++++++++++++++++++++++++++++ day16/src/main.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++ day16/test.txt | 10 ++++++++ 5 files changed, 179 insertions(+) create mode 100644 day16/Cargo.lock create mode 100644 day16/Cargo.toml create mode 100644 day16/data.txt create mode 100644 day16/src/main.rs create mode 100644 day16/test.txt diff --git a/day16/Cargo.lock b/day16/Cargo.lock new file mode 100644 index 0000000..caf7216 --- /dev/null +++ b/day16/Cargo.lock @@ -0,0 +1,42 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "0.7.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" +dependencies = [ + "memchr", +] + +[[package]] +name = "day16" +version = "0.1.0" +dependencies = [ + "regex", +] + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "regex" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" diff --git a/day16/Cargo.toml b/day16/Cargo.toml new file mode 100644 index 0000000..0f02dee --- /dev/null +++ b/day16/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day16" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +regex = "1.7.0" diff --git a/day16/data.txt b/day16/data.txt new file mode 100644 index 0000000..334e693 --- /dev/null +++ b/day16/data.txt @@ -0,0 +1,60 @@ +Valve NQ has flow rate=0; tunnels lead to valves SU, XD +Valve AB has flow rate=0; tunnels lead to valves XD, TE +Valve IA has flow rate=0; tunnels lead to valves CS, WF +Valve WD has flow rate=0; tunnels lead to valves DW, II +Valve XD has flow rate=10; tunnels lead to valves AB, NQ, VT, SC, MU +Valve SL has flow rate=0; tunnels lead to valves RP, DS +Valve FQ has flow rate=15; tunnels lead to valves EI, YC +Valve KF has flow rate=0; tunnels lead to valves FL, QP +Valve QP has flow rate=0; tunnels lead to valves KF, RP +Valve DS has flow rate=0; tunnels lead to valves SL, AA +Valve IK has flow rate=0; tunnels lead to valves XC, AA +Valve HQ has flow rate=0; tunnels lead to valves VM, WV +Valve WR has flow rate=0; tunnels lead to valves WV, HF +Valve HH has flow rate=20; tunnels lead to valves PI, CF, CN, NF, AR +Valve DW has flow rate=19; tunnels lead to valves KD, WD, HS +Valve RP has flow rate=14; tunnels lead to valves SL, QP, BH, LI, WP +Valve EC has flow rate=0; tunnels lead to valves NF, XC +Valve AA has flow rate=0; tunnels lead to valves NH, ES, UC, IK, DS +Valve VM has flow rate=18; tunnel leads to valve HQ +Valve NF has flow rate=0; tunnels lead to valves HH, EC +Valve PS has flow rate=0; tunnels lead to valves AR, SU +Valve IL has flow rate=0; tunnels lead to valves XC, KZ +Valve WP has flow rate=0; tunnels lead to valves CS, RP +Valve WF has flow rate=0; tunnels lead to valves FL, IA +Valve XW has flow rate=0; tunnels lead to valves OL, NL +Valve EH has flow rate=0; tunnels lead to valves UK, YR +Valve UC has flow rate=0; tunnels lead to valves AA, FL +Valve CS has flow rate=3; tunnels lead to valves IA, CN, LD, RJ, WP +Valve AR has flow rate=0; tunnels lead to valves PS, HH +Valve CF has flow rate=0; tunnels lead to valves HH, FL +Valve NH has flow rate=0; tunnels lead to valves AA, LD +Valve RJ has flow rate=0; tunnels lead to valves DJ, CS +Valve XC has flow rate=17; tunnels lead to valves IL, EC, YR, IK, DJ +Valve TE has flow rate=24; tunnels lead to valves AB, YA +Valve CN has flow rate=0; tunnels lead to valves HH, CS +Valve KD has flow rate=0; tunnels lead to valves DW, UK +Valve SC has flow rate=0; tunnels lead to valves EI, XD +Valve MU has flow rate=0; tunnels lead to valves XD, YP +Valve SU has flow rate=22; tunnels lead to valves PS, LI, II, NQ +Valve FL has flow rate=8; tunnels lead to valves KF, WF, CF, UC, HS +Valve OL has flow rate=4; tunnels lead to valves KZ, HF, XW +Valve EI has flow rate=0; tunnels lead to valves FQ, SC +Valve NL has flow rate=0; tunnels lead to valves XW, UK +Valve YP has flow rate=21; tunnels lead to valves YA, MU, YC +Valve BH has flow rate=0; tunnels lead to valves VT, RP +Valve II has flow rate=0; tunnels lead to valves SU, WD +Valve YA has flow rate=0; tunnels lead to valves TE, YP +Valve HS has flow rate=0; tunnels lead to valves FL, DW +Valve DJ has flow rate=0; tunnels lead to valves RJ, XC +Valve KZ has flow rate=0; tunnels lead to valves OL, IL +Valve YR has flow rate=0; tunnels lead to valves EH, XC +Valve UK has flow rate=7; tunnels lead to valves KD, NL, EH +Valve YC has flow rate=0; tunnels lead to valves FQ, YP +Valve ES has flow rate=0; tunnels lead to valves PI, AA +Valve LI has flow rate=0; tunnels lead to valves SU, RP +Valve LD has flow rate=0; tunnels lead to valves NH, CS +Valve VT has flow rate=0; tunnels lead to valves BH, XD +Valve PI has flow rate=0; tunnels lead to valves ES, HH +Valve WV has flow rate=11; tunnels lead to valves WR, HQ +Valve HF has flow rate=0; tunnels lead to valves OL, WR diff --git a/day16/src/main.rs b/day16/src/main.rs new file mode 100644 index 0000000..7d1e5f6 --- /dev/null +++ b/day16/src/main.rs @@ -0,0 +1,58 @@ +use regex::Regex; +use std::collections::{HashMap, HashSet}; + +#[derive(Debug)] +struct Room<'a> { + valve: &'a str, + rate: u8, + tunnels: Vec<&'a str>, +} + +fn main() { + let contents = std::fs::read_to_string("test.txt").expect("Failed to read file"); + + let mut hash: HashMap<&str, Room> = HashMap::new(); + + contents.lines().for_each(|l| parse(l, &mut hash)); + println!("rooms {:?}", hash); + + traverse(&mut hash, "AA"); +} + +fn parse<'a>(str: &'a str, hash: &mut HashMap<&'a str, Room<'a>>) -> () { + let re = Regex::new(r"Valve (.*) has flow rate=(.*); tunnels? leads? to valves? (.*)").unwrap(); + let c = re.captures(str).unwrap(); + let valve = c.get(1).unwrap().as_str(); + let rate = c.get(2).unwrap().as_str().parse().unwrap(); + let tunnels = c + .get(3) + .unwrap() + .as_str() + .split(", ") + .collect::>(); + hash.insert( + valve, + Room { + valve, + rate, + tunnels, + }, + ); +} + +fn traverse(hash: &mut HashMap<&str, Room>, start: &str) -> () { + let mut visited: HashSet<&str> = HashSet::new(); + let cur = hash.get(start).unwrap(); + + // let mut max = 0; + let mut biggest = cur.tunnels.iter().fold((start, 0), |acc, r| { + let room = hash.get(r).unwrap(); + if hash.get(r).unwrap().rate > max { + max = room.rate; + r + } else { + acc + } + }); + println!("biggest {:?}", biggest); +} diff --git a/day16/test.txt b/day16/test.txt new file mode 100644 index 0000000..9f30acc --- /dev/null +++ b/day16/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