Skip to content

Commit

Permalink
Solve 2024 day 7 and 2023 day 14 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Dlurak committed Dec 7, 2024
1 parent dcdffda commit 9b5ca7f
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 0 deletions.
53 changes: 53 additions & 0 deletions 2023/day14/part1.ts
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);
}),
);
29 changes: 29 additions & 0 deletions 2024/day07/lib.ts
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),
);
};
17 changes: 17 additions & 0 deletions 2024/day07/part1.ts
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,
),
);
18 changes: 18 additions & 0 deletions 2024/day07/part2.ts
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,
),
);
10 changes: 10 additions & 0 deletions input/2023/day14.debug
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..#....
9 changes: 9 additions & 0 deletions input/2024/day07.debug
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

0 comments on commit 9b5ca7f

Please sign in to comment.