diff --git a/day-6/day6.ts b/day-6/day6.ts index ff68e37..ce49e64 100644 --- a/day-6/day6.ts +++ b/day-6/day6.ts @@ -26,13 +26,7 @@ function getNextCellCoordinates( return [x, y + 1]; } -function isPathFinished( - coordinates: Coordinates, - height: number, - width: number, -) { - const [x, y] = coordinates; - +function isPathFinished(x: number, y: number, height: number, width: number) { if (x === -1 || y === -1 || x === height || y === width) { return true; } @@ -62,7 +56,7 @@ export function day6(input: string) { let startCoords: Coordinates | null = null; const path: Coordinates[] = []; - for (let i = 0; i < grid.length; i++) { + outer: for (let i = 0; i < grid.length; i++) { const line = grid[i]; for (let j = 0; j < line.length; j++) { const cell = line[j]; @@ -70,7 +64,7 @@ export function day6(input: string) { continue; } startCoords = [i, j]; - break; + break outer; } } @@ -88,7 +82,7 @@ export function day6(input: string) { throw new Error("never"); } const [nextX, nextY] = getNextCellCoordinates(prev, direction); - isFinished = isPathFinished([nextX, nextY], HEIGHT, WIDTH); + isFinished = isPathFinished(nextX, nextY, HEIGHT, WIDTH); if (isFinished) { break; } @@ -112,6 +106,24 @@ export function day6(input: string) { export function day6part2(input: string) { let count = 0; const grid = input.split("\n").map((line) => line.split("")); + const h = grid.length; + const w = grid[0].length; + let startCoords: Coordinates | null = null; + outer: for (let i = 0; i < grid.length; i++) { + const line = grid[i]; + for (let j = 0; j < line.length; j++) { + const cell = line[j]; + if (cell !== "^") { + continue; + } + startCoords = [i, j]; + break outer; + } + } + + if (!startCoords) { + throw new Error("no starting point"); + } for (let i = 0; i < grid.length; i++) { const line = grid[i]; @@ -122,53 +134,30 @@ export function day6part2(input: string) { continue; } newGrid[i][j] = "#"; - const isLoop = checkInput(newGrid); + const isLoop = checkInput(newGrid, startCoords, h, w); if (isLoop) count++; } } + return count; } -export function checkInput(grid: Grid) { - const HEIGHT = grid.length; - const WIDTH = grid[0].length; - +export function checkInput( + grid: Grid, + startCoords: Coordinates, + h: number, + w: number, +) { let direction = Direction.UP; - let startCoords: Coordinates | null = null; - const path: Coordinates[] = []; const seen = new Set(); - for (let i = 0; i < grid.length; i++) { - const line = grid[i]; - for (let j = 0; j < line.length; j++) { - const cell = line[j]; - if (cell !== "^") { - continue; - } - startCoords = [i, j]; - break; - } - } - - if (!startCoords) { - throw new Error("no starting point"); - } - - path.push(startCoords); const s = sc(startCoords, direction); seen.add(s); - let isFinished = false; - - while (!isFinished) { - const prev = path.at(-1); - if (!prev) { - throw new Error("never"); - } + let prev = startCoords; + while (true) { const [nextX, nextY] = getNextCellCoordinates(prev, direction); - isFinished = isPathFinished([nextX, nextY], HEIGHT, WIDTH); + const isFinished = isPathFinished(nextX, nextY, h, w); if (isFinished) { - isFinished = true; - break; } @@ -184,7 +173,7 @@ export function checkInput(grid: Grid) { return true; } seen.add(s); - path.push([nextX, nextY]); + prev = [nextX, nextY]; } } }