-
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
Showing
3 changed files
with
96 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,29 @@ | ||
export default function solve(input: string) { | ||
const locks: number[][] = []; | ||
const keys: number[][] = []; | ||
for (const lines of input.split("\n\n")) { | ||
const rows = lines.split("\n").slice(1, -1); | ||
const heights = Array.from(rows[0], () => 0); | ||
if (lines.startsWith(".")) rows.reverse(); | ||
for (let x = 0; x < rows[0].length; x++) { | ||
for (let height = rows.length; height > 0; height--) { | ||
if (rows[height - 1][x] === ".") continue; | ||
heights[x] = height; | ||
break; | ||
} | ||
} | ||
if (lines.startsWith("#")) locks.push(heights); | ||
else keys.push(heights); | ||
} | ||
let count = 0; | ||
for (const lock of locks) { | ||
for (const key of keys) { | ||
if ( | ||
key.every((height, index, { length: max }) => | ||
height + lock[index] <= max | ||
) | ||
) count++; | ||
} | ||
} | ||
return count; | ||
} |
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,48 @@ | ||
import solve from "./solve.ts"; | ||
|
||
import { assertEquals } from "@std/assert"; | ||
|
||
Deno.test("example", () => { | ||
const input = `\ | ||
##### | ||
.#### | ||
.#### | ||
.#### | ||
.#.#. | ||
.#... | ||
..... | ||
##### | ||
##.## | ||
.#.## | ||
...## | ||
...#. | ||
...#. | ||
..... | ||
..... | ||
#.... | ||
#.... | ||
#...# | ||
#.#.# | ||
#.### | ||
##### | ||
..... | ||
..... | ||
#.#.. | ||
###.. | ||
###.# | ||
###.# | ||
##### | ||
..... | ||
..... | ||
..... | ||
#.... | ||
#.#.. | ||
#.#.# | ||
#####`; | ||
|
||
assertEquals(solve(input), 3); | ||
}); |
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,19 @@ | ||
export default function solve(input: string, { size = 5 } = {}) { | ||
const locks: number[][] = []; | ||
const keys: number[][] = []; | ||
for (const schematic of input.split("\n\n")) { | ||
const rows = schematic.split("\n").slice(1, -1); | ||
const heights = Array.from( | ||
{ length: size }, | ||
(_, x) => rows.reduce((height, row) => height + +(row[x] === "#"), 0), | ||
); | ||
(schematic[0] === "#" ? locks : keys).push(heights); | ||
} | ||
let count = 0; | ||
for (const lock of locks) { | ||
for (const key of keys) { | ||
if (key.every((height, index) => height + lock[index] <= size)) count++; | ||
} | ||
} | ||
return count; | ||
} |