day 6
This commit is contained in:
+81
@@ -0,0 +1,81 @@
|
||||
import { open } from "node:fs/promises";
|
||||
|
||||
const file = await open("./2024/inputs/6");
|
||||
|
||||
const map: string[][] = []
|
||||
for await (const line of file.readLines()) {
|
||||
map.push(line.split(""))
|
||||
}
|
||||
|
||||
type Vector2 = {
|
||||
X: number,
|
||||
Y: number
|
||||
}
|
||||
|
||||
// 0: UP
|
||||
// 1: RIGHT
|
||||
// 2: DOWN
|
||||
// 3: LEFT
|
||||
|
||||
const directions: Vector2[] = [{
|
||||
X: 0,
|
||||
Y: -1
|
||||
},
|
||||
{
|
||||
X: 1,
|
||||
Y: 0
|
||||
},
|
||||
{
|
||||
X: 0,
|
||||
Y: 1
|
||||
},
|
||||
{
|
||||
X: -1,
|
||||
Y: 0
|
||||
}]
|
||||
|
||||
const height = map.length
|
||||
const width = map[0].length
|
||||
|
||||
let d = 0
|
||||
|
||||
const startY = map.findIndex(l => l.includes("^"))
|
||||
const startX = map[startY].indexOf("^")
|
||||
const position: Vector2 = {
|
||||
X: startX,
|
||||
Y: startY
|
||||
}
|
||||
|
||||
function getNextCoordinates(): Vector2 {
|
||||
return {
|
||||
X: position.X + directions[d].X,
|
||||
Y: position.Y + directions[d].Y
|
||||
}
|
||||
}
|
||||
|
||||
function getTile({ X, Y }: Vector2) {
|
||||
if (X < 0 || Y < 0 || X >= width || Y >= height) {
|
||||
return null
|
||||
}
|
||||
return map[Y][X]
|
||||
}
|
||||
|
||||
function rotate() {
|
||||
d = (d + 1) % 4
|
||||
}
|
||||
|
||||
function moveForward() {
|
||||
Object.assign(position, getNextCoordinates())
|
||||
}
|
||||
|
||||
while (getTile(getNextCoordinates())) {
|
||||
const tile = getTile(getNextCoordinates())
|
||||
if (tile == "#") {
|
||||
rotate()
|
||||
} else {
|
||||
moveForward()
|
||||
}
|
||||
map[position.Y][position.X] = "X"
|
||||
}
|
||||
|
||||
console.log(map.reduce((acc, l) => l.reduce((acc, t) => t == "X" ? acc + 1 : acc, 0) + acc, 0))
|
||||
+130
@@ -0,0 +1,130 @@
|
||||
.........................................#.....#..##.....................#...........................#.....................#....#.
|
||||
..................#................................#..#.................................#................#........................
|
||||
.....##.........#..#........#......#............#.....#..................#............................#...#...#..#...#............
|
||||
#..#...#....#...#.................................#.........................................#...............................#.....
|
||||
.................##...................................................#.........................#....................#............
|
||||
.............#.............#...................#...................#..................................#....#..#........#....#.....
|
||||
..#.........................................................................................##....#.................#.............
|
||||
...........#.........#.....#..#.................#...........##..........#....#....................................#........#.#....
|
||||
#...#.........................................#..........................................................#........................
|
||||
.............................................#................................#...............................##..........#.......
|
||||
......#....#..............#..........................................#....#........................#...................##....#....
|
||||
........#.#..............................#..#.............#.......#............................................................#..
|
||||
....#...............#...#..#......#...........................#........................#...........#...#...................#......
|
||||
..........................#...........##....#.......................................#..........................#.#................
|
||||
....................#.........................................................#........#..........................................
|
||||
..............#.............#.##.............#.........#..........#.......................................#...#.........#..#......
|
||||
...............................................................#..........#........#.........#.............#.............#........
|
||||
............#.................#...................#....#......................................................................#...
|
||||
.......#....##....#......##.........#...........................................#....................................#.........#..
|
||||
......#...#......................#............................##....##....................#.......................................
|
||||
...................................##....................#.............................#........................#.................
|
||||
..........................#.#.........................#..........................................................#................
|
||||
.............#...#..........................................................#..#.#.#.....#....#.....#...................#........#
|
||||
....#......#.............#...#......#.........#............##..................#.......................#..........................
|
||||
.......................#.....#...................................#..........................#.....................................
|
||||
#.....##....................................#.....#.....................................................#....#....................
|
||||
............#...#...................................................................#.........#......................#............
|
||||
................#....................................#.....#.........#.........#..............#...................................
|
||||
..........................................................................................#.....#.#...#.........#.......#.........
|
||||
#.............#..............................................#...#.#..#.....................#...#............#.#...#..............
|
||||
..........................................#..................................................................#...#................
|
||||
.........#.............................................#................................#.................#.....#................#
|
||||
........................................................................................#.......#.....#..........#..........#.....
|
||||
...........................#.......#......#..........#............#...............#........#...#......#...........................
|
||||
...##...................#.................................#..................................##...................................
|
||||
........................................................#.#.#.....#............................##................#................
|
||||
......................................#....................#..................#...#.............................#...........#.....
|
||||
.............................#...#.................#.............................#..............#.................................
|
||||
#.....................................................................................................................#..#.....#..
|
||||
...........................#..............................................#................................#....................#.
|
||||
......#..................##................#....................#.............................................................#..#
|
||||
.................#....................................................................................#...........................
|
||||
....#.#..................................#..#........................##...#.............................#..............#..........
|
||||
...............................................................................................................#...#.....#........
|
||||
..#...................#.......................#........................................................................#..........
|
||||
..#............#......................................###.........#..............................#..........#.................#...
|
||||
.................................#..........................................................................#............#........
|
||||
......................#...#...............#..............................#.......#........................#...........#........#..
|
||||
..#..................#............................................................................#......#........................
|
||||
..........................................................................#....#........#...................#.............#.......
|
||||
..............#.........#.#............................................................................................#..........
|
||||
........................#...#............................................#..............................#.........................
|
||||
.........................................#...................................................#........................#...........
|
||||
...#.................................................#..........#.............................#.....#.#.........#.................
|
||||
............................#...........#...............................................................#........................#
|
||||
..........................#..#..................#....................................#..................#.......................#.
|
||||
......#...................#..............................#........#.##..........................................................#.
|
||||
...........................#.....#.............................................................................#..................
|
||||
..............#........................................................#.#.......#...#................#....#......................
|
||||
...#...#..........................................................................................................................
|
||||
..........##.....................#.#.............#...............................#.........................................#......
|
||||
.#.#...................#.................................#.................................................#....#.................
|
||||
........................#..............................................................................................#.......#..
|
||||
...#....................................................................................................#.....##.....#............
|
||||
.............#........................#.....................................................#.............#....................#.#
|
||||
#........................................................................................#.....#..................................
|
||||
........#..............#...............................#.......#...................................#..........#...................
|
||||
.....................#.........................#.......#.......#.....#...........................................................#
|
||||
...............#.....................................................................#..........#.................................
|
||||
.................................#....#..........#..............................#...........#............................#........
|
||||
..............#......#.........#...#.............#.......................#....#...................................................
|
||||
...........................................................^.......................................................#..............
|
||||
..........#...............................#.........................#.....................................................#.....#.
|
||||
.##.....................#....................#.....#.......................................................#..##...#..........#...
|
||||
.......................#...........#..#.#.....................................................#....................#..............
|
||||
..................................................................#......#.............#..................................#.......
|
||||
.#............................................#........................#...................................#.........#............
|
||||
..................................................................................#.##.........................#..................
|
||||
................#........#...........................................#...................................#........................
|
||||
...............#........#....#...................#................................#........#.....#..........#.................#...
|
||||
.....#.............................#..........................................................................................#...
|
||||
............#..............#.....#........................................#............#............##..........#.................
|
||||
........................................................................................#..........................#........##....
|
||||
..#............................#...#.................................#....#..............#........................................
|
||||
...#.....................#...........................................................................#.....................#......
|
||||
....#.........................#.............#..................................#.........#..............#.........................
|
||||
.....................#...................................................................................#..#......#..............
|
||||
.....#.................................#...............................#.....#..........#.........................................
|
||||
............#........#..........#....................#....................#..#.#.....................................#............
|
||||
........................................#......#......................#......................................#..#.......#.........
|
||||
...#...........#...............#.................#.............................................#..........#...#...................
|
||||
.......#....##..............................#......#.......................#..................#..........#.................#...#..
|
||||
.................................##...........................................................#....#..........#.#........#........
|
||||
..................................#....##..#..#.......#............................#............#.#........................#......
|
||||
..........................................#.....................................#.............##...#............#.................
|
||||
....#....................#................................#....#..#.....................#................#...#..#..#..............
|
||||
.............#...#..............................................................................#.................................
|
||||
..........#..........................#.......................................#....................#..#.......#.........#.....#....
|
||||
......................#............#...............#.....#.................#...............................#...#....#.........##..
|
||||
..........................................................................##.............................#...#..............#.....
|
||||
#.#...#..................................................#..................................#..#...#..............................
|
||||
...........................................#....#..............#.........................#........................................
|
||||
...........#....................#.....................##........................#.....#...................................#....#..
|
||||
..........#..........#.................................................#...............................................#..........
|
||||
.....................................................................................#......#.....................#.#.............
|
||||
........#.#..........................#...............................................................................#............
|
||||
..............#..#.............#...........#......................#....#..#......#...................#............................
|
||||
........#.......#.................#........#..........#.....................#.........................................#...........
|
||||
..........................................................#...................................#.....#.........#......#...#........
|
||||
..........#.....#.....................#............#............#.#......................#....#.##.............#...........#......
|
||||
...#.#..#.......#......#........#...........................#....................#.....#.........#...........#.................#..
|
||||
........#.......................................#............#.............#..................#......#......#...............#.#...
|
||||
....................#.....#........#.........................#.................................................#................#.
|
||||
.......#..#...........................................#......#..................#..#......................#..#....................
|
||||
#.#........#....#...#.............................................#............................#.....................#............
|
||||
#...................#...........#...................#..#................................................................#.#.......
|
||||
............#...#.#...........#..#.....................................................................#........................#.
|
||||
...#..............................................#..............#......#...#..............................................#......
|
||||
................#...#..#..................................................................................................##...#..
|
||||
.............................#...#..#........#.........#...#...................#............................................#.....
|
||||
............#......................#.......................................................#.#....................................
|
||||
...#......................#......#.#...........................................#.....#....#......................#...#............
|
||||
.....#..#..........#.............................#..........................................#............#..................#.....
|
||||
.................#...#.......................#..................................................#....................#............
|
||||
............#..................................................#.....#..............#.......#.............................#.#....#
|
||||
........#.............#..#.......................................................................................#.........#......
|
||||
......#...................#..........................................................#................#................#.........#
|
||||
..................#...........................#.....#....#.......................................#..#......................#......
|
||||
....................#....................................#....................#........#............#........#....#...............
|
||||
.........#..............#....#.............................#..#..........#........#.....................#.#.........#.............
|
||||
@@ -0,0 +1,10 @@
|
||||
....#.....
|
||||
.........#
|
||||
..........
|
||||
..#.......
|
||||
.......#..
|
||||
..........
|
||||
.#..^.....
|
||||
........#.
|
||||
#.........
|
||||
......#...
|
||||
Reference in New Issue
Block a user