Add solution day 8

This commit is contained in:
monoid 2024-12-08 17:13:34 +09:00
parent dfbe16ef13
commit 48617021fe
6 changed files with 193 additions and 0 deletions

12
day_8/example.txt Normal file
View File

@ -0,0 +1,12 @@
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............

10
day_8/example_1.txt Normal file
View File

@ -0,0 +1,10 @@
..........
..........
..........
....a.....
........a.
.....a....
..........
......A...
..........
..........

10
day_8/example_2.txt Normal file
View File

@ -0,0 +1,10 @@
T.........
...T......
.T........
..........
..........
..........
..........
..........
..........
..........

50
day_8/input.txt Normal file
View File

@ -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...........................

51
day_8/solve_1.ts Normal file
View File

@ -0,0 +1,51 @@
export async function readMapFile(path: string): Promise<string[][]> {
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);
}

60
day_8/solve_2.ts Normal file
View File

@ -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);
}