-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (52 loc) · 1.97 KB
/
index.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
import { parseData } from "../helpers.js";
const data = parseData("day-5/input.txt", "\n\n");
function create2dCrateArray(rawArray) {
const numbers = rawArray.pop().split("");
const indexHolder = numbers.reduce((indices, currVal, currIdx) => {
if (currVal !== " ") {
indices.push(currIdx);
}
return indices;
}, []);
const crates = new Array(indexHolder.length);
rawArray.reverse().forEach((level, i) => {
indexHolder.forEach((locationIndex, j) => {
if (level[locationIndex] !== " ") {
if (!crates[j]) {
crates[j] = [level[locationIndex]];
} else {
crates[j][i] = level[locationIndex];
}
}
});
});
return crates;
}
function puzzle1(data) {
const crateLocations = create2dCrateArray(data[0].split("\n"));
const moves = data[1].split("\n");
for (const move of moves) {
const moveDirections = move.split(" ");
const quantity = parseInt(moveDirections[1]);
const startIndex = parseInt(moveDirections[3]) - 1;
const endIndex = parseInt(moveDirections[5]) - 1;
const movingCrates = crateLocations[startIndex].splice(-quantity).reverse();
crateLocations[endIndex] = crateLocations[endIndex].concat(movingCrates);
}
return crateLocations.reduce((str, cur) => (str += cur[cur.length - 1]), "");
}
function puzzle2(data) {
const crateLocations = create2dCrateArray(data[0].split("\n"));
const moves = data[1].split("\n");
for (const move of moves) {
const moveDirections = move.split(" ");
const quantity = parseInt(moveDirections[1]);
const startIndex = parseInt(moveDirections[3]) - 1;
const endIndex = parseInt(moveDirections[5]) - 1;
const movingCrates = crateLocations[startIndex].splice(-quantity);
crateLocations[endIndex] = crateLocations[endIndex].concat(movingCrates);
}
return crateLocations.reduce((str, cur) => (str += cur[cur.length - 1]), "");
}
console.log(`Puzzle 1: ${puzzle1(data)}`);
console.log(`Puzzle 2: ${puzzle2(data)}`);