From e8c9aa3f76a2fe2bd4f06cf0d7a209937a503fc5 Mon Sep 17 00:00:00 2001 From: mtrx Date: Fri, 6 Dec 2024 02:38:15 +0100 Subject: [PATCH] day 3 part 2 init --- solutions/03.ts | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/solutions/03.ts b/solutions/03.ts index 238dcca..a185fd3 100644 --- a/solutions/03.ts +++ b/solutions/03.ts @@ -7,21 +7,47 @@ import { readInput } from "../utils.ts"; class DayThree implements Solvable { input = readInput('03') + private multiplyInstruction(inst: string) { + return inst + .slice(4, -1) + .split(',') + .map(arr => Number.parseInt(arr)) + } + + public part1(): Solvable { const re = /mul\([0-9]+,[0-9]+\)/g // get all matches and parse them to int[] const result = this.input.matchAll(new RegExp(re)) - .map(match => match[0] - .slice(4, -1) - .split(',') - .map(arr => Number.parseInt(arr))) + .map(match => this.multiplyInstruction(match[0])) .map(([left, right]) => left * right) .reduce((prev, curr) => prev + curr) console.log(result) return this } + public part2(): Solvable { - return this + const re = /(mul\([0-9]+,[0-9]+\)|do\(\)|don\'t\(\))/g + // get all matches and parse them to int[] + const instructions = this.input.matchAll(new RegExp(re)).map(match => match[0]) + let active = true + let result = 0 + for (const inst of instructions) { + if (inst === 'do()') { + active = true; + continue + } + if (inst === 'don\'t()') { + active = false; + continue + } + if (active) { + const [left, right] = this.multiplyInstruction(inst) + result += left * right + } + } + console.log(result) + return this } }