mirror of
https://github.com/ershisan99/advent-of-code.git
synced 2025-12-16 20:49:24 +00:00
refactor day 6
This commit is contained in:
@@ -26,13 +26,7 @@ function getNextCellCoordinates(
|
|||||||
return [x, y + 1];
|
return [x, y + 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
function isPathFinished(
|
function isPathFinished(x: number, y: number, height: number, width: number) {
|
||||||
coordinates: Coordinates,
|
|
||||||
height: number,
|
|
||||||
width: number,
|
|
||||||
) {
|
|
||||||
const [x, y] = coordinates;
|
|
||||||
|
|
||||||
if (x === -1 || y === -1 || x === height || y === width) {
|
if (x === -1 || y === -1 || x === height || y === width) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -62,7 +56,7 @@ export function day6(input: string) {
|
|||||||
let startCoords: Coordinates | null = null;
|
let startCoords: Coordinates | null = null;
|
||||||
const path: Coordinates[] = [];
|
const path: Coordinates[] = [];
|
||||||
|
|
||||||
for (let i = 0; i < grid.length; i++) {
|
outer: for (let i = 0; i < grid.length; i++) {
|
||||||
const line = grid[i];
|
const line = grid[i];
|
||||||
for (let j = 0; j < line.length; j++) {
|
for (let j = 0; j < line.length; j++) {
|
||||||
const cell = line[j];
|
const cell = line[j];
|
||||||
@@ -70,7 +64,7 @@ export function day6(input: string) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
startCoords = [i, j];
|
startCoords = [i, j];
|
||||||
break;
|
break outer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,7 +82,7 @@ export function day6(input: string) {
|
|||||||
throw new Error("never");
|
throw new Error("never");
|
||||||
}
|
}
|
||||||
const [nextX, nextY] = getNextCellCoordinates(prev, direction);
|
const [nextX, nextY] = getNextCellCoordinates(prev, direction);
|
||||||
isFinished = isPathFinished([nextX, nextY], HEIGHT, WIDTH);
|
isFinished = isPathFinished(nextX, nextY, HEIGHT, WIDTH);
|
||||||
if (isFinished) {
|
if (isFinished) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -112,6 +106,24 @@ export function day6(input: string) {
|
|||||||
export function day6part2(input: string) {
|
export function day6part2(input: string) {
|
||||||
let count = 0;
|
let count = 0;
|
||||||
const grid = input.split("\n").map((line) => line.split(""));
|
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++) {
|
for (let i = 0; i < grid.length; i++) {
|
||||||
const line = grid[i];
|
const line = grid[i];
|
||||||
@@ -122,53 +134,30 @@ export function day6part2(input: string) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
newGrid[i][j] = "#";
|
newGrid[i][j] = "#";
|
||||||
const isLoop = checkInput(newGrid);
|
const isLoop = checkInput(newGrid, startCoords, h, w);
|
||||||
if (isLoop) count++;
|
if (isLoop) count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function checkInput(grid: Grid) {
|
export function checkInput(
|
||||||
const HEIGHT = grid.length;
|
grid: Grid,
|
||||||
const WIDTH = grid[0].length;
|
startCoords: Coordinates,
|
||||||
|
h: number,
|
||||||
|
w: number,
|
||||||
|
) {
|
||||||
let direction = Direction.UP;
|
let direction = Direction.UP;
|
||||||
let startCoords: Coordinates | null = null;
|
|
||||||
const path: Coordinates[] = [];
|
|
||||||
const seen = new Set();
|
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);
|
const s = sc(startCoords, direction);
|
||||||
seen.add(s);
|
seen.add(s);
|
||||||
let isFinished = false;
|
let prev = startCoords;
|
||||||
|
while (true) {
|
||||||
while (!isFinished) {
|
|
||||||
const prev = path.at(-1);
|
|
||||||
if (!prev) {
|
|
||||||
throw new Error("never");
|
|
||||||
}
|
|
||||||
const [nextX, nextY] = getNextCellCoordinates(prev, direction);
|
const [nextX, nextY] = getNextCellCoordinates(prev, direction);
|
||||||
isFinished = isPathFinished([nextX, nextY], HEIGHT, WIDTH);
|
const isFinished = isPathFinished(nextX, nextY, h, w);
|
||||||
if (isFinished) {
|
if (isFinished) {
|
||||||
isFinished = true;
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,7 +173,7 @@ export function checkInput(grid: Grid) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
seen.add(s);
|
seen.add(s);
|
||||||
path.push([nextX, nextY]);
|
prev = [nextX, nextY];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user