From c058b50fea9cfccfd2d61e9cd9b6205d6c021ad3 Mon Sep 17 00:00:00 2001 From: monoid Date: Fri, 6 Dec 2024 16:34:22 +0900 Subject: [PATCH] Add solution day 6 --- day_6/example.txt | 10 ++++ day_6/input.txt | 130 +++++++++++++++++++++++++++++++++++++++++++ day_6/solve_1.ts | 139 ++++++++++++++++++++++++++++++++++++++++++++++ day_6/solve_2.ts | 81 +++++++++++++++++++++++++++ 4 files changed, 360 insertions(+) create mode 100644 day_6/example.txt create mode 100644 day_6/input.txt create mode 100644 day_6/solve_1.ts create mode 100644 day_6/solve_2.ts diff --git a/day_6/example.txt b/day_6/example.txt new file mode 100644 index 0000000..b60e466 --- /dev/null +++ b/day_6/example.txt @@ -0,0 +1,10 @@ +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#... \ No newline at end of file diff --git a/day_6/input.txt b/day_6/input.txt new file mode 100644 index 0000000..bf863fd --- /dev/null +++ b/day_6/input.txt @@ -0,0 +1,130 @@ +......................#...#..............#.....................................#...#..............##.............................. +......#.....#................#....#...........#........#......................#...............................#.....#.#.###..#...# +...#.#.................#........#.................#.........................................................#..................... +.....................................................................................................#.........#.................. +.............................#.................#...................................#.....#....#........................#....#..... +..........................#.....................................................................#..#.............#................ +.................#...........................#..........................#..............................#.#..#...................#. +..#....#....#......................................................................#....#......................................... +..................................#............#...........#...........................................#.#....#.............#.#... +...#...##....................................................................................#..........#..................#...... +..................#.....#......#.....#...#.........#..#.............................#....##.............#...............##.#..#... +......#...............................................................................................................#..........# +...........#................................#.......#.............#...............#...................................#........... +............##...........................#........................................................................#............... +...............................#........#...#.................................#..#.#.............#...........#.................... +........#..........#.#....................................................#........#....#..#.................................#.... +.........#..............#........................#................................................................................ +..................#..........................#.............#..................#.#...........#.....#............................... +...#...........#.#..........#..................................................................................................... +..............#.................#.....................................................................#...#......#.....#.......... +..#.....#................................................................................................#.#.....#..........#..... +#.#..........#...#.............................#...............#..................................................#.#............. +..........................#..#.............#................................................................................#..... +...#..............................................................................................#..............#..#.......#..... +........................................#......#........................................#...................##...........#........ +.............#..............#...........................##...#............#.....#..#.............................................. +..#....#..#...............#........#.................#............................................#........##...............#..... +................................................#.......#...........##.............................#.##........................... +............................#..............#..#...........#...#.....#.#...................#.....#.............................#... +....................#....................................................................................#........................ +.......................................................................................#.......................##..#.............. +#.............................#.............................................................#............................#........ +..............................................................................#.............#...#.........##...................... +...............#..........................#.............#............#...........................................#................ +................#........................#................................#...#............#.............................#........ +..............................................................#.....#..........#.......#........#....#..................#......... +........................................................#..............................................................#.......#.. +..#........#............#........#...#.....#.......#.............................................................................. +..#..............#.#.....................................#..................................................#....##............... +............#........................#..........##.................................................................#........#..... +............#............#.#...............................................#.......#.............................................. +..................#.........................................#............................#......#..................#...........#.. +........................................................#.............##................................#........................# +.....................#...#......#.......#............#........#..................#...#.#....................................#..... +...........................................................................................#..............................#....#.. +....#...................#.............#..........#..................................#............................................. +......................................#...............................................#..#........................................ +.................#..#............................................#.............................................................#.# +............##...................................................................................#.........................#...... +.........#.................................................#..#........................................#.....#..........#.....#... +.........#........................................................................................#.#...............#............# +..........#..#...........#...................#.....#.......#......#....#..#.......................#..........#..........#......... +......#...................................................#....#..............#....#.........##...#............................... +.........#........#........................#...........................................................#.......#................#. +.....................#...............................................#.##......................................................... +................................................................................#.............#..............#............#....... +......#..........#............................................#...#....................#...............................#.........# +......................................#....#.........#..............................................................#...#......... +.......##...#.#..........................................................#............#...........#........#...................#.. +.....#............................................................................#............................#.................. +....#..................#....#......#......#......................#......................#.......................................#. +..#..............#..#............#.................................#............#..................#................#............. +........................................................#....#...................................##.......................#....... +..........#..................................#....#........#.......#..........................#................#...............#.. +.....................................#.................#..................#...........#........................................... +.........#..............#...................................................................................#...................#. +.................##.................................#.......#.......................................................#.........#... +...........#.................#..........................#...............................#..#...........................#.......... +.....................................#...........................#...................#..............................#..#.......... +.............................#.....#.................................................#.................##...............#.......#. +............#.........................................................................................................#........... +..........##.............#...................#.#...........................................#....................................## +..........#..#...#...............#........#........................................................#.................#............ +....................#......................#....................................................#.#.....................#......... +.......................................#............#........................##............#....#..#.............................. +..........#..........#.....................................#.................................#.......#............................ +....#.............#............................................................................................................... +..#..............................................#....#......#........................................#.................#......... +...#......#.....#..............................#....#....................#......#..................#...................#.......#.. +...............#..#...........................##.................................................................................. +........#..#.....#.........................#.......#.............#..........................................#................#.... +...#......#.............#...............#.............................................................................##.......... +#.................................#.......#.........#.#.#...................................#...#.#...........#..........#........ +#.........#....#...#.........#.#.............................................................#.................................... +........................#.#.......#.................................................#............................................. +...#...##...#..#...........................#.....#..........................#...............................#.......#............. +................#..............................#..........##...................................................................#.. +.......................................................................#...#..#...#.....#....#..................##..........#..... +#.....................#.........#........#.....................................................................#.....#.......#.... +...........#......................#.......#.................................................#........................#............ +.......#.........................................#.....#...#...........#................#....#......#...........#......#.......... +....#.............#...............................#...#....#..#..#...........................................#.............#...... +...#.........#..........................................#.....#...........^.#...#....#...........................#................ +.....#..#...............#...........................#.#..........#........................................................#....... +.............#.......#.............................................................................................#...#.......... +..#...................#...............................#......#............#...........#.......#................................... +...........#.......................#..........#...#.......................#..............#.............##.............#..#........ +..#............#.............#.......................................................#.....##....................................# +.#.........#.....................................................................#.........#...................................... +.............#....................................................................#.....#.............#...#...........#..........# +..........................#.....#...#......................#.................................#.....#.................#.......#.... +..........#.#..................................................................................................................... +............#.......#....................#.......#.............................................#.................................. +.......................................#..#........#...................................................#.......................... +...................#........#....#........#..................#..........................................................#......... +........#..............#....................#............#......#...........#.....#..............................#.........#...... +.......#.....#.................#.#..#..............................................................#.......#..#...........#......# +.#....................##.....................................................#...#.....#.......................................... +...........#..................#.....#....#............#...............#....#............#.......................#................. +........#.....................................#......##............#..........................#..........#....................#... +..........................#...............................................................#....................................... +......................#..................................#.....................#.............................................#...# +.........#..............................................................#........##.........#.......#............................. +....#...#........#...........#....#.............#............#.....#.............................................................. +...........#...............................................................................................#.......#..#........... +........#..........................#............................#...........#............#.............................#.......... +.......#...........................#...#...............#..#..#...................#.........#...................................... +..#..............................#........#...........#.......................................#...#............................... +.............#...#.............#.........#...........#.....................................#................#..................... +.........................#..........................#.................................#........................................... +..................##.............#........#......#............#........................#.......#......................#....#...... +.....#............................................................................................................................ +.#..................................#.....#.............#...#.............................................#..............#..##..#. +...............#........................................................#.........#..#..........#.........................#....... +.............................................#...................#.................#..................#......................#.... +............................#.......................................................................................#............. +............#...............#....................#....#..........#..#..............................#......#.......#.......##...... +............#.................................................................................#................................... +........................#...............................................................#......#.......#.......#.......###......#. +......................................#.#.........................................#.....#.............................#.........#. \ No newline at end of file diff --git a/day_6/solve_1.ts b/day_6/solve_1.ts new file mode 100644 index 0000000..faa7251 --- /dev/null +++ b/day_6/solve_1.ts @@ -0,0 +1,139 @@ +export async function readMap(path: string) { + const text = await Deno.readTextFile(path); + return text.split("\n").map((line) => line.trim().split("")); +} +export function mapForEach( + map: string[][], + callback: (x: number, y: number, value: string) => void, +) { + map.forEach((line, y) => { + line.forEach((value, x) => { + callback(x, y, value); + }); + }); +} +export function mapFind( + map: string[][], + callback: (x: number, y: number, value: string) => boolean, +) { + for (let y = 0; y < map.length; y++) { + const line = map[y]; + for (let x = 0; x < line.length; x++) { + if (callback(x, y, line[x])) { + return { x, y, value: line[x] }; + } + } + } + return null; +} +export function displayMap(map: string[][]) { + map.forEach((line) => { + console.log(line.join("")); + }); +} + +export type Dir = "N" | "E" | "S" | "W"; +export type Pos = { x: number; y: number }; +export type Guard = { pos: Pos; direction: Dir }; + +export function step(pos: Pos, direction: Dir) { + const next = { ...pos }; + switch (direction) { + case "N": + next.y -= 1; + break; + case "E": + next.x += 1; + break; + case "S": + next.y += 1; + break; + case "W": + next.x -= 1; + break; + } + return next; +} +export function rotateLeft(direction: Dir) { + switch (direction) { + case "N": + return "W"; + case "E": + return "N"; + case "S": + return "E"; + case "W": + return "S"; + } +} +export function rotateRight(direction: Dir) { + switch (direction) { + case "N": + return "E"; + case "E": + return "S"; + case "S": + return "W"; + case "W": + return "N"; + } +} + +export const EMPTY = "."; +export const WALL = "#"; +const G = "X"; + +export function moveGuard(guard: Guard, map: string[][]): Guard | "out" { + const { direction } = guard; + const next = step(guard.pos, direction); + // Check if next position is out of bounds + if ( + next.y < 0 || + next.y >= map.length || + next.x < 0 || + next.x >= map[next.y].length + ) { + // Out of bounds + return "out"; + } + // Check if next position is a wall + if (map[next.y][next.x] === WALL) { + // Turn right + const right = rotateRight(direction); + return { ...guard, direction: right }; + } + return { ...guard, pos: next }; +} + + + +if (import.meta.main) { + const map = await readMap("input.txt"); + const guardPos = mapFind(map, (_x, _y, value) => value === "^"); + if (!guardPos) { + throw new Error("Guard not found"); + } + const { x, y } = guardPos; + let guard: Guard = { + pos: { x, y }, + direction: "N", + }; + map[y][x] = G; + + for (;;) { + const n = moveGuard(guard, map); + if (n === "out") { + break; + } + guard = n; + map[guard.pos.y][guard.pos.x] = G; + } + displayMap(map); + let count = 0; + mapForEach(map, (_x, _y, value) => { + if (value === G) { + count++; + } + }); + console.log(count); +} \ No newline at end of file diff --git a/day_6/solve_2.ts b/day_6/solve_2.ts new file mode 100644 index 0000000..17d01bb --- /dev/null +++ b/day_6/solve_2.ts @@ -0,0 +1,81 @@ +import { readMap, mapFind, Guard, moveGuard, displayMap, mapForEach, Dir, EMPTY, Pos } from "./solve_1.ts"; + +function getMDir( + dir: Dir, + mapValue: string +){ + if (mapValue === "+") { + return mapValue; + } + if (dir === mapValue || mapValue === EMPTY){ + return dir; + } + return "+"; +} + +function loopDetect( + obstaclePos: Pos, + guard: Guard, + map: string[][] +){ + map[obstaclePos.y][obstaclePos.x] = "#"; + try { + let doubleMoveGaurd: Guard | "out" = guard; + for(;;){ + const n = moveGuard(guard, map); + if (n === "out"){ + return false; + } + guard = n; + doubleMoveGaurd = moveGuard(doubleMoveGaurd, map); + if (doubleMoveGaurd === "out"){ + return false; + } + doubleMoveGaurd = moveGuard(doubleMoveGaurd, map); + if (doubleMoveGaurd === "out"){ + return false; + } + if (guard.pos.x === doubleMoveGaurd.pos.x && guard.pos.y === doubleMoveGaurd.pos.y + && guard.direction === doubleMoveGaurd.direction + ){ + return true; + } + } + } + finally { + map[obstaclePos.y][obstaclePos.x] = "."; + } +} + +if (import.meta.main) { + const map = await readMap("input.txt"); + const guardPos = mapFind(map, (_x, _y, value) => value === "^"); + if (!guardPos) { + throw new Error("Guard not found"); + } + const { x, y } = guardPos; + let guard: Guard = { + pos: { x, y }, + direction: "N", + }; + map[y][x] = "|" + + let count = 0; + for (;;) { + const n = moveGuard(guard, map); + if (n === "out") { + break; + } + if (map[n.pos.y][n.pos.x] === EMPTY){ + if (loopDetect(n.pos, guard, map)){ + console.log(n.pos); + count++; + } + } + guard = n; + const v = map[guard.pos.y][guard.pos.x]; + map[guard.pos.y][guard.pos.x] = getMDir(guard.direction, v); + } + displayMap(map); + console.log(count); +} \ No newline at end of file