day 3 passed

This commit is contained in:
mtrx 2024-12-06 03:07:23 +01:00
parent e8c9aa3f76
commit bd4374c810
2 changed files with 10 additions and 7 deletions

View file

@ -16,7 +16,7 @@ class DayThree implements Solvable {
public part1(): Solvable {
const re = /mul\([0-9]+,[0-9]+\)/g
const re = /mul\([0-9]{1,3},[0-9]{1,3}\)/g
// get all matches and parse them to int[]
const result = this.input.matchAll(new RegExp(re))
.map(match => this.multiplyInstruction(match[0]))
@ -27,23 +27,26 @@ class DayThree implements Solvable {
}
public part2(): Solvable {
const re = /(mul\([0-9]+,[0-9]+\)|do\(\)|don\'t\(\))/g
const re = /(mul\([0-9]{1,3},[0-9]{1,3}\)|do\(\)|don\'t\(\))/g
// get all matches and parse them to int[]
const instructions = this.input.matchAll(new RegExp(re)).map(match => match[0])
const instructions = this.input.replace('\n', '').trim().matchAll(new RegExp(re)).map(match => match[0])
let active = true
let result = 0
for (const inst of instructions) {
console.log((active ? '' : 'skipping ') + inst )
if (inst === 'do()') {
active = true;
active = true
continue
}
if (inst === 'don\'t()') {
active = false;
continue
active = false
continue
}
if (active) {
const [left, right] = this.multiplyInstruction(inst)
result += left * right
console.log((active ? 'multiplying ' : '' ), left, right)
console.log(left, right)
}
}
console.log(result)