Skip to content

Commit ec8e6bb

Browse files
committed
feat: day 7 bridge repair 1/2 soln, #12
1 parent 2a85433 commit ec8e6bb

File tree

9 files changed

+151
-4
lines changed

9 files changed

+151
-4
lines changed

src/2024/2024-12-06/lib/guardController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const guardController = (data: string[][], printTrail: boolean = false):
1919
let yDirection = 0
2020
let xDirection = 0
2121

22-
// Find the guard's direction
22+
// Set the guard's direction in the grid
2323
switch(guard.direction) {
2424
case GuardDirection.UP:
2525
yDirection = -1

src/2024/2024-12-06/lib/guardControllerLoop.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const gridHasInfiniteLoop = (
3232
let yDirection = 0
3333
let xDirection = 0
3434

35-
// Find the guard's direction
35+
// Set the guard's direction in the grid
3636
switch(guard.direction) {
3737
case GuardDirection.UP:
3838
yDirection = -1

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ test('Count distinct guard positions', () => {
1616
expect(guardController(file).positionCount).toBe(26)
1717
})
1818

19-
test('Count obstackle positions', () => {
19+
test('Count obstacle positions', () => {
2020
expect(findObstructionPositions(file)).toBe(2)
2121
})

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## Day 7: Bridge Repair
2+
3+
Visit the Advent of Code website for more information on this puzzle at:
4+
5+
**Source:** https://adventofcode.com/2024/day/7<br>
6+
**Status:** On-going ⭐
7+
8+
## Code
9+
10+
1. **totalCalibration.ts**
11+
- `operatorCombinations()` - Finds the total number of possible combinations in which to place operator symbols for an `N`-length array
12+
13+
- `doEquation()` - Returns the result of an AoC text equation and a set of operator symbols with the condition: left to write equation processing, disregarding operator precedence
14+
15+
- `totalCalibrationResult()` - Counts the total calibration sum of input lines whose elements (numbers) match the line's target sum after processing with one of N possible combinations of `+` and `*` operands

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
190: 10 19
2+
3267: 81 40 27
3+
83: 17 5
4+
156: 15 6
5+
7290: 6 8 6 15
6+
161011: 16 10 13
7+
192: 17 8 14
8+
21037: 9 7 18 13
9+
292: 11 6 16 20
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Finds the total number of possible combinations in which to place operator symbols for an `N`-length array
3+
* @param {string[]} operators String array containing operator symbols
4+
* @param {number} N Length of a linear array
5+
* @returns {number} Total number
6+
*/
7+
const operatorCombinations = (
8+
operators: string[] = ['+', '*'],
9+
N: number
10+
) => {
11+
const combinations: string[][] = []
12+
const totalCombinations = Math.pow(operators.length, N)
13+
14+
for (let i = 0; i < totalCombinations; i += 1) {
15+
const list: string[] = []
16+
let seed = i
17+
18+
for (let j = 0; j < N; j += 1) {
19+
list.push(operators[seed % operators.length] as string)
20+
seed = Math.floor(seed / operators.length)
21+
}
22+
23+
combinations.push(list)
24+
}
25+
26+
return combinations
27+
}
28+
29+
/**
30+
* Returns the result of an AoC text equation and a set of operator symbols with the condition:
31+
* - Left to write equation processing, disregarding operator precedence
32+
* @param {string} eqnString Math equation expressed as string
33+
* @returns {number} Result of the `eqnString`
34+
*/
35+
const doEquation = (numbers: number[], operators: string[]): number => {
36+
let sum = numbers[0] as number
37+
38+
operators.forEach((operator, index) => {
39+
if (operator === '+') {
40+
sum += numbers[index + 1] as number
41+
}
42+
43+
if (operator === '*') {
44+
sum *= numbers[index + 1] as number
45+
}
46+
})
47+
48+
return sum
49+
}
50+
51+
/**
52+
* Counts the total calibration sum of input lines whose elements (numbers) match the line's target sum
53+
* after processing with one of N possible combinations of `+` and `*` operands
54+
* @param input Input string array
55+
* @returns {number} Total calibration sum
56+
*/
57+
export const totalCalibrationResult = (input: string[]): number => {
58+
const operators = ['+', '*']
59+
let sum = 0
60+
61+
for (let i = 0; i < input.length; i += 1) {
62+
const line = input[i]
63+
64+
if (line === undefined) break
65+
const [targetSum, data] = line.split(': ')
66+
const numbers = data?.split(' ').map(Number) as number[]
67+
68+
// Find all operator placement combinations
69+
const combinations = operatorCombinations(operators, numbers.length - 1)
70+
71+
// Build the text equation
72+
for (let j = 0; j < combinations.length; j += 1) {
73+
// Process equation
74+
const result = doEquation(numbers, combinations[j] as string[])
75+
76+
if (result === Number(targetSum)) {
77+
sum += result
78+
break
79+
}
80+
}
81+
}
82+
83+
return sum
84+
}

src/2024/2024-12-07/main.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import path from 'path'
2+
import { AOC_OUTPUT_TYPE, readAOCInputFile } from '@/utils/aocInputFile.js'
3+
import { currentDirectory } from '@/utils/file.js'
4+
5+
import { totalCalibrationResult } from './lib/totalCalibration.js'
6+
7+
// Read and process the input file
8+
const input = (readAOCInputFile({
9+
filePath: path.join(currentDirectory(import.meta.url), 'input.txt'),
10+
type: AOC_OUTPUT_TYPE.STRING
11+
}) as string)
12+
.split('\n')
13+
14+
/**
15+
* Part 1/2 of the 2024-12-07 quiz
16+
* Counts the total calibration result of the input data
17+
*/
18+
const quiz20241221_01 = () => {
19+
totalCalibrationResult(input)
20+
}
21+
22+
quiz20241221_01()

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import path from 'path'
2+
import { test, expect } from 'vitest'
3+
4+
import { readAOCInputFile, AOC_OUTPUT_TYPE } from '@/utils/aocInputFile.js'
5+
import { currentDirectory } from '@/utils/file.js'
6+
7+
import { totalCalibrationResult } from './lib/totalCalibration.js'
8+
9+
// Read and process the input file
10+
const input = (readAOCInputFile({
11+
filePath: path.join(currentDirectory(import.meta.url), 'input.txt'),
12+
type: AOC_OUTPUT_TYPE.STRING
13+
}) as string)
14+
.split('\n')
15+
16+
test('1/2: Total calibration result', () => {
17+
expect(totalCalibrationResult(input)).toBe(3749)
18+
})

src/utils/aocInputFile.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ type Output = string | string[] | string[][] |
2525
* @param param.filePath {string} Full file path to an input text file
2626
* @param param.type {AOC_OUTPUT_TYPE} Type to convert the input text file
2727
* @param param.delimiter {string} String delimiter
28-
* @param moduleFile {string} File URL of the current module being executed: `"import.meta.url"`
2928
* @throws {Error}
3029
*/
3130
export const readAOCInputFile = (param: FileInput): Output => {

0 commit comments

Comments
 (0)