Solve 2024-16 (part 1)

This commit is contained in:
2024-12-17 16:10:36 +01:00
parent 25f3706e0f
commit c32296f243
5 changed files with 235 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="2024-16" type="BunRunConfiguration">
<option name="program" value="2024/16/run.ts" />
<option name="workingDirectory" value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
</component>
+95
View File
@@ -0,0 +1,95 @@
import { describe, expect, it } from 'bun:test';
import { solveFirst, solveSecond } from './index.ts';
describe('2024-16', () => {
it('first', () => {
const testInput = `###############
#.......#....E#
#.#.###.#.###.#
#.....#.#...#.#
#.###.#####.#.#
#.#.#.......#.#
#.#.#####.###.#
#...........#.#
###.#.#####.#.#
#...#.....#.#.#
#.#.#.###.#.#.#
#.....#...#.#.#
#.###.#.#.#.#.#
#S..#.....#...#
###############`;
if (process.env.NODE_ENV === 'production') {
expect(solveFirst(testInput)).toBe(7036);
expect(
solveFirst(`#################
#...#...#...#..E#
#.#.#.#.#.#.#.#.#
#.#.#.#...#...#.#
#.#.#.#.###.#.#.#
#...#.#.#.....#.#
#.#.#.#.#.#####.#
#.#...#.#.#.....#
#.#.#####.#.###.#
#.#.#.......#...#
#.#.###.#####.###
#.#.#...#.....#.#
#.#.#.#####.###.#
#.#.#.........#.#
#.#.#.#########.#
#S#.............#
#################`)
).toBe(11048);
expect(
solveFirst(`####################################################
#......................................#..........E#
#......................................#...........#
#....................#.................#...........#
#....................#.................#...........#
#....................#.................#...........#
#....................#.................#...........#
#....................#.................#...........#
#....................#.................#...........#
#....................#.................#...........#
#....................#.................#...........#
#....................#.............................#
#S...................#.............................#
####################################################`)
).toBe(5078);
expect(
solveFirst(`###########################
#######################..E#
######################..#.#
#####################..##.#
####################..###.#
###################..##...#
##################..###.###
#################..####...#
################..#######.#
###############..##.......#
##############..###.#######
#############..####.......#
############..###########.#
###########..##...........#
##########..###.###########
#########..####...........#
########..###############.#
#######..##...............#
######..###.###############
#####..####...............#
####..###################.#
###..##...................#
##..###.###################
#..####...................#
#.#######################.#
#S........................#
###########################`)
).toBe(21148);
}
});
it('second', () => {
// TODO: add second input
const testInput = ``;
// TODO: add second solution
expect(solveSecond(testInput)).toBe(0);
});
});
+43
View File
@@ -0,0 +1,43 @@
import { findCoordsOfDigit } from '../../utils/coordinates.ts';
import { PriorityQueue } from '../../utils/queue.ts';
export function solveFirst(input: string): number {
const map = input.split('\n');
const [x, y] = findCoordsOfDigit(map, 'S')[0];
const start = { x, y, score: 0, dx: 1, dy: 0 };
const possibleOptions = new PriorityQueue<typeof start>();
possibleOptions.enqueue(start, 0);
const visited = new Set<`${number}:${number}:${number}:${number}`>();
visited.add(`${start.x}:${start.y}:${start.dx}:${start.dy}`);
while (possibleOptions.size() > 0) {
const el = possibleOptions.dequeue()!!;
visited.add(`${el.node.x}:${el.node.y}:${el.node.dx}:${el.node.dy}`);
if (map[el.node.y][el.node.x] === 'E') {
return el.node.score;
}
for (let [nx, ny, score, ndx, ndy] of [
[
el.node.x + el.node.dx,
el.node.y + el.node.dy,
el.node.score + 1,
el.node.dx,
el.node.dy
],
[el.node.x, el.node.y, el.node.score + 1000, el.node.dy, -el.node.dx],
[el.node.x, el.node.y, el.node.score + 1000, -el.node.dy, el.node.dx]
]) {
if (map[ny][nx] === '#') continue;
if (visited.has(`${nx}:${ny}:${ndx}:${ndy}`)) continue;
possibleOptions.enqueue({ x: nx, y: ny, score, dx: ndx, dy: ndy }, score);
}
}
return 0;
}
export function solveSecond(input: string): number {
// TODO: add code
return 0;
}
+12
View File
@@ -0,0 +1,12 @@
import { join } from 'path';
import { solveFirst, solveSecond } from './index.ts';
const input = await Bun.file(join(__dirname, 'input.txt')).text();
const firstAnswer = solveFirst(input);
console.log(firstAnswer);
const secondAnswer = solveSecond(input);
console.log(secondAnswer);