-
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
9856d16
commit 92fb6df
Showing
6 changed files
with
2,774 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,67 @@ | ||
import { readFileSync } from "node:fs"; | ||
|
||
const input = readFileSync("input0.txt", "utf-8"); | ||
const patterns = input.split("\n\n"); | ||
|
||
let result = 0; | ||
for (const pattern of patterns) { | ||
const lines = pattern.split("\n").map((line) => line.split("")); | ||
|
||
const height = lines.length; | ||
const width = lines[0].length; | ||
|
||
const horizontalReflections = getReflections(lines); | ||
const fullHorizontalReflections = Object.entries( | ||
horizontalReflections | ||
).filter(([_, count]) => count === height); | ||
|
||
for (const horizontalReflection of fullHorizontalReflections) { | ||
result += +horizontalReflection[0]; | ||
} | ||
|
||
const verticalReflections = getReflections(transpose(lines)); | ||
const fullVerticalReflections = Object.entries(verticalReflections).filter( | ||
([_, count]) => count === width | ||
); | ||
|
||
for (const verticalReflection of fullVerticalReflections) { | ||
result += +verticalReflection[0] * 100; | ||
} | ||
} | ||
|
||
console.log(result); | ||
|
||
function getReflections(lines) { | ||
const height = lines.length; | ||
const width = lines[0].length; | ||
|
||
const reflections = {}; | ||
for (let y = 0; y < height; y++) { | ||
const line = lines[y]; | ||
|
||
for (let x = 1; x < width; ++x) { | ||
const left = line.slice(0, x); | ||
const right = line.slice(x); | ||
|
||
const minLength = Math.min(left.length, right.length); | ||
|
||
const leftTrimmed = left.slice(-minLength); | ||
const rightTrimmed = right.slice(0, minLength).reverse(); | ||
|
||
if (isEqual(leftTrimmed, rightTrimmed)) { | ||
reflections[x] ??= 0; | ||
reflections[x]++; | ||
} | ||
} | ||
} | ||
|
||
return reflections; | ||
} | ||
|
||
function isEqual(a, b) { | ||
return a.length === b.length && a.every((c, i) => c === b[i]); | ||
} | ||
|
||
function transpose(arr) { | ||
return arr[0].map((_, colIndex) => arr.map((row) => row[colIndex])); | ||
} |
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,15 @@ | ||
#.##..##. | ||
..#.##.#. | ||
##......# | ||
##......# | ||
..#.##.#. | ||
..##..##. | ||
#.#.##.#. | ||
|
||
#...##..# | ||
#....#..# | ||
..##..### | ||
#####.##. | ||
#####.##. | ||
..##..### | ||
#....#..# |
Oops, something went wrong.