-
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
Showing
10 changed files
with
191 additions
and
8 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 |
---|---|---|
|
@@ -6,7 +6,7 @@ import { | |
words, | ||
mapToInt, | ||
sort, | ||
self, | ||
self, | ||
} from '@helper'; | ||
|
||
const input = await loadData(); | ||
|
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,51 @@ | ||
import { applyOffset, Coordinate } from '@helper'; | ||
import { ORTHONAL_OFFSETS } from '@constants'; | ||
|
||
export type Direction = 'up' | 'down' | 'right' | 'left'; | ||
|
||
export const nextDirection = (direction: Direction): Direction => { | ||
const mappings: Record<Direction, Direction> = { | ||
up: 'right', | ||
right: 'down', | ||
down: 'left', | ||
left: 'up', | ||
}; | ||
return mappings[direction]; | ||
}; | ||
|
||
export const offsetForDirection = (direction: Direction) => | ||
({ | ||
up: ORTHONAL_OFFSETS.bottom, | ||
down: ORTHONAL_OFFSETS.top, | ||
left: ORTHONAL_OFFSETS.left, | ||
right: ORTHONAL_OFFSETS.right, | ||
})[direction]; | ||
|
||
export const walk = ( | ||
matrix: string[][], | ||
current: Coordinate, | ||
currentDirection: Direction, | ||
): [Coordinate, Direction][] => { | ||
const potentialStep = applyOffset( | ||
current, | ||
offsetForDirection(currentDirection), | ||
); | ||
const valueAtNextStep = (matrix[potentialStep.row] ?? [])[potentialStep.col]; | ||
|
||
if (!valueAtNextStep) { | ||
return [[current, currentDirection]]; | ||
} | ||
|
||
const direction = | ||
valueAtNextStep === '#' | ||
? nextDirection(currentDirection) | ||
: currentDirection; | ||
|
||
const walked = walk( | ||
matrix, | ||
valueAtNextStep === '#' ? current : potentialStep, | ||
direction, | ||
); | ||
|
||
return [[current, direction], ...walked]; | ||
}; |
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,31 @@ | ||
import { | ||
loadData, | ||
findItemCoordinates, | ||
lines, | ||
uniqueBy, | ||
setValueAtCord, | ||
} from '@helper'; | ||
import { walk } from './lib'; | ||
|
||
const input = await loadData(); | ||
const matrix = lines(input, (l) => l.split('') as ('#' | '.' | '^')[]); | ||
|
||
const uniqueCoords = uniqueBy( | ||
walk(matrix, findItemCoordinates(matrix, '^')!, 'up'), | ||
([a], [b]) => a.col === b.col && a.row === b.row, | ||
); | ||
|
||
if (Bun.env.DATA === 'debug') { | ||
const stringMatrix = uniqueCoords.reduce<string[][]>( | ||
(acc, [coord, dir]) => | ||
setValueAtCord( | ||
acc, | ||
coord, | ||
{ up: '↑', down: '↓', left: '←', right: '→' }[dir], | ||
), | ||
matrix, | ||
); | ||
console.log(stringMatrix.map((line) => line.join('')).join('\n')); | ||
} | ||
|
||
console.log(uniqueCoords.length); |
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,76 @@ | ||
import { | ||
loadData, | ||
applyOffset, | ||
Coordinate, | ||
findItemCoordinates, | ||
lines, | ||
setValueAtCord, | ||
uniqueBy, | ||
} from '@helper'; | ||
import { nextDirection, offsetForDirection, walk } from './lib'; | ||
|
||
type Direction = 'up' | 'down' | 'right' | 'left'; | ||
|
||
const input = await loadData(); | ||
const matrix = lines(input, (l) => l.split('') as ('#' | '.' | '^')[]); | ||
|
||
const hasLoop = ( | ||
matrix: string[][], | ||
current: Coordinate, | ||
currentDirection: Direction, | ||
visited = new Set<string>(), | ||
): boolean => { | ||
const state = `${current.row},${current.col},${currentDirection}`; | ||
if (visited.has(state)) { | ||
return true; | ||
} | ||
visited.add(state); | ||
|
||
const potentialStep = applyOffset( | ||
current, | ||
offsetForDirection(currentDirection), | ||
); | ||
const valueAtNextStep = (matrix[potentialStep.row] ?? [])[potentialStep.col]; | ||
|
||
if (!valueAtNextStep) { | ||
return false; | ||
} | ||
|
||
const direction = | ||
valueAtNextStep === '#' | ||
? nextDirection(currentDirection) | ||
: currentDirection; | ||
|
||
return hasLoop( | ||
matrix, | ||
valueAtNextStep === '#' ? current : potentialStep, | ||
direction, | ||
visited, | ||
); | ||
}; | ||
|
||
const obstacleCoordinates = uniqueBy( | ||
walk(matrix, findItemCoordinates(matrix, '^')!, 'up').map( | ||
([coord, direction]) => { | ||
return applyOffset(coord, offsetForDirection(direction)); | ||
}, | ||
), | ||
(a, b) => a.col === b.col && a.row === b.row, | ||
); | ||
|
||
const loopingMatrixes = obstacleCoordinates.filter((obstacleCord) => { | ||
try { | ||
const newMatrix = setValueAtCord(matrix, obstacleCord, '#'); | ||
|
||
const loopFound = hasLoop( | ||
newMatrix, | ||
findItemCoordinates(newMatrix, '^')!, | ||
'up', | ||
); | ||
return loopFound; | ||
} catch { | ||
return false; | ||
} | ||
}); | ||
|
||
console.log(loopingMatrixes.length); |
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
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
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
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,10 @@ | ||
....#..... | ||
.........# | ||
.......... | ||
..#....... | ||
.......#.. | ||
.......... | ||
.#..^..... | ||
........#. | ||
#......... | ||
......#... |
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
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 |
---|---|---|
@@ -1,7 +1,13 @@ | ||
import { expect, test } from 'bun:test'; | ||
import { unique } from '@helper'; | ||
import { unique, uniqueBy } from '@helper'; | ||
|
||
test('sum', () => { | ||
test('unique', () => { | ||
expect(unique([1, 1, 1, 1, 1, 1, 2])).toEqual([1, 2]); | ||
expect(unique([])).toEqual([]); | ||
}); | ||
|
||
test('uniqueBy', () => { | ||
expect( | ||
uniqueBy([{ a: 1 }, { a: 2 }, { a: 3 }, { a: 1 }], (a, b) => a.a === b.a), | ||
).toEqual([{ a: 1 }, { a: 2 }, { a: 3 }]); | ||
}); |