Skip to content

Commit 700a827

Browse files
authored
Merge pull request #20 from weaponsforge/feat/weaponsforge-8
feat: day 3 mull it over 20241203
2 parents 822b6e0 + 8257b09 commit 700a827

File tree

10 files changed

+198
-1
lines changed

10 files changed

+198
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This repository contains solutions and a local development environment for the [
99

1010
- Day 1: Historian Hysteria [[link]](/src/2024/2024-12-01/README.md)
1111
- Day 2: Red-Nosed Reports [[link]](/src/2024/2024-12-02/README.md)
12+
- Day 3: Mull It Over [[link]](/src/2024/2024-12-03/README.md)
1213

1314
</details>
1415

src/2024/2024-12-02/sample.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export const data: number[][] = `7 6 4 2 1
1111
.split('\n')
1212
.map(row => row.split(' ').map(Number))
1313

14-
1514
test('countSafeReports - demo', () => {
1615
expect(countSafeReports(data)).toBe(2)
1716
})

src/2024/2024-12-03/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
## Day 3: Mull It Over
2+
3+
**Source:** https://adventofcode.com/2024/day/3<br>
4+
**Status:** On-Going
5+
6+
### 📘 Part 1
7+
8+
"Our computers are having issues, so I have no idea if we have any Chief Historians in stock! You're welcome to check the warehouse, though," says the mildly flustered shopkeeper at the North Pole Toboggan Rental Shop. The Historians head out to take a look.
9+
10+
The shopkeeper turns to you. "Any chance you can see why our computers are having issues again?"
11+
12+
The computer appears to be trying to run a program, but its memory (your puzzle input) is corrupted. All of the instructions have been jumbled up!
13+
14+
It seems like the goal of the program is just to multiply some numbers. It does that with instructions like `mul(X,Y)`, where `X` and `Y` are each 1-3 digit numbers. For instance, `mul(44,46)` multiplies `44` by `46` to get a result of `2024`. Similarly, `mul(123,4)` would multiply `123` by `4`.
15+
16+
However, because the program's memory has been corrupted, there are also many invalid characters that should be ignored, even if they look like part of a mul instruction. Sequences like `mul(4*, mul(6,9!, ?(12,34)`, or `mul ( 2 , 4 )` do nothing.
17+
18+
For example, consider the following section of corrupted memory:
19+
20+
```text
21+
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
22+
```
23+
24+
Only the four highlighted sections are real mul instructions. Adding up the result of each instruction produces `161 (2*4 + 5*5 + 11*8 + 8*5)`.
25+
26+
Scan the corrupted memory for uncorrupted mul instructions. What do you get if you add up all of the results of the multiplications?
27+
28+
> [!TIP]
29+
> **Your puzzle answer was** 187194524.
30+
31+
---
32+
33+
### 📗 Part 2
34+
35+
As you scan through the corrupted memory, you notice that some of the conditional statements are also still intact. If you handle some of the uncorrupted conditional statements in the program, you might be able to get an even more accurate result.
36+
37+
There are two new instructions you'll need to handle:
38+
39+
- The `do()` instruction enables future mul instructions.
40+
- The `don't()` instruction disables future mul instructions.
41+
42+
Only the most recent `do()` or `don't()` instruction applies. At the beginning of the program, mul instructions are enabled.
43+
44+
For example:
45+
46+
```text
47+
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
48+
```
49+
50+
This corrupted memory is similar to the example from before, but this time the `mul(5,5)` and `mul(11,8)` instructions are disabled because there is a don't() instruction before them. The other mul instructions function normally, including the one at the end that gets re-enabled by a do() instruction.
51+
52+
This time, the sum of the results is `48 (2*4 + 8*5)`.
53+
54+
Handle the new instructions; what do you get if you add up all of the results of just the enabled multiplications?
55+
56+
> [!TIP]
57+
> **Your puzzle answer was** 127092535.

src/2024/2024-12-03/input.txt

Lines changed: 6 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2+
const mul = (x: number, y: number): number => {
3+
if (x === undefined || y === undefined) {
4+
throw new Error('Missing operand')
5+
}
6+
7+
return x * y
8+
}
9+
10+
/**
11+
* Extracts and processes valid mul(x,y) sequences from long text and returns their sum
12+
* @param corruptedInstructions {string} Corrupted multiply instructions string
13+
* @returns {number} Sum of valid mul(x,y) operations from corrupted string
14+
*/
15+
export const extractMultiply = (corruptedInstructions: string): number => {
16+
const instances = corruptedInstructions.split('mul(')
17+
18+
// 3 per number (2x), 1=comma, 1=closing parenthesis
19+
const maxStringLength = 8
20+
let multiplySum = 0
21+
22+
for (let i = 0; i < instances.length; i += 1) {
23+
const line = (instances[i] as string).substring(0, maxStringLength)
24+
const multiplyText = 'mul(' + line.substring(0, line.indexOf(')') + 1)
25+
26+
try {
27+
multiplySum += eval(multiplyText)
28+
} catch (err: unknown) {
29+
if (err instanceof Error) {
30+
// console.log('eval parsing error:', err.message)
31+
}
32+
}
33+
}
34+
35+
return multiplySum
36+
}
37+
38+
/**
39+
* Extracts and processes valid mul(x,y) sequences from long text that are preceeded with `do()` conditions.
40+
* Ignores mul(x,y) sequences preceeded by `don't()` conditions.
41+
* @param corruptedInstructions {string} Corrupted multiply instructions string
42+
* @returns Sum of valid mul(x,y) operations from corrupted string
43+
*/
44+
export const extractMultiplyCondition = (corruptedInstructions: string): number => {
45+
let multiplySum = 0
46+
47+
// Clean and normalize text
48+
const instances = corruptedInstructions
49+
.split('do()')
50+
.map(line => {
51+
const indexOfDont = line.indexOf('don\'t()')
52+
53+
if (indexOfDont >= 0) {
54+
return line.substring(0, indexOfDont)
55+
}
56+
57+
return line
58+
})
59+
60+
for (let i = 0; i < instances.length; i += 1) {
61+
multiplySum += extractMultiply(instances[i] as string)
62+
}
63+
64+
return multiplySum
65+
}

src/2024/2024-12-03/quiz.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { test, expect } from 'vitest'
2+
import { quiz20241203_01, quiz20241203_02 } from './quiz.js'
3+
4+
test('sum of mul(x,y) texts', () => {
5+
expect(quiz20241203_01()).toBe(187194524)
6+
})
7+
8+
test('sum of mul(x,y) texts', () => {
9+
expect(quiz20241203_02()).toBe(127092535)
10+
})

src/2024/2024-12-03/quiz.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import path from 'path'
2+
import { currentDirectory, readFile } from '@/utils/file.js'
3+
import { extractMultiply, extractMultiplyCondition } from './lib/extractMultiply.js'
4+
5+
const directory = currentDirectory(import.meta.url)
6+
const input = readFile(path.join(directory, 'input.txt'))
7+
8+
/**
9+
* Part 1/2 of the 2024-12-03 quiz
10+
* @returns {number} Sum of "mul(x,y)" strings from long text
11+
*/
12+
export const quiz20241203_01 = (): number => {
13+
const sumMultiply = extractMultiply(input)
14+
15+
console.log('MULTIPLICATION RESULTS:', sumMultiply)
16+
return sumMultiply
17+
}
18+
19+
/**
20+
* Part 2/2 of the 2024-12-03 quiz
21+
* @returns {number} Sum of "mul(x,y)" strings with conditions from long text
22+
*/
23+
export const quiz20241203_02 = (): number => {
24+
const sumMultiply = extractMultiplyCondition(input)
25+
26+
console.log('MULTIPLICATION WITH CONDITION:', sumMultiply)
27+
return sumMultiply
28+
}

src/2024/2024-12-03/sample.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { test, expect } from 'vitest'
2+
import { extractMultiply, extractMultiplyCondition } from './lib/extractMultiply.js'
3+
4+
const data = 'xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))'
5+
6+
// eslint-disable-next-line
7+
const dataCondition = "xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))"
8+
9+
test('extract mul() and process - demo', () => {
10+
expect(extractMultiply(data)).toBe(161)
11+
})
12+
13+
test('extract and process mul() with do() - demo', () => {
14+
expect(extractMultiplyCondition(dataCondition)).toBe(48)
15+
})

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
export { greet } from './sample/greet.js'
22
export { getTotalDistance } from './2024/2024-12-01/lib/listTotalDistance.js'
33
export { similarityScore } from './2024/2024-12-01/lib/similarityScore.js'
4+
export { countSafeReports } from './2024/2024-12-02/lib/countSafeReports.js'
5+
export { extractMultiply } from './2024/2024-12-03/lib/extractMultiply.js'
6+
export { extractMultiplyCondition } from './2024/2024-12-03/lib/extractMultiply.js'

src/utils/numbers.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Checks if a number-like input is a number
3+
* @param str {string | nummber} Input parameter
4+
* @returns {bool} Flag if the input parameter is a number
5+
*/
6+
export const isNumber = (input: string | number): boolean => {
7+
if (
8+
typeof input !== 'string' &&
9+
typeof input !== 'number'
10+
) return false
11+
12+
return !isNaN(Number(input))
13+
}

0 commit comments

Comments
 (0)