Skip to content

feat: day 3 mull it over 20241203 #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This repository contains solutions and a local development environment for the [

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

</details>

Expand Down
1 change: 0 additions & 1 deletion src/2024/2024-12-02/sample.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export const data: number[][] = `7 6 4 2 1
.split('\n')
.map(row => row.split(' ').map(Number))


test('countSafeReports - demo', () => {
expect(countSafeReports(data)).toBe(2)
})
Expand Down
57 changes: 57 additions & 0 deletions src/2024/2024-12-03/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
## Day 3: Mull It Over

**Source:** https://adventofcode.com/2024/day/3<br>
**Status:** On-Going

### 📘 Part 1

"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.

The shopkeeper turns to you. "Any chance you can see why our computers are having issues again?"

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!

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`.

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.

For example, consider the following section of corrupted memory:

```text
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
```

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)`.

Scan the corrupted memory for uncorrupted mul instructions. What do you get if you add up all of the results of the multiplications?

> [!TIP]
> **Your puzzle answer was** 187194524.

---

### 📗 Part 2

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.

There are two new instructions you'll need to handle:

- The `do()` instruction enables future mul instructions.
- The `don't()` instruction disables future mul instructions.

Only the most recent `do()` or `don't()` instruction applies. At the beginning of the program, mul instructions are enabled.

For example:

```text
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
```

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.

This time, the sum of the results is `48 (2*4 + 8*5)`.

Handle the new instructions; what do you get if you add up all of the results of just the enabled multiplications?

> [!TIP]
> **Your puzzle answer was** 127092535.
6 changes: 6 additions & 0 deletions src/2024/2024-12-03/input.txt

Large diffs are not rendered by default.

65 changes: 65 additions & 0 deletions src/2024/2024-12-03/lib/extractMultiply.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const mul = (x: number, y: number): number => {
if (x === undefined || y === undefined) {
throw new Error('Missing operand')
}

return x * y
}

/**
* Extracts and processes valid mul(x,y) sequences from long text and returns their sum
* @param corruptedInstructions {string} Corrupted multiply instructions string
* @returns {number} Sum of valid mul(x,y) operations from corrupted string
*/
export const extractMultiply = (corruptedInstructions: string): number => {
const instances = corruptedInstructions.split('mul(')

// 3 per number (2x), 1=comma, 1=closing parenthesis
const maxStringLength = 8
let multiplySum = 0

for (let i = 0; i < instances.length; i += 1) {
const line = (instances[i] as string).substring(0, maxStringLength)
const multiplyText = 'mul(' + line.substring(0, line.indexOf(')') + 1)

try {
multiplySum += eval(multiplyText)
} catch (err: unknown) {
if (err instanceof Error) {
// console.log('eval parsing error:', err.message)
}
}
}

return multiplySum
}

/**
* Extracts and processes valid mul(x,y) sequences from long text that are preceeded with `do()` conditions.
* Ignores mul(x,y) sequences preceeded by `don't()` conditions.
* @param corruptedInstructions {string} Corrupted multiply instructions string
* @returns Sum of valid mul(x,y) operations from corrupted string
*/
export const extractMultiplyCondition = (corruptedInstructions: string): number => {
let multiplySum = 0

// Clean and normalize text
const instances = corruptedInstructions
.split('do()')
.map(line => {
const indexOfDont = line.indexOf('don\'t()')

if (indexOfDont >= 0) {
return line.substring(0, indexOfDont)
}

return line
})

for (let i = 0; i < instances.length; i += 1) {
multiplySum += extractMultiply(instances[i] as string)
}

return multiplySum
}
10 changes: 10 additions & 0 deletions src/2024/2024-12-03/quiz.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { test, expect } from 'vitest'
import { quiz20241203_01, quiz20241203_02 } from './quiz.js'

test('sum of mul(x,y) texts', () => {
expect(quiz20241203_01()).toBe(187194524)
})

test('sum of mul(x,y) texts', () => {
expect(quiz20241203_02()).toBe(127092535)
})
28 changes: 28 additions & 0 deletions src/2024/2024-12-03/quiz.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import path from 'path'
import { currentDirectory, readFile } from '@/utils/file.js'
import { extractMultiply, extractMultiplyCondition } from './lib/extractMultiply.js'

const directory = currentDirectory(import.meta.url)
const input = readFile(path.join(directory, 'input.txt'))

/**
* Part 1/2 of the 2024-12-03 quiz
* @returns {number} Sum of "mul(x,y)" strings from long text
*/
export const quiz20241203_01 = (): number => {
const sumMultiply = extractMultiply(input)

console.log('MULTIPLICATION RESULTS:', sumMultiply)
return sumMultiply
}

/**
* Part 2/2 of the 2024-12-03 quiz
* @returns {number} Sum of "mul(x,y)" strings with conditions from long text
*/
export const quiz20241203_02 = (): number => {
const sumMultiply = extractMultiplyCondition(input)

console.log('MULTIPLICATION WITH CONDITION:', sumMultiply)
return sumMultiply
}
15 changes: 15 additions & 0 deletions src/2024/2024-12-03/sample.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { test, expect } from 'vitest'
import { extractMultiply, extractMultiplyCondition } from './lib/extractMultiply.js'

const data = 'xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))'

// eslint-disable-next-line
const dataCondition = "xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))"

test('extract mul() and process - demo', () => {
expect(extractMultiply(data)).toBe(161)
})

test('extract and process mul() with do() - demo', () => {
expect(extractMultiplyCondition(dataCondition)).toBe(48)
})
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export { greet } from './sample/greet.js'
export { getTotalDistance } from './2024/2024-12-01/lib/listTotalDistance.js'
export { similarityScore } from './2024/2024-12-01/lib/similarityScore.js'
export { countSafeReports } from './2024/2024-12-02/lib/countSafeReports.js'
export { extractMultiply } from './2024/2024-12-03/lib/extractMultiply.js'
export { extractMultiplyCondition } from './2024/2024-12-03/lib/extractMultiply.js'
13 changes: 13 additions & 0 deletions src/utils/numbers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Checks if a number-like input is a number
* @param str {string | nummber} Input parameter
* @returns {bool} Flag if the input parameter is a number
*/
export const isNumber = (input: string | number): boolean => {
if (
typeof input !== 'string' &&
typeof input !== 'number'
) return false

return !isNaN(Number(input))
}
Loading