-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e20c152
commit a0aeb86
Showing
5 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { assertEquals } from "@std/assert"; | ||
import { main } from "./main.ts"; | ||
|
||
const target = "input"; | ||
|
||
Deno.test(`correct possible design count for ${target}`, async () => { | ||
const result = await main(target); | ||
assertEquals(result.possibleDesigns, 353); | ||
}); | ||
|
||
Deno.test(`correct possible design combos for ${target}`, async () => { | ||
const result = await main(target); | ||
assertEquals(result.possibleDesignCombos, 880877787214477); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// { possibleDesigns: 353, possibleDesignCombos: 880877787214477 } | ||
// Elapsed: 133ms | ||
|
||
function testDesign(towels: string[], design: string): number { | ||
const designCounter = new Array(design.length + 1).fill(0); | ||
designCounter[0] = 1; | ||
for (let i = 0; i <= design.length; i++) { | ||
if (designCounter[i]) { | ||
for (const towel of towels) { | ||
if ( | ||
i + towel.length <= design.length && | ||
towel === design.slice(i, i + towel.length) | ||
) { | ||
designCounter[i + towel.length] += designCounter[i]; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return designCounter[design.length]; | ||
} | ||
|
||
export async function main(target = "input") { | ||
const dirpath = new URL(".", import.meta.url).pathname; | ||
const text = await Deno.readTextFile(`${dirpath}${target}.txt`); | ||
|
||
const { patterns, designs } = text.split("\n").reduce((agg, line) => { | ||
if (line.match(/^[wubrg]+$/)) { | ||
agg.designs.push(line); | ||
return agg; | ||
} | ||
|
||
const towelsMatch = line.match(/^[wubrg, ]+$/); | ||
if (towelsMatch) { | ||
agg.patterns = towelsMatch[0].split(", "); | ||
} | ||
|
||
return agg; | ||
}, { patterns: [] as string[], designs: [] as string[] }); | ||
|
||
return designs.reduce((agg, design) => { | ||
const combos = testDesign(patterns, design); | ||
agg.possibleDesigns += combos > 0 ? 1 : 0; | ||
agg.possibleDesignCombos += combos; | ||
return agg; | ||
}, { possibleDesigns: 0, possibleDesignCombos: 0 }); | ||
} | ||
|
||
if (import.meta.main) { | ||
const startTime = performance.now(); | ||
console.log(await main()); | ||
console.log(`Elapsed: ${Math.round(performance.now() - startTime)}ms`); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
r, wr, b, g, bwu, rb, gb, br | ||
|
||
brwrr | ||
bggr | ||
gbbr | ||
rrbgbr | ||
ubwu | ||
bwurrg | ||
brgr | ||
bbrgwb |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { assertEquals } from "@std/assert"; | ||
import { main } from "./main.ts"; | ||
|
||
const target = "sample"; | ||
|
||
Deno.test(`correct possible design count for ${target}`, async () => { | ||
const result = await main(target); | ||
assertEquals(result.possibleDesigns, 6); | ||
}); | ||
|
||
Deno.test(`correct possible design combos for ${target}`, async () => { | ||
const result = await main(target); | ||
assertEquals(result.possibleDesignCombos, 16); | ||
}); |