Advent of Code 2022 Day 16

Author

Nathan Moore

— Day 16: Proboscidea Volcanium —

You’re in a volcano! You have limited time to get the elephants (?) out of there. To help, you can release some pressure in some valves.

Work out the steps to release the most pressure in 30 minutes. What is the most pressure you can release?

import re

with open('data-2022-16.txt', 'r') as f:
    inp = f.read().splitlines()

There sure does seem to be a lot of input manipulation in these questions.

v = r'Valve ([A-Z]{2})'
fr = r'.* rate=(\d+)'
tun = r'.* (valves|valve) (.*)$'

valves = {re.match(v, i)[1]: 
          [int(re.match(fr, i)[1]), 
          re.match(tun, i)[2].split(', ')]
          for i in inp}

valves
{'AP': [0, ['AA', 'ON']],
 'QN': [21, ['RI', 'CG']],
 'LK': [0, ['XM', 'AA']],
 'HA': [0, ['WH', 'KF']],
 'DS': [16, ['II']],
 'KD': [0, ['KG', 'QB']],
 'JW': [0, ['AD', 'KF']],
 'HU': [0, ['UK', 'CO']],
 'AE': [10, ['IR', 'PT', 'UV']],
 'XA': [0, ['CG', 'EU']],
 'SE': [17, ['YR', 'AD']],
 'TR': [0, ['AL', 'CS']],
 'BS': [0, ['YH', 'XM']],
 'IJ': [24, ['XN', 'WE']],
 'AA': [0, ['LK', 'AP', 'IZ', 'PC', 'QD']],
 'KG': [0, ['KD', 'CS']],
 'QV': [0, ['XM', 'II']],
 'PC': [0, ['AA', 'YF']],
 'GJ': [20, ['RI']],
 'UV': [0, ['UK', 'AE']],
 'IR': [0, ['EU', 'AE']],
 'EU': [13, ['IR', 'DT', 'XA', 'ON']],
 'ED': [0, ['XN', 'CO']],
 'DT': [0, ['EU', 'UK']],
 'YE': [0, ['XM', 'WS']],
 'AD': [0, ['JW', 'SE']],
 'WE': [0, ['IJ', 'NA']],
 'UK': [5, ['UV', 'DT', 'QD', 'HU']],
 'YR': [0, ['OS', 'SE']],
 'II': [0, ['QV', 'DS']],
 'GT': [0, ['CS', 'MN']],
 'YH': [0, ['BS', 'QB']],
 'BQ': [0, ['XM', 'KF']],
 'OS': [0, ['YR', 'NA']],
 'WH': [0, ['QB', 'HA']],
 'QB': [4, ['WH', 'KD', 'YH', 'IZ']],
 'ON': [0, ['AP', 'EU']],
 'IZ': [0, ['AA', 'QB']],
 'MN': [25, ['GT']],
 'CG': [0, ['XA', 'QN']],
 'QD': [0, ['UK', 'AA']],
 'AL': [0, ['KF', 'TR']],
 'XN': [0, ['ED', 'IJ']],
 'WS': [0, ['YE', 'CS']],
 'CO': [18, ['ED', 'PT', 'HU']],
 'PT': [0, ['CO', 'AE']],
 'RI': [0, ['QN', 'GJ']],
 'CS': [9, ['YF', 'GT', 'WS', 'TR', 'KG']],
 'YF': [0, ['PC', 'CS']],
 'NA': [23, ['OS', 'WE']],
 'KF': [12, ['HA', 'AL', 'JW', 'BQ']],
 'XM': [3, ['LK', 'QV', 'YE', 'BS', 'BQ']]}

We don’t want to spend time opening valves that have no flow rate, but they might be able to get us to valves that do have good flow.

Start at AA; None of the valves that come next have any flow, but some of them also lead back to AA, which is not helpful. Care needs to be taken when searching to not make loops. Don’t go back to where we have already been.