Skip to content

Commit

Permalink
2024 Day 3 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Magiczne committed Dec 3, 2024
1 parent ecd0388 commit 2dfcdbe
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions 2024/d03/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,32 @@ 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 => {
return readFileSync(file, 'utf-8').trim()
}

runExamples(2024, '03', reader, part1, part2)
runSolution(2024, '03', reader, part1, part2)
runSolution(2024, '03', reader, part1, part2)

0 comments on commit 2dfcdbe

Please sign in to comment.