Last active
January 17, 2025 15:51
-
-
Save niorad/293b3eefa9f1821e3dbb0dd36f282330 to your computer and use it in GitHub Desktop.
xxx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { goget } from "../_util/util.ts"; | |
const allPatterns: string[] = goget("input.txt")[0].split(", ").sort(( | |
a, | |
b, | |
) => a.length - b.length); | |
const designs: string[] = goget("input2.txt"); | |
let possibleDesigns = 0; | |
let patterns: string[] = []; | |
const cache = {}; | |
designs.forEach((design) => { | |
console.log("Checking", design); | |
patterns = allPatterns.filter((p) => design.includes(p)); | |
if (doit(design)) possibleDesigns++; | |
}); | |
console.log("Solution Pt. 1", possibleDesigns); | |
function doit(restOfString: string): boolean { | |
if (restOfString === "") { | |
console.log("Solved!"); | |
return true; | |
} | |
if (cache.hasOwnProperty(restOfString)) { | |
return cache[restOfString]; | |
} | |
const filteredPatterns = patterns.filter((p) => restOfString.startsWith(p)); | |
for (let i = 0; i < filteredPatterns.length; i++) { | |
if (doit(restOfString.substring(filteredPatterns[i].length))) { | |
cache[restOfString] = true; | |
return true; | |
} | |
} | |
cache[restOfString] = false; | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment