-
Notifications
You must be signed in to change notification settings - Fork 0
/
day5.js
50 lines (46 loc) · 1.68 KB
/
day5.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
import { readFile } from './helpers.js';
const [top, bottom] = readFile(5).split('\n\n');
const state = top.split('\n').slice(0, -1);
const instructions = bottom.split('\n');
const length = top.split('\n').slice(-1)[0].split(' ').at(-1);
const go = (transform) => {
// { 1: [], 2: [], 3: [] }
const stack = Array.from({ length }).reduce(
(acc, _, i) => ({ ...acc, [i + 1]: [] }),
{}
);
// Build the starting stack
state.forEach((items, i) => {
Array.from({ length }).forEach((_, index) => {
// cut every 3rd set
const start = index * 3 + index;
// [N] [F] [Z] -> [N] -> N
const value = items
.slice(start, start + 3)
.trim()
.replace(/\[|\]/g, '');
// 1 indexed to make the instructions easier to read in later
value && stack[index + 1].unshift(value);
});
});
instructions.forEach((instruction) => {
if (!instruction) return;
// [move, 5, from, 2, to, 6] and _ values are discarded
const [_, count, __, from, ___, to] = instruction.split(' ');
// Get what is moving
const moving = Array.from({ length: count }).map((i) =>
stack[from].pop()
);
// In part 2 we need to reverse the order of the moving items
// so i updated this to accept a transform cb function
stack[to].push(...transform(moving));
});
// format for the expected aoc input
return Object.values(stack)
.map((s) => s.at(-1))
.join('');
};
const part1 = go((data) => data);
console.log(part1);
const part2 = go((data) => data.reverse());
console.log(part2);