57 lines
No EOL
1.7 KiB
TypeScript
57 lines
No EOL
1.7 KiB
TypeScript
|
|
import { solvables } from "../main.ts";
|
|
|
|
import type { Solvable } from "../solvable.ts";
|
|
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]{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]))
|
|
.map(([left, right]) => left * right)
|
|
.reduce((prev, curr) => prev + curr)
|
|
console.log(result)
|
|
return this
|
|
}
|
|
|
|
public part2(): Solvable {
|
|
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.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
|
|
continue
|
|
}
|
|
if (inst === 'don\'t()') {
|
|
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)
|
|
return this
|
|
}
|
|
}
|
|
|
|
solvables.push(new DayThree()) |