-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 5.js
53 lines (44 loc) · 1.39 KB
/
Day 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
const fs = require('fs');
const path = require('path');
const data = fs.readFileSync(path.resolve(__dirname, 'data.txt'), 'utf8');
//final output
let tempString = '';
// const crates = [
// ['do not use'],
// ['Z', 'N'],
// ['M', 'C', 'D'],
// ['P']
// ]
//starting crate data
const crates = [
['do not use'],
['S', 'M', 'R', 'N', 'W', 'J', 'V', 'T'],
['B', 'W', 'D', 'J', 'Q', 'P', 'C', 'V'],
['B', 'J', 'F', 'H', 'D', 'R', 'P'],
['F', 'R', 'P', 'B', 'M', 'N', 'D'],
['H', 'V', 'R', 'P', 'T', 'B'],
['C', 'B', 'P', 'T'],
['B', 'J', 'R', 'P', 'L'],
['N', 'C', 'S', 'L', 'T', 'Z', 'B', 'W'],
['L', 'S', 'G']
];
//parse move list into new array containing only nums
const moveList = data.split(/(?:\s)\s/g)
.map(m => m.split(' '))
.map(([,num,, first,, second]) => [num, first, second]
.map(i => parseInt(i)));
//function to update crate storage. accepts arguemts for the amount of items to move, crate to move from (crate 1), and a crate to move to (crate2)
updateCrates = (amount, crate1, crate2) => {
crates[crate2].push(...crates[crate1].splice(-amount, amount));
}
//update crates based on provided move list
moveList.forEach(move => {
updateCrates(move[0], move[1], move[2])
});
crates.forEach((crate, index) => {
if(index !== 0) {
tempString += crate[crate.length -1 ];
}
});
//log final result
console.log(tempString);