Skip to content

Commit

Permalink
day 13
Browse files Browse the repository at this point in the history
  • Loading branch information
andipaetzold committed Dec 13, 2023
1 parent 9856d16 commit 92fb6df
Show file tree
Hide file tree
Showing 6 changed files with 2,774 additions and 0 deletions.
67 changes: 67 additions & 0 deletions 13a/index.mjs
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]));
}
15 changes: 15 additions & 0 deletions 13a/input0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.

#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#
Loading

0 comments on commit 92fb6df

Please sign in to comment.