From 1ef61438954088742d2fbff4c08d921e7f640d94 Mon Sep 17 00:00:00 2001 From: Lars Martens Date: Fri, 16 Dec 2022 21:42:59 +0100 Subject: [PATCH] Fail day 16 Here's my attempt anyways. --- input/16 | 55 +++++++++++++++++++++++++ input/16-test | 10 +++++ src/bin/16.rs | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 176 insertions(+) create mode 100644 input/16 create mode 100644 input/16-test create mode 100644 src/bin/16.rs diff --git a/input/16 b/input/16 new file mode 100644 index 0000000..c829cdc --- /dev/null +++ b/input/16 @@ -0,0 +1,55 @@ +Valve SW has flow rate=0; tunnels lead to valves LX, LD +Valve VS has flow rate=0; tunnels lead to valves JO, OO +Valve OO has flow rate=10; tunnels lead to valves KK, HD, VS, KI +Valve DZ has flow rate=8; tunnels lead to valves KV, GX, WQ, BA, PK +Valve GX has flow rate=0; tunnels lead to valves AA, DZ +Valve IF has flow rate=0; tunnels lead to valves OI, DW +Valve BO has flow rate=0; tunnels lead to valves UJ, ZT +Valve KI has flow rate=0; tunnels lead to valves OO, KU +Valve JT has flow rate=3; tunnels lead to valves FC, AM, KV, XP, XZ +Valve TQ has flow rate=0; tunnels lead to valves AA, DW +Valve KK has flow rate=0; tunnels lead to valves QW, OO +Valve NR has flow rate=0; tunnels lead to valves UG, XM +Valve VO has flow rate=0; tunnels lead to valves YR, AA +Valve MS has flow rate=17; tunnels lead to valves LT, LX +Valve JO has flow rate=0; tunnels lead to valves YR, VS +Valve ZB has flow rate=0; tunnels lead to valves UJ, LT +Valve ZT has flow rate=0; tunnels lead to valves XM, BO +Valve YR has flow rate=9; tunnels lead to valves VO, FY, WB, JO +Valve QS has flow rate=0; tunnels lead to valves QW, FY +Valve UD has flow rate=0; tunnels lead to valves CA, JB +Valve AP has flow rate=0; tunnels lead to valves CA, DW +Valve KV has flow rate=0; tunnels lead to valves JT, DZ +Valve JH has flow rate=0; tunnels lead to valves IK, UJ +Valve LD has flow rate=15; tunnels lead to valves IK, SW +Valve XK has flow rate=0; tunnels lead to valves XZ, BH +Valve XM has flow rate=11; tunnels lead to valves XP, CJ, ZT, NR +Valve FY has flow rate=0; tunnels lead to valves YR, QS +Valve GI has flow rate=22; tunnel leads to valve TI +Valve JB has flow rate=14; tunnels lead to valves WB, UD, WQ, HD +Valve DW has flow rate=6; tunnels lead to valves AP, TQ, NQ, IF, PK +Valve UJ has flow rate=13; tunnels lead to valves JH, ZB, BO +Valve KU has flow rate=0; tunnels lead to valves CA, KI +Valve WQ has flow rate=0; tunnels lead to valves JB, DZ +Valve BA has flow rate=0; tunnels lead to valves BH, DZ +Valve AA has flow rate=0; tunnels lead to valves YX, TQ, VO, GX, QP +Valve TI has flow rate=0; tunnels lead to valves GI, UG +Valve FC has flow rate=0; tunnels lead to valves QP, JT +Valve CA has flow rate=18; tunnels lead to valves KU, UD, AP +Valve QW has flow rate=25; tunnels lead to valves QS, KK +Valve XZ has flow rate=0; tunnels lead to valves JT, XK +Valve YX has flow rate=0; tunnels lead to valves AA, CJ +Valve OI has flow rate=0; tunnels lead to valves IF, BH +Valve NQ has flow rate=0; tunnels lead to valves AM, DW +Valve QP has flow rate=0; tunnels lead to valves AA, FC +Valve AM has flow rate=0; tunnels lead to valves NQ, JT +Valve XP has flow rate=0; tunnels lead to valves XM, JT +Valve BH has flow rate=12; tunnels lead to valves BA, XK, OI +Valve HD has flow rate=0; tunnels lead to valves OO, JB +Valve LT has flow rate=0; tunnels lead to valves MS, ZB +Valve LX has flow rate=0; tunnels lead to valves MS, SW +Valve CJ has flow rate=0; tunnels lead to valves XM, YX +Valve PK has flow rate=0; tunnels lead to valves DW, DZ +Valve IK has flow rate=0; tunnels lead to valves LD, JH +Valve WB has flow rate=0; tunnels lead to valves YR, JB +Valve UG has flow rate=21; tunnels lead to valves TI, NR diff --git a/input/16-test b/input/16-test new file mode 100644 index 0000000..85fa5b0 --- /dev/null +++ b/input/16-test @@ -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 diff --git a/src/bin/16.rs b/src/bin/16.rs new file mode 100644 index 0000000..3ecd974 --- /dev/null +++ b/src/bin/16.rs @@ -0,0 +1,111 @@ +use std::collections::HashMap; + +use aoc2022::*; +use itertools::Itertools; + +const INPUT: &str = include_str!("../../input/16"); + +fn main() { + println!("This does not work :("); + let mut cave = Cave::new(); + solved_level_1(cave.level1()); +} + +type Str = &'static str; + +#[derive(Debug)] +struct Valve { + id: Str, + rate: i64, + tunnels: Vec, +} + +struct Cave { + valves: HashMap, + distance_cache: HashMap<(Str, Str), i64>, +} + +impl Cave { + fn level1(&mut self) -> i64 { + let mut remaining = self + .valves + .keys() + .cloned() + .filter(|&k| k != "AA") + .collect_vec(); + + self.level1_rec("AA", &mut remaining, 30) + } + + fn level1_rec(&mut self, at: Str, remaining: &mut Vec, time: i64) -> i64 { + let mut other_releases = Vec::new(); + + for _ in 0..remaining.len() { + let next = remaining.remove(0); + + let time = time - self.distance(at, next, 0) - 1; + if time > 0 { + other_releases.push(self.level1_rec(next, remaining, time)); + } + + remaining.push(next); + } + + let release = self.valves[at].rate * time; + release + other_releases.into_iter().max().unwrap_or(0) + } + + fn distance(&mut self, from: Str, to: Str, stopper: usize) -> i64 { + if from == to { + return 0; + } + if let Some(d) = self.distance_cache.get(&(from, to)) { + return *d; + } + if let Some(d) = self.distance_cache.get(&(to, from)) { + return *d; + } + if self.valves[from].tunnels.contains(&to) { + return 1; + } + if stopper > 30 { + return 1000; + } + + // Find recursively via neighbors. + let mut distances = Vec::new(); + for neighbor in self.valves[from].tunnels.clone() { + let d = self.distance(neighbor, to, stopper + 1); + distances.push(d + 1); + } + + let best = distances.into_iter().min().unwrap(); + self.distance_cache.insert((from, to), best); + best + } + + fn new() -> Self { + let valves = INPUT + .lines() + .map(|line| { + let parts = line.split(' ').collect_vec(); + let id = parts[1]; + let rate = parts[4].split_once('=').unwrap().1; + let rate = rate.strip_suffix(';').unwrap_or(rate).parse().unwrap(); + + let tunnels = parts + .iter() + .skip(9) + .map(|part| part.strip_suffix(',').unwrap_or(part)) + .collect(); + + Valve { id, rate, tunnels } + }) + .map(|v| (v.id, v)) + .collect(); + Self { + valves, + distance_cache: HashMap::new(), + } + } +}