From a3d02ed5d7c513ef4d0e52c075bab2ffe048664b Mon Sep 17 00:00:00 2001 From: "Maximilian R." Date: Thu, 14 Dec 2023 14:14:12 +0100 Subject: [PATCH] FINALLY --- d12/main.ts | 55 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/d12/main.ts b/d12/main.ts index 3eb4f0f..2cb49e8 100644 --- a/d12/main.ts +++ b/d12/main.ts @@ -1,3 +1,4 @@ +import { assert } from 'console'; import { env } from 'process'; import * as rl from 'readline'; @@ -8,12 +9,15 @@ const term = rl.createInterface({ function debug(obj: any) { if (env.DEBUG || env.TRACE) - console.log(obj); + console.log("\t".repeat(indentLevel) + obj); } function checkFit(field: string, len: number, idx: number) { + if (idx + len > field.length) + return false; + for (let i = idx; i < idx + len; ++i) { - if (i >= field.length || field[i] == '.') + if (field[i] == '.') return false; } @@ -26,19 +30,44 @@ function checkFit(field: string, len: number, idx: number) { return true; } +function checkCovered(field: string, nums: number[], stack: number[]) { + assert(stack.length == nums.length); + for (let i = 0; i < field.length; ++i) { + if (field[i] == '#') { + let found = false; + for (let j = 0; j < stack.length; ++j) { + if (stack[j] <= i && i < stack[j] + nums[j]) { + found = true; + break; + } + } + + if (!found) { + debug("rejecting " + stack); + return false; + } + } + } + + return true; +} + let indentLevel = 0; -function recurse(nums: number[], field: string, idx: number) { - if (nums.length == 0) { - return 1; +function recurse(nums: number[], numIdx: number, field: string, idx: number, stack: number[]) { + if (nums.length == numIdx) { + debug("checking final."); + return checkCovered(field, nums, stack) ? 1 : 0; } let count = 0; for (let i = idx; i < field.length; ++i) { - if (checkFit(field, nums[0], i)) { + if (checkFit(field, nums[numIdx], i)) { indentLevel++; - let sub = recurse(nums.slice(1), field, i + nums[0] + 1); + stack.push(i); + let sub = recurse(nums, numIdx + 1, field, i + nums[numIdx] + 1, stack); + stack.pop(); indentLevel--; - debug("\t".repeat(indentLevel) + `counting ${sub} at ${i} with len = ${nums[0]}`); + debug(`counting ${sub} at ${i} with len = ${nums[numIdx]}`); count += sub; } } @@ -51,14 +80,16 @@ function doTheThing(lines: string[]) { for (const line of lines) { const split = line.trim().split(" ").filter(i => i); const nums = split[1].split(",").map(x => Number(x)); - const springs = split[0]; - let res = recurse(nums, springs, 0); - console.log(`${springs} ${res}`); + let springs = split[0]; + let stack: number[] = []; + // springs = ".............................................................." + springs + "..................................................................."; + let res = recurse(nums, 0, springs, 0, stack); + console.log(`${line} ${res}`); sum += res; } console.log(`sum: ${sum}`); } let lines: string[] = []; -term.on("line", (input: string) => { lines.push(input) }); +term.on("line", (input: string) => { lines.push(input); }); term.on("close", () => doTheThing(lines));