123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- import { DeepClone, Inspect, LoadInput } from "../common.ts";
- import { BuildSchematicsMap, ParseSchematics, ValidateEngineParts, SchematicsGear as SchematicsGear, EnginePart } from "./3_common.ts";
- const tests = [
- "467..114..",
- "...*......",
- "..35..633.",
- "......#...",
- "617*......",
- ".....+.58.",
- "..592.....",
- "......755.",
- "...$.*....",
- ".664.598..",
- ];
- const tests_reddit = [
- "12.......*..",
- "+.........34",
- ".......-12..",
- "..78........",
- "..*....60...",
- "78..........",
- ".......23...",
- "....90*12...",
- "............",
- "2.2......12.",
- ".*.........*",
- "1.1.......56",
- ];
- const input = await LoadInput(3);
- const schematics = BuildSchematicsMap(input);
- let engineParts = ParseSchematics(schematics);
- engineParts = ValidateEngineParts(engineParts, schematics);
- let gears = FindPotentialGears(schematics);
- gears = ValidateGears(gears, engineParts);
- const sumOfGearRatios = gears.reduce((accumulator, gear) => accumulator += gear.GearRatio, 0);
- console.log(`The sum of Gear Ratios is: ${sumOfGearRatios}`);
- function FindPotentialGears(schematicsMap: string[][]): SchematicsGear[] {
- const gearsInSchematics: SchematicsGear[] = [];
- for (let y = 0; y < schematicsMap.length; y++) {
- for (let x = 0; x < schematicsMap[y].length; x++) {
-
- if (/[*]/.test(schematicsMap[y][x])) {
- gearsInSchematics.push({
- Symbol: "*",
- Coordinates: {
- X: x,
- Y: y,
- },
- GearRatio: -1,
- });
- }
- }
- }
- return gearsInSchematics;
- }
- function ValidateGears(potentialGears: SchematicsGear[], engineParts: EnginePart[]): SchematicsGear[] {
- const validGears: SchematicsGear[] = [];
- for (const gear of potentialGears) {
-
- let partsClone = DeepClone(engineParts) as EnginePart[];
-
- partsClone = partsClone.filter((part) => {
- return part.Coordinates[0].Y == gear.Coordinates.Y
- || part.Coordinates[0].Y - 1 == gear.Coordinates.Y
- || part.Coordinates[0].Y + 1 == gear.Coordinates.Y;
- });
-
- if (partsClone.length < 2) { continue; }
-
- partsClone = partsClone.filter((part) => {
- for(const coord of part.Coordinates) {
- if(gear.Coordinates.X == coord.X
- || gear.Coordinates.X + 1 == coord.X
- || gear.Coordinates.X - 1 == coord.X) {
- return true;
- }
- }
- return false;
- });
-
- if (partsClone.length == 2 ) {
- gear.GearRatio = partsClone.reduce((accumulator, part) => accumulator *= part.PartNumber, 1);
- validGears.push(gear);
- }
- }
- return validGears;
- }
|