-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfive.js
More file actions
65 lines (49 loc) · 1.38 KB
/
Copy pathfive.js
File metadata and controls
65 lines (49 loc) · 1.38 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const { readFile } = require('fs/promises');
// 5
async function init() {
const input = await readFile('five.txt', 'utf8');
// const input = await readFile('five.test.txt', 'utf8');
const data = input.split('\n');
const blankI = data.findIndex((d) => !d);
const board = data.slice(0, blankI - 1).reverse();
const nBins = (data[blankI - 1].length - 2) / 3;
const moves = data.slice(blankI + 1);
const stacks = buildStacks(nBins, board);
// console.log({ stacks, moves });
moves.forEach((move) => {
let [a, num, b, from, c, to] = move.split(' ');
num = parseInt(num);
from = parseInt(from) - 1;
to = parseInt(to) - 1;
let temp = [];
for (let i = 0; i < num; i++) {
// a
// stacks[to].push(stacks[from].pop());
// b
temp.push(stacks[from].pop());
}
// b
temp.reverse().forEach((t) => stacks[to].push(t));
});
// console.log({ stacks });
const message = stacks.reduce((acc, stack) => `${acc}${stack.pop()}`, '');
console.log(message);
}
function buildStacks(n, board) {
const stacks = [];
for (let bin = 0; bin < n; bin++) {
stacks[bin] = [];
}
board.forEach((layer, y) => {
for (let bin = 0; bin < n; bin++) {
const x = 4 * bin + 1;
const letter = layer[x];
if (letter === ' ') {
continue;
}
stacks[bin].push(letter);
}
});
return stacks;
}
init();