Cleanup on 2024-08

This commit is contained in:
2024-12-08 20:05:00 +01:00
parent 532913e039
commit 795d140ba4
3 changed files with 25 additions and 37 deletions
+1 -3
View File
@@ -1,4 +1,2 @@
.output
.nuxt
node_modules node_modules
prisma/migrations input.txt
+23 -34
View File
@@ -1,26 +1,32 @@
import { combinations, unique } from '../../utils/array.ts'; import { combinations, unique } from '../../utils/array.ts';
import { inBounds, isCoordinateEqual } from '../../utils/coordinates.ts'; import {
type Coordinates,
inBounds,
isCoordinateEqual
} from '../../utils/coordinates.ts';
export function solveFirst(input: string): number { export function groupCoordinates(rows: string[]) {
const rows = input.split('\n'); const antennas = new Map<string, Coordinates>();
const antennas = new Map<string, [number, number][]>();
for (let y = 0; y < rows.length; y++) { for (let y = 0; y < rows.length; y++) {
for (let x = 0; x < rows[y].length; x++) { for (let x = 0; x < rows[y].length; x++) {
const digit = rows[y][x]; const char = rows[y][x];
if (digit === '.') { if (char === '.') {
continue; continue;
} }
const entry = antennas.get(digit) || []; const entry = antennas.get(char) || [];
entry.push([x, y]); entry.push([x, y]);
antennas.set(digit, entry); antennas.set(char, entry);
} }
} }
const antiNodes: [number, number][] = []; return antennas;
}
export function solveFirst(input: string): number {
const rows = input.split('\n');
const antennas = groupCoordinates(rows);
const antiNodes: Coordinates = [];
// console.log(rows);
// console.log(antennas);
antennas.forEach((positions) => { antennas.forEach((positions) => {
combinations(positions).forEach(({ a, b }) => combinations(positions).forEach(({ a, b }) =>
antiNodes.push([a[0] - (b[0] - a[0]), a[1] - (b[1] - a[1])]) antiNodes.push([a[0] - (b[0] - a[0]), a[1] - (b[1] - a[1])])
@@ -31,36 +37,18 @@ export function solveFirst(input: string): number {
.filter(inBounds(rows[0].length, rows.length)) .filter(inBounds(rows[0].length, rows.length))
.filter(unique(isCoordinateEqual)); .filter(unique(isCoordinateEqual));
// validAntiNodes.forEach(([x, y]) => rows[y][x] === '.' && replaceChar(rows, x, y, '#'));
//
// console.log(rows.join('\n'));
return validAntiNodes.length; return validAntiNodes.length;
} }
export function solveSecond(input: string): number { export function solveSecond(input: string): number {
const rows = input.split('\n'); const rows = input.split('\n');
const antennas = new Map<string, [number, number][]>(); const antennas = groupCoordinates(rows);
const antiNodes: Coordinates = [];
const yMax = rows.length;
const xMax = rows[0].length;
for (let y = 0; y < yMax; y++) {
for (let x = 0; x < xMax; x++) {
const digit = rows[y][x];
if (digit === '.') {
continue;
}
const entry = antennas.get(digit) || [];
entry.push([x, y]);
antennas.set(digit, entry);
}
}
const antiNodes: [number, number][] = [];
antennas.forEach((positions) => { antennas.forEach((positions) => {
combinations(positions).forEach(({ a, b }) => { combinations(positions).forEach(({ a, b }) => {
for (let y = 0; y < yMax; y++) { for (let y = 0; y < rows.length; y++) {
antiNodes.push([a[0] + (b[0] - a[0]) * y, a[1] + (b[1] - a[1]) * y]); antiNodes.push([a[0] + (b[0] - a[0]) * y, a[1] + (b[1] - a[1]) * y]);
} }
}); });
@@ -70,7 +58,8 @@ export function solveSecond(input: string): number {
.filter(inBounds(rows[0].length, rows.length)) .filter(inBounds(rows[0].length, rows.length))
.filter(unique(isCoordinateEqual)); .filter(unique(isCoordinateEqual));
// console.log(validAntiNodes); // Show the anti-nodes on the input map
//
// validAntiNodes.forEach( // validAntiNodes.forEach(
// ([x, y]) => rows[y][x] === '.' && replaceChar(rows, x, y, '#') // ([x, y]) => rows[y][x] === '.' && replaceChar(rows, x, y, '#')
// ); // );
+1
View File
@@ -1,4 +1,5 @@
export type Coordinate = [number, number]; export type Coordinate = [number, number];
export type Coordinates = Coordinate[];
/** /**
* Returns a filter lambda to be used in array.filter() on a xy-Coordinate array (array of two number arrays). * Returns a filter lambda to be used in array.filter() on a xy-Coordinate array (array of two number arrays).