Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
shahata committed Dec 13, 2023
1 parent f189093 commit 3d6e526
Showing 1 changed file with 21 additions and 63 deletions.
84 changes: 21 additions & 63 deletions src/2023/day13.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,41 @@
function diff(a, b) {
let result = 0;
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) result++;
function equals(a, b, state) {
const x = a.split('').filter((x, i) => x !== b[i]).length;
if (x <= state.smudges) {
state.smudges -= x;
return true;
}
return result;
}

function equals(a, b, state) {
if (state.smudges > 0) {
const x = diff(a, b);
if (x <= state.smudges) {
state.smudges -= x;
return true;
}
function isMirror(rows, i, smudges) {
const state = { smudges };
for (let j = 0; i - j >= 0 && i + j + 1 < rows.length; j++) {
if (!equals(rows[i - j], rows[i + j + 1], state)) return false;
}
return a === b;
return state.smudges === 0;
}

function getRowMirror(patch, smudges = 0) {
const rows = patch.map(row => row.join(''));
for (let i = 0; i < rows.length; i++) {
const state = { smudges };
let mirror = false;
if (i + 1 < rows.length && equals(rows[i], rows[i + 1], state)) {
mirror = true;
for (let j = 1; i - j >= 0 && i + j + 1 < rows.length; j++) {
if (!equals(rows[i - j], rows[i + j + 1], state)) {
mirror = false;
}
}
}
if (mirror && state.smudges === 0) {
return i + 1;
}
function getRowMirror(rows, smudges = 0) {
for (let i = 0; i < rows.length - 1; i++) {
if (isMirror(rows, i, smudges)) return i + 1;
}
return 0;
}

function rotate(patch) {
const result = [];
for (let i = 0; i < patch[0].length; i++) {
const row = patch.map(row => row[i]);
result.push(row);
}
return result;
const result = new Array(patch[0].length).fill();
return result.map((x, i) => patch.map(row => row[i]).join(''));
}

export function part1(input) {
const patches = input
.split('\n\n')
.map(patch => patch.split('\n').map(row => row.split('')));
export function part1(input, smudges = 0) {
const patches = input.split('\n\n').map(patch => patch.split('\n'));
let result = 0;
for (let patch of patches) {
const row = getRowMirror(patch);
if (row > 0) {
result += row * 100;
continue;
}
patch = rotate(patch);
const col = getRowMirror(patch);
result += row * 100 + col;
const row = getRowMirror(patch, smudges);
if (row) result += row * 100;
else result += getRowMirror(rotate(patch), smudges);
}
return result;
}

export function part2(input) {
const patches = input
.split('\n\n')
.map(patch => patch.split('\n').map(row => row.split('')));
let result = 0;
for (let patch of patches) {
const row = getRowMirror(patch, 1);
if (row > 0) {
result += row * 100;
continue;
}
patch = rotate(patch);
const col = getRowMirror(patch, 1);
result += col;
}
return result; //?
return part1(input, 1);
}

0 comments on commit 3d6e526

Please sign in to comment.