-
Notifications
You must be signed in to change notification settings - Fork 0
/
b.ts
44 lines (37 loc) · 1.29 KB
/
b.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
function solution(input: string) {
const [stacksStr, rearrangementStr] = input.split("\n\n");
const rearrangement = rearrangementStr.split("\n");
const stacks = stacksStr.split("\n");
const nums = stacks.splice(-1, 1)[0].split("");
const stacksArr = Array.from(
{ length: Number(nums.at(-2)) },
() => [],
) as string[][];
stacks.slice().reverse().forEach((stack) => {
nums.forEach((num, i) => {
const n = Number(num);
if (n > 0 && stack[i] !== " ") {
stacksArr[n - 1].push(stack[i]);
}
});
});
rearrangement.forEach((proc) => {
const [count, from, to] = proc.match(/\d+/g)!.map((n) => Number(n));
const elem = stacksArr[from - 1].splice(-count, count);
stacksArr[to - 1].push(...elem);
});
return stacksArr.reduce((acc, stack) => acc + stack.at(-1), "");
}
Deno.test("example", () => {
const input = Deno.readTextFileSync("./05/example.txt");
const actual = solution(input);
const expected = "MCD";
assertEquals(actual, expected);
});
Deno.test("puzzle input", { ignore: false }, () => {
const input = Deno.readTextFileSync("./05/input.txt");
const actual = solution(input);
const expected = "LVMRWSSPZ";
assertEquals(actual, expected);
});