-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
63 lines (53 loc) · 1.97 KB
/
main.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
62
63
const parseData = (values: string[]) => {
const stackPositions = [1, 5, 9, 13, 17, 21, 25, 29, 33]
const stackHeight = 8
let stacks: string[][] = []
for (const stackPosition of stackPositions) {
let stack: string[] = []
for (let i = 0; i < stackHeight; i++) {
if (values[i][stackPosition] !== ' ') {
stack.push(values[i][stackPosition])
}
}
stack.reverse()
stacks.push(stack)
}
const instructions = values.filter(line => line.startsWith('move'))
.map(instruction => {
return {
numberOfBlocks: parseInt(instruction.split(' ')[1]),
fromStack: parseInt(instruction.split(' ')[3]),
toStack: parseInt(instruction.split(' ')[5])
}
});
return {
stacks,
instructions
}
}
export const partOne = (values: string[]) => {
const parsedData = parseData(values)
const stacks = parsedData.stacks
const instructions = parsedData.instructions
for (const instruction of instructions) {
for (let i = 0; i < instruction.numberOfBlocks; i++) {
let movingBlock: string = stacks[instruction.fromStack-1].pop()!
stacks[instruction.toStack-1].push(movingBlock)
}
}
return stacks.map(stack => stack[stack.length-1]).join('')
}
export const partTwo = (values: string[]) => {
const parsedData = parseData(values)
const stacks = parsedData.stacks
const instructions = parsedData.instructions
for (const instruction of instructions) {
let movingBlocks: string[] = []
for (let i = 0; i < instruction.numberOfBlocks; i++) {
let movingBlock: string = stacks[instruction.fromStack-1].pop()!
movingBlocks.push(movingBlock)
}
stacks[instruction.toStack-1] = stacks[instruction.toStack-1].concat(movingBlocks.reverse())
}
return stacks.map(stack => stack[stack.length-1]).join('')
}