Skip to content

Commit

Permalink
FINALLY
Browse files Browse the repository at this point in the history
  • Loading branch information
maxi0604 committed Dec 14, 2023
1 parent ab1e70a commit a3d02ed
Showing 1 changed file with 43 additions and 12 deletions.
55 changes: 43 additions & 12 deletions d12/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { assert } from 'console';
import { env } from 'process';
import * as rl from 'readline';

Expand All @@ -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;
}

Expand All @@ -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;
}
}
Expand All @@ -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));

0 comments on commit a3d02ed

Please sign in to comment.