-
Notifications
You must be signed in to change notification settings - Fork 0
/
soln_towers.js
49 lines (43 loc) · 1.63 KB
/
soln_towers.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
40
41
42
43
44
45
46
47
48
// manually transform input:
// JFCNDBW
// TSLQVZP
// TJGBZP
// CHBZJLTD
// SJBVG
// QSP
// NPMLFDVB
// RLDBFMSP
// RTDV
var stacks = input.split("\n").slice(0, 9);
stacks = stacks.map(s => s.split("").reverse().join(""));
var instructions = input.split("\n").slice(10, input.split("\n").length);
function makeMove(count, from, onto) {
// console.log(`move ${count} from ${from} to ${onto}`);
const fromIndex = from - 1; const ontoIndex = onto - 1;
const stackHeight = stacks[fromIndex].length;
const payload = stacks[fromIndex].substring(stackHeight - count, stackHeight);
stacks[fromIndex] = stacks[fromIndex].substring(0, stackHeight - count);
stacks[ontoIndex] += payload;
}
function makeMoveUpsideDown(count, from, onto) {
// console.log(`move ${count} from ${from} to ${onto}`);
const fromIndex = from - 1; const ontoIndex = onto - 1;
const stackHeight = stacks[fromIndex].length;
const payload = stacks[fromIndex].substring(stackHeight - count, stackHeight);
stacks[fromIndex] = stacks[fromIndex].substring(0, stackHeight - count);
stacks[ontoIndex] += payload.split("").reverse().join("");
}
// part 1
instructions.forEach(instruction => {
var [m, count, f, from, t, onto] = instruction.split(" ");
makeMoveUpsideDown(count, from, onto);
});
console.log(stacks.map(stack => stack.at(stack.length - 1)).join(""));
// part 2
stacks = input.split("\n").slice(0, 9);
stacks = stacks.map(s => s.split("").reverse().join(""));
instructions.forEach(instruction => {
var [m, count, f, from, t, onto] = instruction.split(" ");
makeMove(count, from, onto);
});
console.log(stacks.map(stack => stack.at(stack.length - 1)).join(""));