-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday5.js
More file actions
71 lines (59 loc) · 1.67 KB
/
day5.js
File metadata and controls
71 lines (59 loc) · 1.67 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
66
67
68
69
70
71
import fs from 'fs'
const [crates, moves] = fs
.readFileSync('../inputs/day5.txt')
.toString('utf-8')
.split('\n\n')
// Split based on linebreak
const stackRows = crates.split('\n').slice(0, -1)
// Cleanup by taking every 4th char
const stackMatrix = stackRows.map((row) =>
[...row].filter((_, i) => i % 4 === 1)
)
// Empty matrix to fill
const stacks = Array(stackMatrix[0].length).fill('')
// Transposing the matrix
for (let i = 0; i < stackMatrix.length; i++) {
for (let j = 0; j < stackMatrix[0].length; j++) {
const char = stackMatrix[i][j]
if (char !== ' ') {
stacks[j] += char
}
}
}
const reverse = (str) => [...str].reverse().join('')
const moveCrate = (stacks, amount, from, to) => {
return stacks.map((stack, i) =>
i === from - 1
? stack.slice(amount)
: i === to - 1
? reverse(stacks[from - 1].slice(0, amount)) + stack
: stack
)
}
const moveCrates = (stacks, amount, from, to) => {
return stacks.map((stack, i) =>
i === from - 1
? stack.slice(amount)
: i === to - 1
? stacks[from - 1].slice(0, amount) + stack
: stack
)
}
const parseMove = (move) =>
move
.split(' ')
.filter((_, i) => i % 2 !== 0)
.map(Number)
const intructions = moves.split('\n').slice(0, -1).map(parseMove)
const finalStacks1 = intructions.reduce(
(stacks, [amount, from, to]) => moveCrate(stacks, amount, from, to),
stacks
)
const finalStacks2 = intructions.reduce(
(stacks, [amount, from, to]) => moveCrates(stacks, amount, from, to),
stacks
)
const part1 = finalStacks1.map((stack) => stack[0]).join('')
const part2 = finalStacks2.map((stack) => stack[0]).join('')
console.log(part1)
console.log(part2)