Day 3 solutions 😀

This commit is contained in:
Peter 2023-12-03 15:38:59 +08:00
parent 60cbaac7df
commit c15fe9f069
3 changed files with 143 additions and 0 deletions

78
day_3/parser.ts Normal file
View File

@ -0,0 +1,78 @@
enum PARSER_STATE {
EMPTY,
NUMBER,
}
export interface Point {
i: number;
j: number;
}
export const iterPoint = (p: Point) => {
const adjacent_points: Point[] = [
{ i: p.i + 1, j: p.j + 1 },
{ i: p.i + 1, j: p.j + 0 },
{ i: p.i + 1, j: p.j + -1 },
{ i: p.i + 0, j: p.j + 1 },
// {i: p.i + 0, j: p.j + 0},
{ i: p.i + 0, j: p.j + -1 },
{ i: p.i + -1, j: p.j + 1 },
{ i: p.i + -1, j: p.j + 0 },
{ i: p.i + -1, j: p.j + -1 },
];
return adjacent_points;
};
export interface Symbol {
p: Point;
symbol: string;
}
export const point_2_string = (p: Point) => `${p.i},${p.j}`;
export const parse_file = (input: string) => {
const grid = input.split("\n").map((row) => row.split(""));
var parser_state = PARSER_STATE.EMPTY;
const part_numbers: number[] = [];
const part_number_cells: Map<string, number> = new Map();
const symbols: Symbol[] = [];
for (const j in grid) {
const row = grid[j];
// Numbers do not go down rows.
parser_state = parser_state = PARSER_STATE.EMPTY;
var number_buf = "";
for (const i in row) {
const cell = row[i];
const point: Point = { i: parseInt(i), j: parseInt(j) };
if (["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"].includes(cell)) {
parser_state = PARSER_STATE.NUMBER;
number_buf += cell;
part_number_cells.set(point_2_string(point), part_numbers.length);
} else {
if (parser_state === PARSER_STATE.NUMBER) {
part_numbers.push(parseInt(number_buf));
number_buf = "";
}
if (cell !== ".") {
symbols.push({ p: point, symbol: cell });
}
parser_state = PARSER_STATE.EMPTY;
}
}
// Cleanup at end of file.
if (parser_state === PARSER_STATE.NUMBER) {
part_numbers.push(parseInt(number_buf));
}
}
return {
part_numbers: part_numbers,
part_number_cells: part_number_cells,
symbols: symbols,
};
};

25
day_3/part_1.ts Normal file
View File

@ -0,0 +1,25 @@
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)
);

40
day_3/part_2.ts Normal file
View File

@ -0,0 +1,40 @@
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);
interface GearPair {
g1: number;
g2: number;
}
const gear_pairs: Set<GearPair> = new Set();
symbols.forEach((e) => {
if (e.symbol === "*") {
const gears: Set<number> = new Set();
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) {
gears.add(part_number_index);
}
});
if (gears.size === 2) {
const gear_pair: GearPair = {
g1: [...gears][0],
g2: [...gears][1],
};
gear_pairs.add(gear_pair);
}
}
});
console.log(
[...gear_pairs]
.map((pair) => part_numbers[pair.g1] * part_numbers[pair.g2])
.reduce((sum, v) => sum + v, 0)
);