-
Notifications
You must be signed in to change notification settings - Fork 0
/
5.js
75 lines (64 loc) · 1.82 KB
/
5.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
function Stacks(input) {
const lines = input.split(/\r?\n/)
this.stacks = lines.pop().trim().split(/\W+/)
Object.assign(this, this.stacks.reduce((o,k) => {
o[k] = []
return o
}, {}))
for(const line of lines) {
line
.split(/(?:\[(\w)\]| ) ?/)
.filter(s => s !== '')
.forEach((val, i) => val ? this[this.stacks[i]].unshift(val) : 0)
}
}
Stacks.prototype = {
move(n, from, to) {
this[to].push(...this[from].splice(-n, n).reverse())
},
move2(n, from, to) {
this[to].push(...this[from].splice(-n, n))
},
topmost() {
return this.stacks.map(name => this[name][this[name].length-1])
}
}
function SupplyStacks1(input) {
const [drawing, rest] = input.toString().split(/\r?\n\r?\n/);
const stacks = new Stacks(drawing)
moves = rest
.split(/\r?\n/)
.map(line => /move (\d+) from (\d+) to (\d+)/.exec(line))
.map(match => [ parseInt(match[1]), match[2], match[3]])
for(const [n, from, to] of moves) {
stacks.move(n, from, to)
}
return stacks.topmost().join('')
}
function SupplyStacks2(input) {
const [drawing, rest] = input.toString().split(/\r?\n\r?\n/);
const stacks = new Stacks(drawing)
moves = rest
.split(/\r?\n/)
.map(line => /move (\d+) from (\d+) to (\d+)/.exec(line))
.map(match => [ parseInt(match[1]), match[2], match[3]])
for(const [n, from, to] of moves) {
stacks.move2(n, from, to)
}
return stacks.topmost().join('')
}
module.exports = { name: "Supply Stacks", SupplyStacks1, SupplyStacks2 }
const example = ` [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`
if(!module || require.main == module) {
const fs = require('fs')
const input = fs.readFileSync("5.input.txt", "utf8")
console.log(SupplyStacks1(input))
console.log(SupplyStacks2(input))
}