-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solve 2024 day 7 and 2023 day 14 part 1
- Loading branch information
Showing
6 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { | ||
Coordinate, | ||
applyOffset, | ||
enumerate, | ||
lines, | ||
loadData, | ||
setValueAtCord, | ||
sumBy, | ||
} from '@helper'; | ||
import { ORTHONAL_OFFSETS } from '../../constants/offset'; | ||
|
||
const input = await loadData(); | ||
const matrix = lines(input, (l) => l.split('')); | ||
|
||
const stoneCoords = matrix.reduce<Coordinate[]>((acc, line, row) => { | ||
return [ | ||
...acc, | ||
...enumerate(line) | ||
.filter(([char]) => char === 'O') | ||
.map(([_, col]) => ({ row: row, col })), | ||
]; | ||
}, []); | ||
|
||
const rollStone = ( | ||
matrix: string[][], | ||
coord: Coordinate, | ||
offset: [number, number], | ||
) => { | ||
let okCoord = coord; | ||
while (true) { | ||
const nextCoord = applyOffset(okCoord, offset); | ||
const nextValue = (matrix[nextCoord.row] ?? [])[nextCoord.col]; | ||
if (nextValue === '.') { | ||
okCoord = nextCoord; | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
return setValueAtCord(setValueAtCord(matrix, coord, '.'), okCoord, 'O'); | ||
}; | ||
|
||
const newMatrix = stoneCoords.reduce( | ||
(acc, coord) => rollStone(acc, coord, ORTHONAL_OFFSETS.bottom), | ||
matrix, | ||
); | ||
|
||
console.log( | ||
sumBy(newMatrix, (line, i) => { | ||
const stoneCount = line.filter((c) => c === 'O').length; | ||
return stoneCount * (matrix.length - i); | ||
}), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { mapToInt, lines, words } from '@helper'; | ||
|
||
export const parse = (input: string) => { | ||
return lines(input, (l) => { | ||
const [testValue, numbers] = l.split(': ', 2); | ||
return { | ||
testValue: parseInt(testValue), | ||
numbers: mapToInt(words(numbers)), | ||
}; | ||
}); | ||
}; | ||
|
||
export const testNumbers = ( | ||
numbers: number[], | ||
testValue: number, | ||
combine: (num1: number, num2: number) => number[], | ||
): boolean => { | ||
if (numbers.length === 1) { | ||
return numbers[0] === testValue; | ||
} | ||
const [first, second] = numbers; | ||
if (first > testValue) { | ||
return false; | ||
} | ||
|
||
return combine(first, second).some((num) => | ||
testNumbers([num, ...numbers.slice(2)], testValue, combine), | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { loadData, sumBy } from '@helper'; | ||
import { parse, testNumbers } from './lib'; | ||
|
||
const input = await loadData(); | ||
const parsed = parse(input); | ||
|
||
console.log( | ||
sumBy( | ||
parsed.filter(({ testValue, numbers }) => { | ||
return testNumbers(numbers, testValue, (num1, num2) => [ | ||
num1 + num2, | ||
num1 * num2, | ||
]); | ||
}), | ||
({ testValue }) => testValue, | ||
), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { loadData, sumBy } from '@helper'; | ||
import { parse, testNumbers } from './lib'; | ||
|
||
const input = await loadData(); | ||
const parsed = parse(input); | ||
|
||
console.log( | ||
sumBy( | ||
parsed.filter(({ testValue, numbers }) => { | ||
return testNumbers(numbers, testValue, (num1, num2) => [ | ||
num1 + num2, | ||
num1 * num2, | ||
parseInt(`${num1}${num2}`), | ||
]); | ||
}), | ||
({ testValue }) => testValue, | ||
), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
O....#.... | ||
O.OO#....# | ||
.....##... | ||
OO.#O....O | ||
.O.....O#. | ||
O.#..O.#.# | ||
..O..#O..O | ||
.......O.. | ||
#....###.. | ||
#OO..#.... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
190: 10 19 | ||
3267: 81 40 27 | ||
83: 17 5 | ||
156: 15 6 | ||
7290: 6 8 6 15 | ||
161011: 16 10 13 | ||
192: 17 8 14 | ||
21037: 9 7 18 13 | ||
292: 11 6 16 20 |