diff --git a/2024/ts/day-05/code.ts b/2024/ts/day-05/code.ts index 1b79ce9..2c67988 100644 --- a/2024/ts/day-05/code.ts +++ b/2024/ts/day-05/code.ts @@ -1,9 +1,8 @@ // Solution for day 5 of advent of code 2024 -import { readInputLines, readInput } from "../system/aoc-helper"; +import { readInputLines, readInput, dlog } from "../system/aoc-helper"; import "../utils/array-helpers"; import { Puzzle } from "../model/puzzle"; -import { parse } from "path"; type Input = { ruleset: [number, number][], @@ -43,12 +42,12 @@ const checkUpdate = (update: number[], ruleset: [number, number][]) => { for (let sindex = findex + 1; sindex < update.length; sindex++) { const second = update[sindex]; if (ruleset.some(([frule, srule]) => (frule === second && srule === first))) { - //console.log(`Failed update ${update} for ${first} and ${second}`); + dlog(`Failed update ${update} for ${first} and ${second}`); return false; } } } - //console.log(`Passed update ${update}`); + dlog(`Passed update ${update}`); return true; }; @@ -69,7 +68,6 @@ const findProblemIndices = (update: number[], ruleset: [number, number][]) => { for (let sindex = findex + 1; sindex < update.length; sindex++) { const second = update[sindex]; if (ruleset.some(([frule, srule]) => (frule === second && srule === first))) { - //console.log(`Failed update ${update} for ${first} and ${second}`); return [findex, sindex]; } } @@ -98,10 +96,11 @@ const fixUpdate = (update: number[], ruleset: [number, number][]) => { const partTwo = (input: Input, debug: boolean) => { let result = 0; const invalidUpdates = input.updates.filter(update => !checkUpdate(update, input.ruleset)); + dlog("-----"); for (const update of invalidUpdates) { const fixed = fixUpdate(update, input.ruleset); // assumes that the update has an odd number of elements - // console.log(`Fixed update ${update} to ${fixed}`); + dlog(`Fixed update [${update}] to [${fixed}]`); result += fixed[(fixed.length - 1) / 2]; } return result; diff --git a/2024/ts/system/aoc-helper.ts b/2024/ts/system/aoc-helper.ts index 8b130fa..ee92775 100644 --- a/2024/ts/system/aoc-helper.ts +++ b/2024/ts/system/aoc-helper.ts @@ -1,4 +1,5 @@ import * as fs from "fs"; +import { debug } from "./run-task"; export function getDay(day: number) { if (day < 10) { @@ -18,7 +19,7 @@ export function readInputLines(day: number) { return input.split("\r\n"); } -export function debugLog(debug: boolean, ...args:any[]) { +export function dlog(...args:any[]) { if (debug) { console.log(...args); } diff --git a/2024/ts/system/run-task.ts b/2024/ts/system/run-task.ts index dddd5ab..fc7e2ad 100644 --- a/2024/ts/system/run-task.ts +++ b/2024/ts/system/run-task.ts @@ -1,8 +1,8 @@ import { performance } from "perf_hooks"; import { Puzzle } from "../model/puzzle"; -const debug = process.env.DEBUG; -const test = process.env.TEST; +export const debug = process.env.DEBUG; +export const test = process.env.TEST; (async () => {