-
Notifications
You must be signed in to change notification settings - Fork 0
/
5.ts
61 lines (43 loc) · 1.39 KB
/
5.ts
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
import { readFile } from 'fs/promises'
const input = (await readFile('input.txt')).toString()
const lines = input.trim().split('\n')
const LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
let stackLineStart: number = -1
for (const [index, line] of lines.entries()) {
if (line.startsWith(' 1')) {
stackLineStart = index - 1
}
}
const stacks: string[][] = []
for (let i = stackLineStart; i >= 0; i--) {
const line = lines[i]
let currentIndex = -1
let numSpaces = 0
for (const char of line) {
if (numSpaces === 3) {
numSpaces = 0
currentIndex += 1
continue
}
if (char === ' ') numSpaces += 1
if (LETTERS.includes(char)) {
numSpaces = 0
currentIndex += 1
if (Array.isArray(stacks[currentIndex])) stacks[currentIndex].push(char)
else stacks[currentIndex] = [char]
}
}
}
// now that we have the stacks, let's move
// fmt: move x from y to z
const instructions = lines.slice(stackLineStart + 3)
for (const instruction of instructions) {
const MOVE_REGEX = /move (\d+) from (\d+) to (\d+)/g
const [, amount, fromIndex, toIndex] = MOVE_REGEX.exec(instruction)!.map(
(s) => parseInt(s)
)
const slice = stacks[fromIndex - 1].splice(-amount)
// part 1: slice.reverse(), part 2: just slice :)
stacks[toIndex - 1] = [...stacks[toIndex - 1], ...slice.reverse()]
}
console.log(stacks.reduce((s, cur) => s + cur[cur.length - 1], ''))