-
Notifications
You must be signed in to change notification settings - Fork 0
/
day05_1.js
39 lines (33 loc) · 1.32 KB
/
day05_1.js
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
console.log("---");
import { readLines } from "https://deno.land/[email protected]/io/bufio.ts";
import { range } from "https://deno.land/x/[email protected]/range.mjs";
import { slidingWindows } from "https://deno.land/[email protected]/collections/mod.ts";
import count from "https://deno.land/x/[email protected]/src/collection/count.ts";
const all = (await Deno.readTextFile("./resources/input05.txt"))
.trimEnd()
.split("\n\n");
const crates = all[0].split("\n");
const cratesNum = Math.floor((crates[crates.length - 1].length + 2) / 4);
console.log({ crates });
const stacks = [];
// console.log({ floor });
for (let col = 0; col < cratesNum; col++) {
stacks[col] = [];
for (let floor = crates.length - 2; floor >= 0; floor--) {
// console.log({ floor, col });
const val = (crates[floor][col * 4 + 1] || "").trim();
if (val) stacks[col].push(val);
}
}
console.log({ stacks });
const moves = all[1].split("\n").map((s) => s.split(" "));
for (let [_, scnt, _2, sfrom, _3, sto] of moves) {
const [cnt, from, to] = [parseInt(scnt), parseInt(sfrom), parseInt(sto)];
console.log({ cnt, from, to });
for (let i = 0; i < cnt; i++) {
console.log({ from, to }, stacks[from - 1], stacks[to - 1]);
stacks[to - 1].push(stacks[from - 1].pop());
}
}
console.log({ stacks });
console.log(stacks.map((s) => s[s.length - 1]).join(""));