diff --git a/.vscode/launch.json b/.vscode/launch.json index 283ef30..0804bd6 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -7,8 +7,10 @@ "args": [ "--experimental-specifier-resolution=node", "--trace-warnings", - "--loader", - "ts-node/esm", + // "--loader", + // "ts-node/esm", + "--import", + "data:text/javascript,import { register } from 'node:module'; import { pathToFileURL } from 'node:url'; register('ts-node/esm', pathToFileURL('./'));", "${file}" ], "skipFiles": [ @@ -16,4 +18,4 @@ ], }, ] -} +} \ No newline at end of file diff --git a/16.1.ts b/16.1.ts index ebc1adc..b0304aa 100644 --- a/16.1.ts +++ b/16.1.ts @@ -1,9 +1,68 @@ import { open } from 'node:fs/promises'; -const file = await open('./inputs/16'); +const path = './inputs/16' -let input = (await file.readFile()).toString().split('\n') +const input = (await (await open(path)).readFile()).toString().split('\n').map(e => e.split("")) +const process = (await (await open(path)).readFile()).toString().split('\n').map(e => e.split("").map(e => [] as Coords[])) -function ray(position: { x: number, y: number }, direction: { x: number, y: number }) { +type Coords = { x: number, y: number } -} \ No newline at end of file +function moveForward({ x, y }: Coords, direction: Coords) { + ray({ x: x + direction.x, y: y + direction.y }, direction) +} + +function ray({ x, y }: Coords, direction: Coords) { + + if (input[y] === undefined) { + return + } + const current = input[y][x] + + if (current === undefined) { + return + } + + if (process[y][x].find(e => e.x == direction.x && e.y == direction.y)) { + return + } + process[y][x].push(direction) + + + + if (current === '\\') { + direction = { x: direction.y, y: direction.x } + moveForward({ x, y }, direction) + return + } + + if (current === '/') { + direction = { x: -direction.y, y: -direction.x } + moveForward({ x, y }, direction) + return + } + + if (current === '|') { + if (direction.x === 0) { + moveForward({ x, y }, direction) + return + } + moveForward({ x, y }, { x: 0, y: 1 }) + moveForward({ x, y }, { x: 0, y: -1 }) + return + } + + if (current === '-') { + if (direction.y === 0) { + moveForward({ x, y }, direction) + return + } + moveForward({ x, y }, { x: 1, y: 0 }) + moveForward({ x, y }, { x: -1, y: 0 }) + return + } + moveForward({ x, y }, direction) +} + +ray({ x: 0, y: 0 }, { x: 1, y: 0 }) + +console.log(process.flat(1).reduce((acc, e) => e.length > 0 ? acc + 1 : acc, 0)) \ No newline at end of file diff --git a/16.2.ts b/16.2.ts new file mode 100644 index 0000000..1440b81 --- /dev/null +++ b/16.2.ts @@ -0,0 +1,74 @@ +import { open } from 'node:fs/promises'; + +const path = './inputs/16' +const file1 = (await open(path)) +const file2 = (await open(path)) + +const input = (await file1.readFile()).toString().split('\n').map(e => e.split("")) +const process = (await file2.readFile()).toString().split('\n').map(e => e.split("").map(e => [] as Coords[])) + +type Coords = { x: number, y: number } + +function moveForward({ x, y }: Coords, direction: Coords) { + ray({ x: x + direction.x, y: y + direction.y }, direction) +} + +function ray({ x, y }: Coords, direction: Coords) { + + if (input[y] === undefined) { + return + } + const current = input[y][x] + + if (current === undefined) { + return + } + + if (process[y][x].find(e => e.x == direction.x && e.y == direction.y)) { + return + } + process[y][x].push(direction) + + + + if (current === '\\') { + direction = { x: direction.y, y: direction.x } + moveForward({ x, y }, direction) + return + } + + if (current === '/') { + direction = { x: -direction.y, y: -direction.x } + moveForward({ x, y }, direction) + return + } + + if (current === '|') { + if (direction.x === 0) { + moveForward({ x, y }, direction) + return + } + moveForward({ x, y }, { x: 0, y: 1 }) + moveForward({ x, y }, { x: 0, y: -1 }) + return + } + + if (current === '-') { + if (direction.y === 0) { + moveForward({ x, y }, direction) + return + } + moveForward({ x, y }, { x: 1, y: 0 }) + moveForward({ x, y }, { x: -1, y: 0 }) + return + } + moveForward({ x, y }, direction) +} + +ray({ x: 0, y: 0 }, { x: 1, y: 0 }) + +console.log(process.flat(1).reduce((acc, e) => e.length > 0 ? acc + 1 : acc, 0)) + + + +await Promise.all([file1.close(), file2.close()]) diff --git a/inputs/16.example b/inputs/16.example new file mode 100644 index 0000000..c78b2e7 --- /dev/null +++ b/inputs/16.example @@ -0,0 +1,10 @@ +.|...\.... +|.-.\..... +.....|-... +........|. +.......... +.........\ +..../.\\.. +.-.-/..|.. +.|....-|.\ +..//.|.... \ No newline at end of file