-
Notifications
You must be signed in to change notification settings - Fork 8
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
1,515 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,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; //? | ||
} |
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,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); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.