-
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
e2d2bec
commit 3485e7e
Showing
4 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
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,25 @@ | ||
import { expect, describe, test } from "bun:test"; | ||
import { part1, part2 } from "."; | ||
import { getInputs } from "../../utils/get-inputs"; | ||
|
||
const { exampleInput, puzzleInput } = await getInputs("2024/19"); | ||
|
||
describe("part 1", () => { | ||
test("example", () => { | ||
expect(part1(exampleInput)).toBe(6); | ||
}); | ||
|
||
test("puzzle", () => { | ||
expect(part1(puzzleInput)).toBe(358); | ||
}); | ||
}); | ||
|
||
describe("part 2", () => { | ||
test("example", () => { | ||
expect(part2(exampleInput)).toBe(16); | ||
}); | ||
|
||
test("puzzle", () => { | ||
expect(part2(puzzleInput)).toBe(600639829400603); | ||
}); | ||
}); |
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,78 @@ | ||
import { timePart1, timePart2 } from "../../utils/time-part"; | ||
|
||
const parseInput = (input: string) => { | ||
const [availableTowels, designs] = input.split("\n\n"); | ||
|
||
return { | ||
availableTowels: availableTowels.split(", "), | ||
designs: designs.split("\n"), | ||
}; | ||
}; | ||
|
||
export const part1 = timePart1((input: string) => { | ||
const { availableTowels, designs } = parseInput(input); | ||
let possibleDesigns = 0; | ||
|
||
for (const design of designs) { | ||
let queue: Array<{ remaining: string }> = []; | ||
|
||
for (const towel of availableTowels) { | ||
if (design.indexOf(towel) === 0) { | ||
queue.push({ remaining: design.substring(towel.length) }); | ||
} | ||
} | ||
|
||
while (queue.length) { | ||
queue.sort((a, b) => a.remaining.length - b.remaining.length); | ||
const entry = queue.shift()!; | ||
|
||
for (const towel of availableTowels) { | ||
if (entry.remaining.indexOf(towel) === 0) { | ||
if (entry.remaining.length === towel.length) { | ||
possibleDesigns++; | ||
queue = []; | ||
break; | ||
} | ||
|
||
queue.push({ remaining: entry.remaining.substring(towel.length) }); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return possibleDesigns; | ||
}); | ||
|
||
export const part2 = timePart2((input: string) => { | ||
const { availableTowels, designs } = parseInput(input); | ||
let possibleDesigns = 0; | ||
const canBeCompleted = new Map<string, number>(); | ||
|
||
const countTowelVariants = (remaining: string) => { | ||
if (canBeCompleted.has(remaining)) { | ||
return canBeCompleted.get(remaining)!; | ||
} | ||
|
||
let variants = 0; | ||
|
||
for (const towel of availableTowels) { | ||
if (remaining.indexOf(towel) === 0) { | ||
if (remaining.length === towel.length) { | ||
variants++; | ||
continue; | ||
} | ||
|
||
variants += countTowelVariants(remaining.substring(towel.length)); | ||
} | ||
} | ||
|
||
canBeCompleted.set(remaining, variants); | ||
return variants; | ||
}; | ||
|
||
for (const design of designs) { | ||
possibleDesigns += countTowelVariants(design); | ||
} | ||
|
||
return possibleDesigns; | ||
}); |
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