From d8c28ae3c08490fbd6f80c1d53d077ec7d2155ae Mon Sep 17 00:00:00 2001 From: Alix von Schirp Date: Mon, 9 Dec 2024 08:10:43 +0100 Subject: [PATCH] Day 3 --- Day 03/Part 1/index.ts | 24 ++++++++++++++++++++++++ Day 03/Part 2/index.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 Day 03/Part 1/index.ts create mode 100644 Day 03/Part 2/index.ts diff --git a/Day 03/Part 1/index.ts b/Day 03/Part 1/index.ts new file mode 100644 index 0000000..d9f5e4b --- /dev/null +++ b/Day 03/Part 1/index.ts @@ -0,0 +1,24 @@ +import fs from 'node:fs'; + +export default function runner(input: string) { + fs.readFile(`./${input}`, 'utf8', (err, data) => { + if (err) throw err; + let count = 0; + + const regex = /mul\((?\d{1,3}),(?\d{1,3})\)/gm; + + let m; + + while ((m = regex.exec(data)) !== null) { + if (m.index === regex.lastIndex) { + regex.lastIndex++; + } + + count += parseInt(m[1]??"")*parseInt(m[2]??"") + } + + + + console.log(count) + }) +} \ No newline at end of file diff --git a/Day 03/Part 2/index.ts b/Day 03/Part 2/index.ts new file mode 100644 index 0000000..7218bfc --- /dev/null +++ b/Day 03/Part 2/index.ts @@ -0,0 +1,27 @@ +import fs from 'node:fs'; + +export default function runner(input: string) { + fs.readFile(`./${input}`, 'utf8', (err, data) => { + if (err) throw err; + let count = 0; + + data.split("do()").forEach(value => { + value.split("don't()").forEach((value1, index) => { + if(index != 0) return + const regex = /mul\((?\d{1,3}),(?\d{1,3})\)/gm; + + let m; + + while ((m = regex.exec(value1)) !== null) { + if (m.index === regex.lastIndex) { + regex.lastIndex++; + } + + count += parseInt(m[1]??"")*parseInt(m[2]??"") + } + }) + }) + + console.log(count) + }) +} \ No newline at end of file