Skip to content

Commit 727ef33

Browse files
committed
Year 2024, Day 6
1 parent e924829 commit 727ef33

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

clients/typescript/solutions/S2406.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import ISolution from "./ISolution.ts";
2-
import routine from "https://deno.land/x/routine/mod.ts";
32

43
export default class S2406 implements ISolution {
54
firstPart(input: string): (number | string) | Promise<number | string> {
@@ -14,7 +13,7 @@ export default class S2406 implements ISolution {
1413
}
1514
}
1615
}
17-
const poss: {x: number, y: number}[] = [];
16+
const poss: { x: number, y: number }[] = [];
1817
poss.push({ x: currentPos.x, y: currentPos.y });
1918
walk: while (true) {
2019
switch (direction) {
@@ -69,11 +68,10 @@ export default class S2406 implements ISolution {
6968
}
7069
return poss.filter((pos, index) => poss.findIndex(p => p.x === pos.x && p.y === pos.y) === index).length;
7170
}
72-
async secondPart(input: string): Promise<string | number> {
71+
secondPart(input: string) {
7372
const coords = input.split("\n").map(line => line.split(""));
74-
function tryWith(coords: string[][]): number {
75-
const poss: {x: number, y: number}[] = [];
76-
const turn: {x: number, y: number, t: string}[] = [];
73+
function tryWith(coords: string[][]): [ number, { x: number, y: number }[] ] {
74+
const turn: { x: number, y: number, t: string }[] = [];
7775
let direction = "up";
7876
const currentPos = { x: 0, y: 0 };
7977
for (const y in coords) {
@@ -84,10 +82,11 @@ export default class S2406 implements ISolution {
8482
}
8583
}
8684
}
85+
const poss: { x: number, y: number }[] = [];
8786
poss.push({ x: currentPos.x, y: currentPos.y });
8887
walk: while (true) {
8988
if (turn.some((t, i) => turn.findIndex(tt => tt.x === t.x && tt.y === t.y && tt.t === t.t) !== i)) {
90-
return 1;
89+
return [ 1, poss ];
9190
}
9291
switch (direction) {
9392
case "up":
@@ -99,6 +98,7 @@ export default class S2406 implements ISolution {
9998
turn.push({ x: currentPos.x, y: currentPos.y, t: "right" });
10099
} else {
101100
currentPos.y--;
101+
poss.push({ x: currentPos.x, y: currentPos.y });
102102
}
103103
break;
104104
case "right":
@@ -110,6 +110,7 @@ export default class S2406 implements ISolution {
110110
turn.push({ x: currentPos.x, y: currentPos.y, t: "down" });
111111
} else {
112112
currentPos.x++;
113+
poss.push({ x: currentPos.x, y: currentPos.y });
113114
}
114115
break;
115116
case "down":
@@ -121,6 +122,7 @@ export default class S2406 implements ISolution {
121122
turn.push({ x: currentPos.x, y: currentPos.y, t: "left" });
122123
} else {
123124
currentPos.y++;
125+
poss.push({ x: currentPos.x, y: currentPos.y });
124126
}
125127
break;
126128
case "left":
@@ -132,23 +134,23 @@ export default class S2406 implements ISolution {
132134
turn.push({ x: currentPos.x, y: currentPos.y, t: "up" });
133135
} else {
134136
currentPos.x--;
137+
poss.push({ x: currentPos.x, y: currentPos.y });
135138
}
136139
break;
137140

138141
}
139142
}
140-
return 0;
143+
return [ 0, poss.filter((pos, index) => poss.findIndex(p => p.x === pos.x && p.y === pos.y) === index) ];
141144
}
145+
const [ _, fields ] = tryWith(coords);
142146
const routines = [];
143-
for (const y in coords) {
144-
for (const x in coords[ y ]) {
145-
if (coords[ y ][ x ] === ".") {
146-
coords[ y ][ x ] = "#";
147-
routines.push(routine(tryWith, coords));
148-
coords[ y ][ x ] = ".";
149-
}
147+
for (const { x, y } of fields) {
148+
if (coords[ y ][ x ] === ".") {
149+
coords[ y ][ x ] = "#";
150+
routines.push(tryWith(coords.copyWithin(0, 0)));
151+
coords[ y ][ x ] = ".";
150152
}
151153
}
152-
return await Promise.all(routines).then(res => res.reduce((a, b) => a + b, 0));
154+
return routines.reduce((a, b) => a + b[ 0 ], 0);
153155
}
154156
}

0 commit comments

Comments
 (0)