From 2dfcdbe881cacde73af5841cc336d4ac01505509 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kleszczy=C5=84ski?= Date: Tue, 3 Dec 2024 16:29:29 +0100 Subject: [PATCH] 2024 Day 3 part 2 --- 2024/d03/main.ts | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/2024/d03/main.ts b/2024/d03/main.ts index f796c82..383c036 100644 --- a/2024/d03/main.ts +++ b/2024/d03/main.ts @@ -2,35 +2,27 @@ import { readFileSync } from 'node:fs' import { runExamples, runSolution } from '@magiczne/advent-of-code-ts-core/aoc' -const dontDoMatch = /don't\(\).+?do\(\)/gm -const mulMatch = /mul\((\d+),(\d+)\)/gm +const dontDoMatch = /don't\(\)(.+?)(do\(\)|$)/gs +const mulMatch = /mul\((\d{1,3}),(\d{1,3})\)/gs const part1 = (data: string): number => { return [...data.matchAll(mulMatch)] .map(match => { - return [ - parseInt(match[1], 10), - parseInt(match[2], 10) - ] + return [parseInt(match[1], 10), parseInt(match[2], 10)] }) .map(match => match[0] * match[1]) .reduce((acc, result) => acc + result, 0) } const part2 = (data: string): number => { - const dontDoSections = data.matchAll(dontDoMatch) - const clearData = data.replaceAll(dontDoMatch, '') + data = data.replaceAll(dontDoMatch, '') - return [...clearData.matchAll(mulMatch)] + return [...data.matchAll(mulMatch)] .map(match => { - return [ - parseInt(match[1], 10), - parseInt(match[2], 10) - ] + return [parseInt(match[1], 10), parseInt(match[2], 10)] }) .map(match => match[0] * match[1]) .reduce((acc, result) => acc + result, 0) - } const reader = (file: string): string => { @@ -38,4 +30,4 @@ const reader = (file: string): string => { } runExamples(2024, '03', reader, part1, part2) -runSolution(2024, '03', reader, part1, part2) \ No newline at end of file +runSolution(2024, '03', reader, part1, part2)