Skip to content

Commit

Permalink
Impoved debug log management
Browse files Browse the repository at this point in the history
  • Loading branch information
sweko committed Dec 5, 2024
1 parent fcde2fc commit f5d8eb1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
11 changes: 5 additions & 6 deletions 2024/ts/day-05/code.ts
Original file line number Diff line number Diff line change
@@ -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][],
Expand Down Expand Up @@ -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;
};

Expand All @@ -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];
}
}
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion 2024/ts/system/aoc-helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as fs from "fs";
import { debug } from "./run-task";

export function getDay(day: number) {
if (day < 10) {
Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions 2024/ts/system/run-task.ts
Original file line number Diff line number Diff line change
@@ -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 () => {

Expand Down

0 comments on commit f5d8eb1

Please sign in to comment.