Skip to content

Commit

Permalink
solve day 13
Browse files Browse the repository at this point in the history
  • Loading branch information
shahata committed Dec 13, 2023
1 parent 2d9cb2f commit f189093
Show file tree
Hide file tree
Showing 3 changed files with 1,515 additions and 0 deletions.
83 changes: 83 additions & 0 deletions src/2023/day13.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
function diff(a, b) {
let result = 0;
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) result++;
}
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;
}
}
return a === b;
}

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;
}
}
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;
}

export function part1(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);
if (row > 0) {
result += row * 100;
continue;
}
patch = rotate(patch);
const col = getRowMirror(patch);
result += row * 100 + col;
}
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; //?
}
66 changes: 66 additions & 0 deletions src/2023/day13.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { part1, part2 } from './day13.js';
import readInput from '../utils/read-input.js';

const input = readInput(import.meta.url);

describe('day13 2023', () => {
describe('part1', () => {
it('should work for part 1 examples', () => {
expect(
part1(
[
'#.##..##.',
'..#.##.#.',
'##......#',
'##......#',
'..#.##.#.',
'..##..##.',
'#.#.##.#.',
'',
'#...##..#',
'#....#..#',
'..##..###',
'#####.##.',
'#####.##.',
'..##..###',
'#....#..#',
].join('\n'),
),
).toEqual(405);
});

it('should work for part 1 input', () => {
expect(part1(input)).toEqual(37025);
});
});

describe('part2', () => {
it('should work for part 2 examples', () => {
expect(
part2(
[
'#.##..##.',
'..#.##.#.',
'##......#',
'##......#',
'..#.##.#.',
'..##..##.',
'#.#.##.#.',
'',
'#...##..#',
'#....#..#',
'..##..###',
'#####.##.',
'#####.##.',
'..##..###',
'#....#..#',
].join('\n'),
),
).toEqual(400);
});

it('should work for part 2 input', () => {
expect(part2(input)).toEqual(32854);
});
});
});
Loading

0 comments on commit f189093

Please sign in to comment.