diff --git a/day_8/example.txt b/day_8/example.txt new file mode 100644 index 0000000..de0f909 --- /dev/null +++ b/day_8/example.txt @@ -0,0 +1,12 @@ +............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............ \ No newline at end of file diff --git a/day_8/example_1.txt b/day_8/example_1.txt new file mode 100644 index 0000000..2e73a87 --- /dev/null +++ b/day_8/example_1.txt @@ -0,0 +1,10 @@ +.......... +.......... +.......... +....a..... +........a. +.....a.... +.......... +......A... +.......... +.......... \ No newline at end of file diff --git a/day_8/example_2.txt b/day_8/example_2.txt new file mode 100644 index 0000000..023061c --- /dev/null +++ b/day_8/example_2.txt @@ -0,0 +1,10 @@ +T......... +...T...... +.T........ +.......... +.......... +.......... +.......... +.......... +.......... +.......... \ No newline at end of file diff --git a/day_8/input.txt b/day_8/input.txt new file mode 100644 index 0000000..3e3b536 --- /dev/null +++ b/day_8/input.txt @@ -0,0 +1,50 @@ +............................................e..... +.................................................e +......O...................................Y....... +................3................E..........Y..... +.....O1............................e....j......... +......................6........................... +.....8......Z..........6.......................... +...............3.............................u..j. +.E...........A............b...5................... +.........1.O.Z.................................... +........G...0.E..........1..6..................... +......8................A..............g.B......... +..............3..............b...u................ +........Z......8..b.........u....BO..........n.... +....8....Z.............3.....................B.... +...........................................Y...... +...................G.............................. +...0...............................j.......4...... +.....0................A.................4......n.. +..0..............x................n.e............. +.............................................4.Y.. +.G.......................b................Q....... +.............x......................M.a...m....... +..E...........G.....................a............. +.................9.......Q..............7.n....... +...........................5......m....a.......... +.........................5........................ +.....X...J......5...............................M. +..............X..........................M........ +........................W......o4...7........g.M.. +..................................N............j.. +..........................N..Q...............q.... +.......J..............x....N.......a.............. +....................x........N......U............. +.....2......J.....................w............... +...............6...................7.m........z... +.....................W..z..7.m.......o........gU.. +........y......................................... +............y.........W.......Q................... +....2.......................................q..... +.y.....................q................o..z.....g +J..........9........................o.w........z.. +.................................................. +.............................................U.... +....u..............X..........................q... +.....................................w............ +..........9....................................... +......9..........2.y......................A....... +.......................................w.......... +......................X........................... \ No newline at end of file diff --git a/day_8/solve_1.ts b/day_8/solve_1.ts new file mode 100644 index 0000000..194e5a2 --- /dev/null +++ b/day_8/solve_1.ts @@ -0,0 +1,51 @@ +export async function readMapFile(path: string): Promise { + const text = await Deno.readTextFile(path); + const lines = text.split('\n'); + return lines.map(line => line.trim().split('')); +} + +export function checkMapRange(map: string[][], x: number, y: number): boolean { + return x >= 0 && x < map[0].length && y >= 0 && y < map.length; +} + +export function displayMap(map: string[][]): void { + for (const row of map) { + console.log(row.join('')); + } +} + +if (import.meta.main) { + const map = await readMapFile('input.txt'); + const antinodeMap = map.map(row => row.map(_ => '.')); + + const antennaKind = new Set(map.flatMap(row => row.filter(cell => cell !== '.'))); + const antennas = map.flatMap((row, y) => row.map((cell, x) => ({x, y, cell}))); + console.log(antennaKind); + for (const kind of antennaKind) { + const antenna = antennas.filter(antenna => antenna.cell === kind); + for (const one of antenna) { + for (const another of antenna) { + if (one !== another) { + const dx = one.x - another.x; + const dy = one.y - another.y; + if (checkMapRange(antinodeMap, one.x + dx, one.y + dy)) { + antinodeMap[one.y + dy][one.x + dx] = "#"; + } + if (checkMapRange(antinodeMap, another.x - dx, another.y - dy)) { + antinodeMap[another.y - dy][another.x - dx] = "#"; + } + } + } + } + } + displayMap(antinodeMap); + let count = 0; + for (const row of antinodeMap) { + for (const cell of row) { + if (cell === '#') { + count++; + } + } + } + console.log(count); +} \ No newline at end of file diff --git a/day_8/solve_2.ts b/day_8/solve_2.ts new file mode 100644 index 0000000..d9cf090 --- /dev/null +++ b/day_8/solve_2.ts @@ -0,0 +1,60 @@ +import { readMapFile,checkMapRange, displayMap } from "./solve_1.ts"; + +interface Vec2 { + x: number; + y: number; +} + +function addVec(a: Vec2, b: Vec2): Vec2 { + return {x: a.x + b.x, y: a.y + b.y}; +} +function subVec(a: Vec2, b: Vec2): Vec2 { + return {x: a.x - b.x, y: a.y - b.y}; +} + +if (import.meta.main) { + const map = await readMapFile('input.txt'); + const antinodeMap = map.map(row => row.map(_ => '.')); + + const markAntinode = (vec: Vec2) => { + if (checkMapRange(antinodeMap, vec.x, vec.y)) { + antinodeMap[vec.y][vec.x] = "#"; + } + } + + const antennaKind = new Set(map.flatMap(row => row.filter(cell => cell !== '.'))); + const antennas = map.flatMap((row, y) => row.map((cell, x) => ({x, y, cell}))); + console.log(antennaKind); + for (const kind of antennaKind) { + const antenna = antennas.filter(antenna => antenna.cell === kind); + for (const one of antenna) { + for (const another of antenna) { + if (one !== another) { + const delta = subVec(one, another); + + markAntinode(one); + let start = {x: one.x, y: one.y}; + while (checkMapRange(antinodeMap, start.x, start.y)) { + markAntinode(start); + start = addVec(start, delta); + } + start = {x: another.x, y: another.y}; + while (checkMapRange(antinodeMap, start.x, start.y)) { + markAntinode(start); + start = subVec(start, delta); + } + } + } + } + } + displayMap(antinodeMap); + let count = 0; + for (const row of antinodeMap) { + for (const cell of row) { + if (cell === '#') { + count++; + } + } + } + console.log(count); +} \ No newline at end of file