advent-of-code-2023/day_3/part_1.ts
2023-12-03 15:38:59 +08:00

26 lines
679 B
TypeScript

import * as fs from "fs";
import { iterPoint, parse_file, point_2_string } from "./parser";
const input = fs.readFileSync("input", "utf8");
const { part_numbers, part_number_cells, symbols } = parse_file(input);
const valid_part_number_indices: Set<number> = new Set();
symbols.forEach((e) => {
iterPoint(e.p)
.map((e) => point_2_string(e))
.forEach((adjP) => {
const part_number_index = part_number_cells.get(adjP);
if (part_number_index !== undefined) {
valid_part_number_indices.add(part_number_index);
}
});
});
console.log(
[...valid_part_number_indices]
.map((i) => part_numbers[i])
.reduce((sum, v) => sum + v, 0)
);