-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5.js
81 lines (66 loc) · 1.85 KB
/
day5.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import fs from "fs";
import assert from "assert";
import * as R from "ramda";
const parseInput = (input) => {
const lines = input.split("\n");
const start = R.transpose(
lines
.slice(0, lines.findIndex((line) => line.indexOf("move") !== -1) - 1)
.map((line) => line.match(/.{1,4}/g))
.map((line) => line.map((col) => col.match(/\w|\d/g) ?? null).flat())
);
const instructions = lines.slice(
lines.findIndex((line) => line.indexOf("move") !== -1)
);
const stacks = Object.fromEntries(
start.map((entry) => [
entry.slice(-1),
entry
.filter((elem) => elem)
.slice(0, -1)
.reverse(),
])
);
return { stacks, instructions };
};
const part1 = (input) => {
const { stacks, instructions } = parseInput(input);
instructions.forEach((instruction) => {
const [amount, from, to] = instruction
.split(" ")
.filter((elem) => !isNaN(elem));
for (let i = 0; i < Number(amount); i++) {
stacks[to].push(stacks[from].pop());
}
});
return Object.values(stacks)
.map((stack) => stack.pop())
.join("");
};
const part2 = (input) => {
const { stacks, instructions } = parseInput(input);
instructions.forEach((instruction) => {
const [amount, from, to] = instruction
.split(" ")
.filter((elem) => !isNaN(elem));
stacks[to] = stacks[to].concat(
stacks[from].splice(-Number(amount), Number(amount))
);
});
return Object.values(stacks)
.map((stack) => stack.pop())
.join("");
};
const testInput = ` [D]
[N] [C]
[Z] [M] [P]
1 2 3
move 1 from 2 to 1
move 3 from 1 to 3
move 2 from 2 to 1
move 1 from 1 to 2`;
assert(part1(testInput) === "CMZ");
assert(part2(testInput) === "MCD");
const input = fs.readFileSync("inputs/day5.txt", "utf-8");
console.log(`Part 1: ${part1(input)}`);
console.log(`Part 2: ${part2(input)}`);