Skip to content

Commit 4d8ba59

Browse files
committed
Year 2024, Day 15, Part 1
1 parent b3c8a99 commit 4d8ba59

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

clients/typescript/solutions/S2415.ts

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import ISolution from "./ISolution.ts";
2+
3+
export default class S2415 implements ISolution {
4+
firstPart(input: string): (number | string) | Promise<number | string> {
5+
const [ rawmap, rawinstructions ] = input.split("\n\n");
6+
const map = rawmap.split("\n").map((row) => row.split(""));
7+
const instructions = rawinstructions.split("");
8+
for (const instruction of instructions) {
9+
const y = map.findIndex((row) => row.includes("@"));
10+
const x = map[ y ].indexOf("@");
11+
const move = this.moveable(map, x, y, instruction);
12+
if (move > 0) {
13+
if (instruction === "<") {
14+
15+
map[ y ][ x ] = ".";
16+
map[ y ][ x - 1 ] = "@";
17+
if (move > 1) {
18+
map[ y ][ x - move ] = "O";
19+
}
20+
}
21+
if (instruction === ">") {
22+
map[ y ][ x ] = ".";
23+
map[ y ][ x + 1 ] = "@";
24+
if (move > 1) {
25+
map[ y ][ x + move ] = "O";
26+
}
27+
}
28+
if (instruction === "^") {
29+
map[ y ][ x ] = ".";
30+
map[ y - 1 ][ x ] = "@";
31+
if (move > 1) {
32+
map[ y - move ][ x ] = "O";
33+
}
34+
}
35+
if (instruction === "v") {
36+
map[ y ][ x ] = ".";
37+
map[ y + 1 ][ x ] = "@";
38+
if (move > 1) {
39+
map[ y + move ][ x ] = "O";
40+
}
41+
}
42+
}
43+
}
44+
// console.log(map.map((row) => row.join("")).join("\n"));
45+
return map.map((row, y) => row.map((cell, x) => cell === "O" ? 100*y+x : 0)).flat().reduce((a, b) => a + b);
46+
}
47+
moveable(map: string[][], x: number, y: number, direction: string): number {
48+
if (direction === "<") {
49+
if (map[ y ][ x - 1 ] === "#") {
50+
return 0;
51+
}
52+
if (map[ y ][ x - 1 ] === "O") {
53+
const moves = this.moveable(map, x - 1, y, direction);
54+
return moves > 0 ? moves + 1 : 0;
55+
}
56+
if (map[y][x-1] === "]") {
57+
const moves = this.moveable(map, x - 2, y, direction);
58+
return moves > 0 ? moves + 2 : 0;
59+
}
60+
return 1;
61+
}
62+
if (direction === ">") {
63+
if (map[ y ][ x + 1 ] === "#") {
64+
return 0;
65+
}
66+
if (map[ y ][ x + 1 ] === "O") {
67+
const moves = this.moveable(map, x + 1, y, direction);
68+
return moves > 0 ? moves + 1 : 0;
69+
}
70+
if (map[y][x+1] === "[") {
71+
const moves = this.moveable(map, x + 2, y, direction);
72+
return moves > 0 ? moves + 2 : 0;
73+
}
74+
return 1;
75+
}
76+
if (direction === "^") {
77+
if (map[ y - 1 ][ x ] === "#") {
78+
return 0;
79+
}
80+
if (map[ y - 1 ][ x ] === "O") {
81+
const moves = this.moveable(map, x, y - 1, direction);
82+
return moves > 0 ? moves + 1 : 0;
83+
}
84+
if (map[y-1][x] === "[") {
85+
const moves1 = this.moveable(map, x, y - 1, direction);
86+
const moves2 = this.moveable(map, x+1, y - 1, direction);
87+
return Math.min(moves1, moves2) > 0 ? Math.max(moves1, moves2) + 1 : 0;
88+
}
89+
if (map[y-1][x] === "]") {
90+
const moves1 = this.moveable(map, x, y - 1, direction);
91+
const moves2 = this.moveable(map, x-1, y - 1, direction);
92+
return Math.min(moves1, moves2) > 0 ? Math.max(moves1, moves2) + 1 : 0;
93+
}
94+
return 1;
95+
}
96+
if (direction === "v") {
97+
if (map[ y + 1 ][ x ] === "#") {
98+
return 0;
99+
}
100+
if (map[ y + 1 ][ x ] === "O") {
101+
const moves = this.moveable(map, x, y + 1, direction);
102+
return moves > 0 ? moves + 1 : 0;
103+
}
104+
return 1;
105+
}
106+
return 0;
107+
}
108+
secondPart(input: string): (number | string) | Promise<number | string> {
109+
return "";
110+
}
111+
112+
}

0 commit comments

Comments
 (0)