Skip to content

Commit

Permalink
Seems fine
Browse files Browse the repository at this point in the history
  • Loading branch information
maxi0604 committed Dec 14, 2023
1 parent 15e2fe1 commit ed12de5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
14 changes: 13 additions & 1 deletion d12/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@ build:
tsc

run-d1:
just run
< input.txt just run
run-d2:
just run

run:
node main.js

example:
just build
< p1in.txt just run | less

debug:
just build
DEBUG=1 < p1in.txt just run | less

trace:
just build
TRACE=1 < p1in.txt just run | less
34 changes: 28 additions & 6 deletions d12/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
import { env } from 'process';
import * as rl from 'readline';

const term = rl.createInterface({
input: process.stdin,
output: process.stdout,
})

function checkFit(field: string, len: number, idx: number) {
return !!field.slice(Math.max(idx - 1, 0), Math.min(idx + len + 1, field.length)).match(/^[.?][#?]+[.?]?$/);
function debug(obj: any) {
if (env.DEBUG || env.TRACE)
console.log(obj);
}

function trace(obj: any) {
if (env.TRACE)
console.log(obj);
}

function checkFit(field: string, len: number, idx: number, last: boolean) {
for (let i = idx; i < idx + len; ++i) {
if (i > field.length || field[i] == '.')
return false;
}

if (idx > 0 && field[idx - 1] == '#')
return false;

if (idx + len < field.length && field[idx + len] == '#')
return false;

return true;
}

let indentLevel = 0;
Expand All @@ -17,11 +39,11 @@ function recurse(nums: number[], field: string, idx: number) {

let count = 0;
for (let i = idx; i < field.length; ++i) {
if (checkFit(field, nums[0], i)) {
if (checkFit(field, nums[0], i, nums.length == 1)) {
indentLevel++;
let sub = recurse(nums.slice(1), field, i + nums[0] + 1);
indentLevel--;
console.log(" ".repeat(4 * indentLevel) + `counting ${sub} at ${i} with len = ${nums[0]}`);
debug(" ".repeat(4 * indentLevel) + `counting ${sub} at ${i} with len = ${nums[0]}`);
count += sub;
}
}
Expand All @@ -34,8 +56,8 @@ function doTheThing(lines: string[]) {
const split = line.trim().split(" ").filter(i => i);
const nums = split[1].split(",").map(x => Number(x));
const springs = split[0];
console.log(nums, springs);
console.log(recurse(nums, springs, 0));
let res = recurse(nums, springs, 0);
console.log(`${springs} ${res}`);
}
}

Expand Down

0 comments on commit ed12de5

Please sign in to comment.