From c15fe9f0693084bb774cb338b0a6d2ea8b159517 Mon Sep 17 00:00:00 2001 From: Peter Date: Sun, 3 Dec 2023 15:38:59 +0800 Subject: [PATCH] =?UTF-8?q?Day=203=20solutions=20=F0=9F=98=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day_3/parser.ts | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ day_3/part_1.ts | 25 ++++++++++++++++ day_3/part_2.ts | 40 +++++++++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 day_3/parser.ts create mode 100644 day_3/part_1.ts create mode 100644 day_3/part_2.ts diff --git a/day_3/parser.ts b/day_3/parser.ts new file mode 100644 index 0000000..e34e4cd --- /dev/null +++ b/day_3/parser.ts @@ -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 = 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, + }; +}; diff --git a/day_3/part_1.ts b/day_3/part_1.ts new file mode 100644 index 0000000..ca12792 --- /dev/null +++ b/day_3/part_1.ts @@ -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 = 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) +); diff --git a/day_3/part_2.ts b/day_3/part_2.ts new file mode 100644 index 0000000..f535ec8 --- /dev/null +++ b/day_3/part_2.ts @@ -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 = new Set(); + +symbols.forEach((e) => { + if (e.symbol === "*") { + const gears: Set = 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) +);