From 4eed10da179092bde5d4ab6e72c5c8d672117815 Mon Sep 17 00:00:00 2001 From: legonzaur Date: Fri, 6 Dec 2024 10:21:58 +0100 Subject: [PATCH] day 6 --- 2024/6.1.ts | 81 ++++++++++++++++++++++++++ 2024/inputs/6 | 130 ++++++++++++++++++++++++++++++++++++++++++ 2024/inputs/6.example | 10 ++++ 3 files changed, 221 insertions(+) create mode 100644 2024/6.1.ts create mode 100644 2024/inputs/6 create mode 100644 2024/inputs/6.example diff --git a/2024/6.1.ts b/2024/6.1.ts new file mode 100644 index 0000000..452efee --- /dev/null +++ b/2024/6.1.ts @@ -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)) \ No newline at end of file diff --git a/2024/inputs/6 b/2024/inputs/6 new file mode 100644 index 0000000..2f03a79 --- /dev/null +++ b/2024/inputs/6 @@ -0,0 +1,130 @@ +.........................................#.....#..##.....................#...........................#.....................#....#. +..................#................................#..#.................................#................#........................ +.....##.........#..#........#......#............#.....#..................#............................#...#...#..#...#............ +#..#...#....#...#.................................#.........................................#...............................#..... +.................##...................................................#.........................#....................#............ +.............#.............#...................#...................#..................................#....#..#........#....#..... +..#.........................................................................................##....#.................#............. +...........#.........#.....#..#.................#...........##..........#....#....................................#........#.#.... +#...#.........................................#..........................................................#........................ +.............................................#................................#...............................##..........#....... +......#....#..............#..........................................#....#........................#...................##....#.... +........#.#..............................#..#.............#.......#............................................................#.. +....#...............#...#..#......#...........................#........................#...........#...#...................#...... +..........................#...........##....#.......................................#..........................#.#................ +....................#.........................................................#........#.......................................... +..............#.............#.##.............#.........#..........#.......................................#...#.........#..#...... +...............................................................#..........#........#.........#.............#.............#........ +............#.................#...................#....#......................................................................#... +.......#....##....#......##.........#...........................................#....................................#.........#.. +......#...#......................#............................##....##....................#....................................... +...................................##....................#.............................#........................#................. +..........................#.#.........................#..........................................................#................ +.............#...#..........................................................#..#.#.#.....#....#.....#...................#........# +....#......#.............#...#......#.........#............##..................#.......................#.......................... +.......................#.....#...................................#..........................#..................................... +#.....##....................................#.....#.....................................................#....#.................... +............#...#...................................................................#.........#......................#............ +................#....................................#.....#.........#.........#..............#................................... +..........................................................................................#.....#.#...#.........#.......#......... +#.............#..............................................#...#.#..#.....................#...#............#.#...#.............. +..........................................#..................................................................#...#................ +.........#.............................................#................................#.................#.....#................# +........................................................................................#.......#.....#..........#..........#..... +...........................#.......#......#..........#............#...............#........#...#......#........................... +...##...................#.................................#..................................##................................... +........................................................#.#.#.....#............................##................#................ +......................................#....................#..................#...#.............................#...........#..... +.............................#...#.................#.............................#..............#................................. +#.....................................................................................................................#..#.....#.. +...........................#..............................................#................................#....................#. +......#..................##................#....................#.............................................................#..# +.................#....................................................................................#........................... +....#.#..................................#..#........................##...#.............................#..............#.......... +...............................................................................................................#...#.....#........ +..#...................#.......................#........................................................................#.......... +..#............#......................................###.........#..............................#..........#.................#... +.................................#..........................................................................#............#........ +......................#...#...............#..............................#.......#........................#...........#........#.. +..#..................#............................................................................#......#........................ +..........................................................................#....#........#...................#.............#....... +..............#.........#.#............................................................................................#.......... +........................#...#............................................#..............................#......................... +.........................................#...................................................#........................#........... +...#.................................................#..........#.............................#.....#.#.........#................. +............................#...........#...............................................................#........................# +..........................#..#..................#....................................#..................#.......................#. +......#...................#..............................#........#.##..........................................................#. +...........................#.....#.............................................................................#.................. +..............#........................................................#.#.......#...#................#....#...................... +...#...#.......................................................................................................................... +..........##.....................#.#.............#...............................#.........................................#...... +.#.#...................#.................................#.................................................#....#................. +........................#..............................................................................................#.......#.. +...#....................................................................................................#.....##.....#............ +.............#........................#.....................................................#.............#....................#.# +#........................................................................................#.....#.................................. +........#..............#...............................#.......#...................................#..........#................... +.....................#.........................#.......#.......#.....#...........................................................# +...............#.....................................................................#..........#................................. +.................................#....#..........#..............................#...........#............................#........ +..............#......#.........#...#.............#.......................#....#................................................... +...........................................................^.......................................................#.............. +..........#...............................#.........................#.....................................................#.....#. +.##.....................#....................#.....#.......................................................#..##...#..........#... +.......................#...........#..#.#.....................................................#....................#.............. +..................................................................#......#.............#..................................#....... +.#............................................#........................#...................................#.........#............ +..................................................................................#.##.........................#.................. +................#........#...........................................#...................................#........................ +...............#........#....#...................#................................#........#.....#..........#.................#... +.....#.............................#..........................................................................................#... +............#..............#.....#........................................#............#............##..........#................. +........................................................................................#..........................#........##.... +..#............................#...#.................................#....#..............#........................................ +...#.....................#...........................................................................#.....................#...... +....#.........................#.............#..................................#.........#..............#......................... +.....................#...................................................................................#..#......#.............. +.....#.................................#...............................#.....#..........#......................................... +............#........#..........#....................#....................#..#.#.....................................#............ +........................................#......#......................#......................................#..#.......#......... +...#...........#...............#.................#.............................................#..........#...#................... +.......#....##..............................#......#.......................#..................#..........#.................#...#.. +.................................##...........................................................#....#..........#.#........#........ +..................................#....##..#..#.......#............................#............#.#........................#...... +..........................................#.....................................#.............##...#............#................. +....#....................#................................#....#..#.....................#................#...#..#..#.............. +.............#...#..............................................................................#................................. +..........#..........................#.......................................#....................#..#.......#.........#.....#.... +......................#............#...............#.....#.................#...............................#...#....#.........##.. +..........................................................................##.............................#...#..............#..... +#.#...#..................................................#..................................#..#...#.............................. +...........................................#....#..............#.........................#........................................ +...........#....................#.....................##........................#.....#...................................#....#.. +..........#..........#.................................................#...............................................#.......... +.....................................................................................#......#.....................#.#............. +........#.#..........................#...............................................................................#............ +..............#..#.............#...........#......................#....#..#......#...................#............................ +........#.......#.................#........#..........#.....................#.........................................#........... +..........................................................#...................................#.....#.........#......#...#........ +..........#.....#.....................#............#............#.#......................#....#.##.............#...........#...... +...#.#..#.......#......#........#...........................#....................#.....#.........#...........#.................#.. +........#.......................................#............#.............#..................#......#......#...............#.#... +....................#.....#........#.........................#.................................................#................#. +.......#..#...........................................#......#..................#..#......................#..#.................... +#.#........#....#...#.............................................#............................#.....................#............ +#...................#...........#...................#..#................................................................#.#....... +............#...#.#...........#..#.....................................................................#........................#. +...#..............................................#..............#......#...#..............................................#...... +................#...#..#..................................................................................................##...#.. +.............................#...#..#........#.........#...#...................#............................................#..... +............#......................#.......................................................#.#.................................... +...#......................#......#.#...........................................#.....#....#......................#...#............ +.....#..#..........#.............................#..........................................#............#..................#..... +.................#...#.......................#..................................................#....................#............ +............#..................................................#.....#..............#.......#.............................#.#....# +........#.............#..#.......................................................................................#.........#...... +......#...................#..........................................................#................#................#.........# +..................#...........................#.....#....#.......................................#..#......................#...... +....................#....................................#....................#........#............#........#....#............... +.........#..............#....#.............................#..#..........#........#.....................#.#.........#............. diff --git a/2024/inputs/6.example b/2024/inputs/6.example new file mode 100644 index 0000000..b60e466 --- /dev/null +++ b/2024/inputs/6.example @@ -0,0 +1,10 @@ +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#... \ No newline at end of file