From 164549a01de7193b847a5a69c0c251a8c4f583a5 Mon Sep 17 00:00:00 2001 From: Skye Ewers Date: Fri, 1 Dec 2023 23:50:21 +0100 Subject: [PATCH] Day 01 Part 02 --- 01/index.ts | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/01/index.ts b/01/index.ts index 2a0a5cb..496dfd0 100644 --- a/01/index.ts +++ b/01/index.ts @@ -1,11 +1,31 @@ const fs = require('fs').promises; const input: string = (await fs.readFile('./input.txt')).toString() +// const testInput = `two1nine +// eightwothree +// abcone2threexyz +// xtwone3four +// 4nineeightseven2 +// zoneight234 +// 7pqrstsixteen` -console.log("Solution first part: " + partOne()) +const patterns: {[id: string]: string} = { + one: '1', + two: '2', + three: '3', + four: '4', + five: '5', + six: '6', + seven: '7', + eight: '8', + nine: '9' +} + +console.log("Solution first part: " + partOne(input)) +console.log("Solution second part: " + partTwo(input)) -function partOne(): string { +function partOne(input: string): string { let digits = [] for (const line of input.split('\n')) { let first = undefined @@ -27,4 +47,20 @@ function partOne(): string { return `${sum}` } -function partTwo() {} \ No newline at end of file +function partTwo(input: string): string { + let fixedParts: Array = [] + + for (const line of input.split('\n')) { + let modified = line + for (const pattern of Object.keys(patterns)) { + let count = 0 + for (const match of modified.matchAll(new RegExp(pattern, 'g'))) { + let pos = match.index! + count + modified = [modified.slice(0, pos), pattern[0], patterns[pattern], pattern[pattern.length], modified.slice(pos)].join('') + count += 3 + } + } + fixedParts.push(modified) + } + return partOne(fixedParts.join('\n')) +} \ No newline at end of file