day 3, made reusable

This commit is contained in:
Joshua Seigler 2024-12-03 01:15:25 -05:00
parent 9cb3d6b5a4
commit 1f80d9c4d1
4 changed files with 101 additions and 12 deletions

View file

@ -33,16 +33,16 @@
},
{
"part1": {
"solved": false,
"result": null,
"solved": true,
"result": "189600467",
"attempts": [],
"time": null
"time": 0.810574
},
"part2": {
"solved": false,
"result": null,
"solved": true,
"result": "107069718",
"attempts": [],
"time": null
"time": 1.666723
}
},
{

View file

@ -13,7 +13,7 @@
[![Day](https://badgen.net/badge/01/%E2%98%85%E2%98%85/green)](src/day01)
[![Day](https://badgen.net/badge/02/%E2%98%85%E2%98%85/green)](src/day02)
![Day](https://badgen.net/badge/03/%E2%98%86%E2%98%86/gray)
[![Day](https://badgen.net/badge/03/%E2%98%85%E2%98%85/green)](src/day03)
![Day](https://badgen.net/badge/04/%E2%98%86%E2%98%86/gray)
![Day](https://badgen.net/badge/05/%E2%98%86%E2%98%86/gray)
![Day](https://badgen.net/badge/06/%E2%98%86%E2%98%86/gray)
@ -83,9 +83,9 @@ Both parts: 3.539ms
```
Day 03
Time part 1: -
Time part 2: -
Both parts: -
Time part 1: 0.657ms
Time part 2: 0.406ms
Both parts: 1.064ms
```
```
@ -243,8 +243,8 @@ Both parts: -
```
```
Total stars: 4/50
Total time: 5.883ms
Total stars: 6/50
Total time: 6.946ms
```
<!--/RESULTS-->

9
src/day03/README.md Normal file
View file

@ -0,0 +1,9 @@
# 🎄 Advent of Code 2024 - day 3 🎄
## Info
Task description: [link](https://adventofcode.com/2024/day/3)
## Notes
...

80
src/day03/index.ts Normal file
View file

@ -0,0 +1,80 @@
import run from "aocrunner"
type State = {
mulEnabled: boolean
total: number
}
type Instruction = "do"|"don't"|"mul"
const instructions: Record<Instruction, (state: State, args: string[]) => State> = {
"do": (state, _args) => {
return {
...state,
mulEnabled: true
}
},
"don't": (state, _args) => {
return {
...state,
mulEnabled: false
}
},
"mul": (state, [a, b]) => {
return {
...state,
total: state.total + (state.mulEnabled ? parseInt(a) * parseInt(b) : 0)
}
}
}
const parseInput = (rawInput: string) => {
const tokenRegexp = new RegExp(`(${Object.keys(instructions).join('|')})\\(((\\d+,?)*)\\)`, 'g')
const matches = rawInput.matchAll(tokenRegexp)
return Array.from(matches).map(match => {
const [, instruction, parameters] = match
return [instruction as Instruction, parameters === "" ? [] : parameters.split(',')] as const
})
}
const part1 = (rawInput: string) => {
const input = rawInput
const matches = input.matchAll(/mul\((\d+),(\d+)\)/g)
return Array.from(matches).reduce((acc, m) => {
const [,a,b] = m
return acc + parseInt(a) * parseInt(b)
}, 0)
}
const part2 = (rawInput: string) => {
const program = parseInput(rawInput)
let state: State = {
total: 0,
mulEnabled: true
}
program.forEach(([instruction, parameters]) => {
state = instructions[instruction](state, parameters)
})
return state.total
}
run({
part1: {
tests: [
{
input: `xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))`,
expected: 161,
},
],
solution: part1,
},
part2: {
tests: [
{
input: `xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))`,
expected: 48,
},
],
solution: part2,
},
trimTestInputs: true,
onlyTests: false,
})