mirror of
https://github.com/ershisan99/advent-of-code.git
synced 2025-12-16 12:32:49 +00:00
day 6
This commit is contained in:
37
day-6/day6.test.ts
Normal file
37
day-6/day6.test.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { expect, test } from "bun:test";
|
||||
import * as path from "node:path";
|
||||
import { day6, day6part2 } from "./day6.ts";
|
||||
|
||||
test("day 6, part 1", async () => {
|
||||
const testInput = await Bun.file(
|
||||
path.resolve(__dirname, "test-input.txt"),
|
||||
).text();
|
||||
const input = await Bun.file(path.resolve(__dirname, "input.txt")).text();
|
||||
|
||||
const testResult = day6(testInput);
|
||||
const finalResult = day6(input);
|
||||
console.log("\n\n");
|
||||
console.log("Test data:", testResult);
|
||||
console.log("Full data:", finalResult);
|
||||
console.log("\n\n");
|
||||
|
||||
expect(testResult).toEqual(41);
|
||||
expect(finalResult).toEqual(5153);
|
||||
});
|
||||
|
||||
test("day 6, part 2", async () => {
|
||||
const testInput = await Bun.file(
|
||||
path.resolve(__dirname, "test-input.txt"),
|
||||
).text();
|
||||
const input = await Bun.file(path.resolve(__dirname, "input.txt")).text();
|
||||
|
||||
const testResult = day6part2(testInput);
|
||||
console.log("\n\n");
|
||||
console.log("Test data:", testResult);
|
||||
expect(testResult).toEqual(6);
|
||||
const finalResult = day6part2(input);
|
||||
console.log("Full data:", finalResult);
|
||||
expect(finalResult).toEqual(1711);
|
||||
|
||||
console.log("\n\n");
|
||||
});
|
||||
194
day-6/day6.ts
Normal file
194
day-6/day6.ts
Normal file
@@ -0,0 +1,194 @@
|
||||
enum Direction {
|
||||
UP = 0,
|
||||
DOWN = 1,
|
||||
LEFT = 2,
|
||||
RIGHT = 3,
|
||||
}
|
||||
|
||||
type Coordinates = [number, number];
|
||||
|
||||
type Grid = string[][];
|
||||
|
||||
function getNextCellCoordinates(
|
||||
coordinates: Coordinates,
|
||||
direction: Direction,
|
||||
): Coordinates {
|
||||
const [x, y] = coordinates;
|
||||
if (direction === Direction.UP) {
|
||||
return [x - 1, y];
|
||||
}
|
||||
if (direction === Direction.DOWN) {
|
||||
return [x + 1, y];
|
||||
}
|
||||
if (direction === Direction.LEFT) {
|
||||
return [x, y - 1];
|
||||
}
|
||||
return [x, y + 1];
|
||||
}
|
||||
|
||||
function isPathFinished(
|
||||
coordinates: Coordinates,
|
||||
height: number,
|
||||
width: number,
|
||||
) {
|
||||
const [x, y] = coordinates;
|
||||
|
||||
if (x === -1 || y === -1 || x === height || y === width) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function getNewDirection(prev: Direction) {
|
||||
switch (prev) {
|
||||
case Direction.LEFT:
|
||||
return Direction.UP;
|
||||
case Direction.RIGHT:
|
||||
return Direction.DOWN;
|
||||
case Direction.DOWN:
|
||||
return Direction.LEFT;
|
||||
case Direction.UP:
|
||||
return Direction.RIGHT;
|
||||
}
|
||||
}
|
||||
|
||||
export function day6(input: string) {
|
||||
const grid = input.split("\n").map((line) => line.split(""));
|
||||
const HEIGHT = grid.length;
|
||||
const WIDTH = grid[0].length;
|
||||
|
||||
let direction = Direction.UP;
|
||||
let startCoords: Coordinates | null = null;
|
||||
const path: Coordinates[] = [];
|
||||
|
||||
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);
|
||||
|
||||
let isFinished = false;
|
||||
|
||||
while (!isFinished) {
|
||||
const prev = path.at(-1);
|
||||
if (!prev) {
|
||||
throw new Error("never");
|
||||
}
|
||||
const [nextX, nextY] = getNextCellCoordinates(prev, direction);
|
||||
isFinished = isPathFinished([nextX, nextY], HEIGHT, WIDTH);
|
||||
if (isFinished) {
|
||||
break;
|
||||
}
|
||||
|
||||
const nextCellContent = grid[nextX][nextY];
|
||||
|
||||
if (nextCellContent === "#") {
|
||||
direction = getNewDirection(direction);
|
||||
} else {
|
||||
path.push([nextX, nextY]);
|
||||
}
|
||||
}
|
||||
|
||||
const filtered = path.filter(
|
||||
(o, index, arr) =>
|
||||
arr.findIndex((item) => item[0] === o[0] && item[1] === o[1]) === index,
|
||||
);
|
||||
return filtered.length;
|
||||
}
|
||||
|
||||
export function day6part2(input: string) {
|
||||
let count = 0;
|
||||
const grid = input.split("\n").map((line) => line.split(""));
|
||||
|
||||
for (let i = 0; i < grid.length; i++) {
|
||||
const line = grid[i];
|
||||
for (let j = 0; j < line.length; j++) {
|
||||
const newGrid = structuredClone(grid);
|
||||
const currentValue = newGrid[i][j];
|
||||
if (currentValue === "#" || currentValue === "^") {
|
||||
continue;
|
||||
}
|
||||
newGrid[i][j] = "#";
|
||||
const isLoop = checkInput(newGrid);
|
||||
if (isLoop) count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
export function checkInput(grid: Grid) {
|
||||
const HEIGHT = grid.length;
|
||||
const WIDTH = grid[0].length;
|
||||
|
||||
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");
|
||||
}
|
||||
const [nextX, nextY] = getNextCellCoordinates(prev, direction);
|
||||
isFinished = isPathFinished([nextX, nextY], HEIGHT, WIDTH);
|
||||
if (isFinished) {
|
||||
isFinished = true;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
const nextCellContent = grid[nextX][nextY];
|
||||
|
||||
if (nextCellContent === "#") {
|
||||
direction = getNewDirection(direction);
|
||||
} else {
|
||||
const s = sc([nextX, nextY], direction);
|
||||
const x = seen.has(s);
|
||||
|
||||
if (x) {
|
||||
return true;
|
||||
}
|
||||
seen.add(s);
|
||||
path.push([nextX, nextY]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function sc(c: Coordinates, dir: Direction) {
|
||||
return [...c, dir].join(",");
|
||||
}
|
||||
130
day-6/input.txt
Normal file
130
day-6/input.txt
Normal file
@@ -0,0 +1,130 @@
|
||||
.......................#...................#....#..#......#.................#.#..#........................#.#..............#.....#
|
||||
................#.#.......#..#.......#.................................................................................#.##.......
|
||||
............#..............#......................#...#.....#...#..........##.........#.....#.....................................
|
||||
..#...#......................................#............................#..................#..........#..#......................
|
||||
....#........#.#....................#..........#.....#.............#..#............................#..................#......#....
|
||||
..........................................................#................#............................................#.........
|
||||
..#............................#.....#...........................#...........#........##...........................#..#...........
|
||||
.....................#...........................#.#..............#......................................#.........#....#.....#...
|
||||
.#...#.#.................#....................#...............................................................#.#..........#......
|
||||
....#....................................#...........................................#.....#..........#...........................
|
||||
.......................#..........#..............#......#....#.......#........................................#.#.................
|
||||
......#...........................................................#...#............#.................#............................
|
||||
.......#.................#..#.....................#...............................................................................
|
||||
..............#....................#.#..........#........#......#................#.................#..........#...................
|
||||
#.#.......................................#.........................#...................................#.........................
|
||||
...#.........................................................................................#................#..................#
|
||||
#.....#....#............................................................#.........................#..................#.#..........
|
||||
.#.....#.#.......#.#..................#.#......................#.......#.....#..#..................##.....#.......#...#...........
|
||||
....#.........#.................................#..........#........#...............#................#...................#....#...
|
||||
.................#...............#...#.................#.................................................#........................
|
||||
....#...#...............................#..................................................#..........#................#.....#....
|
||||
...........#........#....#.......#...#..........#...............#.....#.....#.....#..........#....................................
|
||||
............#.........#.................#................#.................#.#..................................................#.
|
||||
............#......................................#...................................................................#..........
|
||||
...........#....#.....................................................................................#............#..........#...
|
||||
..........#........................................#.............#.................#........................#..#.......#........#.
|
||||
......#.............................#.................##..................................#.....................#.................
|
||||
.#.................................#.............................................#.................##.....#..............#......#.
|
||||
.........................#..............................#..........#...........#....#................#......................#.....
|
||||
.......#...................#......##........................#.........#...........................................................
|
||||
......................................................................##...........#................#.............................
|
||||
....#...................................##...............................................#........................................
|
||||
...##.....#..#........#.................#..............................................................#..........................
|
||||
........##......#....................#..............................#......................#...................#..................
|
||||
.........................................................#.......................................................#.#..............
|
||||
..........#.............................#.........#...............#................#.#.........#..........................#.#.....
|
||||
....................#...................#..#.......................................................#..........#...............#...
|
||||
...........................................................#........................#.........#........#..........................
|
||||
.......#..............#......#.................................#...................##...........................#.........#.......
|
||||
#...#...............................................................................................................#.............
|
||||
................................#..#....#............#...........#..................#.............................#...............
|
||||
....................#..............#.........#.................................#.............#..##................................
|
||||
#...#..................................................................................#.....#...........#........................
|
||||
........#...........#........#...#..................#.....#....#.........#.........................#...........................#..
|
||||
.............#...#...................................................................#...........#................................
|
||||
.............#........................................#...#.............#..............................#..#.........#.............
|
||||
...#.................................................................................#.............#..........................##..
|
||||
...#..............................................................................................................................
|
||||
................#.........#..................................#...................#................................................
|
||||
...#................#...#.#.....................#...........#..................................#........#....#......#.............
|
||||
..........................#.....................#..#...................................##........................................#
|
||||
#........................#...............................................#........#..#..........#...........................#.....
|
||||
.#............#.#..............................................................#...........#......................................
|
||||
......#..................................................................#.......#..............................#...#....#........
|
||||
........#........................#.........#.............#.....#..................................................................
|
||||
...............................................................................#...........................................#......
|
||||
........................#..................................#.....................................................#................
|
||||
............................................................#..........................#.................#.......................#
|
||||
.....................#..#.........................#..............#.............................#........#..............##......#..
|
||||
.....................#.................................................#.......#............#........................#.......#....
|
||||
......................#.....................................................#......#............................#.................
|
||||
................................................................#...#..##..#.....................................#...............#
|
||||
..............#..............................................................................................#....................
|
||||
............................................#......#..........#...............................................................#...
|
||||
....#.................................#..................................................................#..........##..#.........
|
||||
............#.....#................................................................................#..........#.........#.........
|
||||
.....................#......................................#.........#....................................................#....#.
|
||||
.........................#...............................#...............................#....................#...................
|
||||
.#.....................#.#.......................................#....#...........#.......................#.......................
|
||||
.....................................................#.#..#......................................................................#
|
||||
#..........#..........................#............................#.............#..................#.............#...........#...
|
||||
........#.........................................................................................................................
|
||||
..........#.............#.........................................#................................................#..............
|
||||
...........#................................#.................#...........................#.............................#.........
|
||||
..................#.........................................#.#..................#...#...........#.......#..#.....................
|
||||
........................................................#...#.......................................................#.............
|
||||
.........................................................................#.......#..#.....#.......................................
|
||||
............#..............................................................#.........#..............#.............#...............
|
||||
#...#..........................................#...........#.....#.....................................#...............#..........
|
||||
...........................#............#.........................................................................................
|
||||
...#..........................................#.......................................................................#...........
|
||||
..............#......#.....#.....................#..........................#.....................................................
|
||||
.........................#....#...................#...#.......#..............................#........................#...........
|
||||
.............................#............^.#................................................#......#..#.#...........#............
|
||||
....................................................................................#...#.#...................................#...
|
||||
.......................#..............................##..............................................#...........................
|
||||
......................#...................................................#..........................................#............
|
||||
......#........................................................................#.....................................#............
|
||||
.......................#.............................#........................#...........#...........#............#...........#..
|
||||
....#...............#..#......#....#..................#.............#.......#...............#.......................#.............
|
||||
............#...#..................#..............................................................................................
|
||||
....#.......#.................#.......................................................#..#......#.................................
|
||||
.........#......#..........#......................#.....................#..................#.#....................................
|
||||
...............##.......................................................#...#.............................#.......#...............
|
||||
........................................................##...................#...#............#......#............................
|
||||
...........#...#..............#.............................................................#...................#............#....
|
||||
...............................................#.....#.....#......................................................................
|
||||
.#....................#...#........#....................#.#.........#..........#................................#................#
|
||||
.............#.....#..#....................................#.#...........#.................................................#..#.#.
|
||||
......#.....................................#....................................................................#................
|
||||
............................................................##......#.....................#.......#.....................#...#.....
|
||||
.#.......#......................#.................................................................................................
|
||||
................#....#......................#.........#...................#........#.......##.....#...............................
|
||||
..#....#....................#............................................................#.#......#............#.....#.#....#.....
|
||||
.................................#.................................................#.......#......................................
|
||||
..................#...........#....#.....................#......#...............#..................#....................##........
|
||||
.##...............................................#............#...........................#......................................
|
||||
.............#...........#...........................#..#.......#...#..........#......................#..........#................
|
||||
.......#...................................#.................#....#..............................................................#
|
||||
........#....................................................#.................#.....#.........#.#................................
|
||||
................#.................................#.....................#.....#.....#............................##...............
|
||||
..............................................##.................#..##........#...........#............#..........................
|
||||
....#......................................#...............................................................................#......
|
||||
..#..............#.#................#..#..............#.......................................#.#.......#..............#......#..#
|
||||
.....#..............................................................................................................#.......#.....
|
||||
..#.......#.#..........#.......#..................................#..........#.......#.................#...........#...........#..
|
||||
...............#.....#................#............#................#............#.....#.#........#...............................
|
||||
....#.....................#................#.....................................#......................#.#.......#....#..........
|
||||
........#...............#...#.....................................................#........#.........#.........#..................
|
||||
...#......#..........................#.....#..........................##.......#....#.............................................
|
||||
.............#................................#............................#.....#.......#......#................................#
|
||||
.......................#............#..........................#..................................................................
|
||||
.................................................#..............#...#...........................#.................................
|
||||
......#...................#..............................................................................................#....#..#
|
||||
....................................#......#.....................#..............#.......#...................#.....................
|
||||
............#...................................#...........#........#........#...........................................##......
|
||||
#...........#..........................#....................................#.........#................................#..........
|
||||
......##..#...............................................................#...............................#.........#.............
|
||||
.......................#.....#.........................#...#.........................#.....#..........#.#...#.....................
|
||||
.......................#..#.....#.....#..........................#.........#.....#....................#.....#....................#
|
||||
10
day-6/test-input.txt
Normal file
10
day-6/test-input.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
....#.....
|
||||
.........#
|
||||
..........
|
||||
..#.......
|
||||
.......#..
|
||||
..........
|
||||
.#..^.....
|
||||
........#.
|
||||
#.........
|
||||
......#...
|
||||
Reference in New Issue
Block a user