-
Notifications
You must be signed in to change notification settings - Fork 0
/
day13.js
71 lines (55 loc) · 1.87 KB
/
day13.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
const { test, data } = require('./data/day13');
const foldRegex = /^fold along (x|y)=(\d+)$/;
const parseData = (input) => {
return input.reduce((agg, curr) => {
const fold = curr.match(foldRegex);
if (fold) {
const [, axis, line] = fold;
return {
...agg,
folds: [...agg.folds, { [axis]: Number(line) }],
};
}
return {
...agg,
points: [...agg.points, curr.split(',').map(Number)],
};
}, { points: [], folds: [] });
};
const executeFold = (points, fold) => {
const newPoints = points.map(([x, y]) => {
if (Object.keys(fold).includes('x')) {
if (x > fold.x) {
// a - (b - a) === 2 * a - b
return [2 * fold.x - x, y]
}
}
if (y > fold.y) {
return [x, 2 * fold.y - y];
}
return [x, y];
});
return newPoints.filter(([x, y], idx, arr) => arr.findIndex(([xCandidate, yCandidate]) => `${x},${y}` === `${xCandidate},${yCandidate}`) === idx);
};
const parsedTest = parseData(test);
const parsedData = parseData(data);
console.log({ test: executeFold(parsedTest.points, parsedTest.folds[0]).length });
console.log({ data: executeFold(parsedData.points, parsedData.folds[0]).length });
// pt 2
const testPoints = parsedTest.folds.reduce(executeFold, parsedTest.points);
const points = parsedData.folds.reduce(executeFold, parsedData.points);
const drawPoints = (arrayOfPoints) => {
const xs = arrayOfPoints.map(([x]) => x);
const ys = arrayOfPoints.map(([, y]) => y);
const maxX = Math.max(...xs);
const maxY = Math.max(...ys);
const display = Array.from({ length: maxY + 1 }, () => Array.from({ length: maxX + 1 }, () => ' '));
arrayOfPoints.forEach(([x, y]) => {
display[y][x] = 'X';
});
console.log(display.map(row => row.join('')).join('\n'));
};
console.log('\npt 2\n\ntest');
drawPoints(testPoints);
console.log('--------\ndata');
drawPoints(points);