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
+23 -34
View File
@@ -1,26 +1,32 @@
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 {
const rows = input.split('\n');
const antennas = new Map<string, [number, number][]>();
export function groupCoordinates(rows: string[]) {
const antennas = new Map<string, Coordinates>();
for (let y = 0; y < rows.length; y++) {
for (let x = 0; x < rows[y].length; x++) {
const digit = rows[y][x];
if (digit === '.') {
const char = rows[y][x];
if (char === '.') {
continue;
}
const entry = antennas.get(digit) || [];
const entry = antennas.get(char) || [];
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) => {
combinations(positions).forEach(({ a, b }) =>
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(unique(isCoordinateEqual));
// validAntiNodes.forEach(([x, y]) => rows[y][x] === '.' && replaceChar(rows, x, y, '#'));
//
// console.log(rows.join('\n'));
return validAntiNodes.length;
}
export function solveSecond(input: string): number {
const rows = input.split('\n');
const antennas = new Map<string, [number, number][]>();
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][] = [];
const antennas = groupCoordinates(rows);
const antiNodes: Coordinates = [];
antennas.forEach((positions) => {
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]);
}
});
@@ -70,7 +58,8 @@ export function solveSecond(input: string): number {
.filter(inBounds(rows[0].length, rows.length))
.filter(unique(isCoordinateEqual));
// console.log(validAntiNodes);
// Show the anti-nodes on the input map
//
// validAntiNodes.forEach(
// ([x, y]) => rows[y][x] === '.' && replaceChar(rows, x, y, '#')
// );