initial upload
This commit is contained in:
28
2024/03/index.js
Normal file
28
2024/03/index.js
Normal file
@ -0,0 +1,28 @@
|
||||
import fs from 'node:fs'
|
||||
fs.readFile('input.txt', ({ encoding: "utf-8" }), (err, data) => {
|
||||
const mul_regex = /mul\((\d+),(\d+)\)/g
|
||||
const instruct_regex = /do(n't)?\(\)/g
|
||||
let do_instruction = true
|
||||
let result = 0
|
||||
let multiplications_done = 0
|
||||
const instructions = [...data.matchAll(instruct_regex)]
|
||||
const matches = [...data.matchAll(mul_regex)]
|
||||
for (let i = 0; i < instructions.length; i++) {
|
||||
const now = instructions[i]
|
||||
const then = (i + 1) in [...instructions.keys()] ? instructions[i + 1].index : data.length
|
||||
do_instruction = now[0] === "do()" ? true : false
|
||||
if (!do_instruction) { continue }
|
||||
matches.forEach(match => {
|
||||
if ((match.index > now.index && match.index < then && do_instruction) || (match.index < now.index && i == 0)) {
|
||||
let [full, x, y] = match
|
||||
result += parseInt(x) * parseInt(y) //mul(x, y)
|
||||
multiplications_done += 1
|
||||
}
|
||||
})
|
||||
}
|
||||
console.log(`total multiplications: ${matches.length}`)
|
||||
console.log(`multiplications done: ${multiplications_done}`)
|
||||
console.log(`multiplications ignored: ${matches.length - multiplications_done}`)
|
||||
console.log(`result: ${result}`)
|
||||
|
||||
})
|
Reference in New Issue
Block a user