diff --git a/2022/16/part1.js b/2022/16/part1.js new file mode 100644 index 0000000..9809253 --- /dev/null +++ b/2022/16/part1.js @@ -0,0 +1,66 @@ +import * as R from "remeda"; +import u from "updeep"; + +import { readFile } from "../05/part1.js"; + +const parseLine = (line) => { + const {groups} = line.match( /Valve (?..).*flow rate=(?\d+).*valves? (?.*)/ ); + return [ groups.valve, { + exits: groups.exits.split(',').map( x => x.trim() ), + flow: groups.flow + } ] +} +const readInput = (...args) => + R.pipe( + readFile(...args), + (lines) => lines.split("\n"), + R.compact, // remove last line + R.map(parseLine), + Object.fromEntries, + ); + +export const puzzleInput = readInput(import.meta.url, "input"); +export const sample = readInput(import.meta.url, "sample"); + +function bestMoveFor(tunnels,minutesLeft,opened,location='AA',totalSteam=0,activeSteam=0) { + console.log(minutesLeft, totalSteam); + + if(minutesLeft<=0) return totalSteam; + + totalSteam += activeSteam; + + const possibilities = []; + + if(!opened.includes(location)) { + possibilities.push( + bestMoveFor( + tunnels, + minutesLeft-1, + [...opened, location], + location, + totalSteam, + activeSteam + tunnels[location].flow + ) + ) + } + + for( const exit of tunnels[location].exits ) { + possibilities.push( + bestMoveFor( + tunnels, + minutesLeft-1, + opened, + exit, + totalSteam, + activeSteam, + ) + ) + } + + return Math.max( ...possibilities ); +} + + +export default (tunnels) => { + return bestMoveFor(tunnels,30,[]); +}; diff --git a/2022/16/part2.js b/2022/16/part2.js new file mode 100644 index 0000000..10fc179 --- /dev/null +++ b/2022/16/part2.js @@ -0,0 +1,4 @@ +import * as R from "remeda"; + + +export default () => {}; diff --git a/2022/16/sample b/2022/16/sample new file mode 100644 index 0000000..9f30acc --- /dev/null +++ b/2022/16/sample @@ -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 diff --git a/2022/16/test.js b/2022/16/test.js new file mode 100644 index 0000000..8521f1c --- /dev/null +++ b/2022/16/test.js @@ -0,0 +1,29 @@ +import { test, expect, describe } from "vitest"; + +import { expectSolution } from "../01/main.js"; +import part1, { sample, puzzleInput } from "./part1.js"; +import part2 from "./part2.js"; + +describe("part 1", () => { + test( "input", ()=> { + expect(sample).toMatchObject({AA: { + exists: [ + 'DD', 'II', 'BB', + ], + flow: 0, + }}) + + } ) + test.only("sample", () => { + expectSolution(part1(sample)).toEqual(1651); + }); + test.todo("solution", () => { + expectSolution(part1(puzzleInput)).toEqual("TODO"); + }); +}); + +describe("part 2", () => { + test.todo("solution", () => { + expectSolution(part2(puzzleInput)).toEqual("TODO"); + }); +});