-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththirteen.js
More file actions
63 lines (53 loc) · 1.33 KB
/
Copy paththirteen.js
File metadata and controls
63 lines (53 loc) · 1.33 KB
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
import { readFile } from 'fs/promises';
let POINTS = [];
const FOLDS = [];
// 13a
async function init() {
const input = await readFile('thirteen.txt', 'utf8');
const lines = input.split('\n');
let folds = false;
lines.forEach((line) => {
if (folds) {
const [a, b, c] = line.split(' ');
const [dir, pos] = c.split('=');
FOLDS.push({ dir, pos: parseInt(pos) });
} else if (line === '') {
folds = true;
} else {
const [x, y] = line.split(',');
POINTS.push({ x: parseInt(x), y: parseInt(y) });
}
});
FOLDS.forEach((fold) => {
const { dir, pos } = fold;
POINTS.forEach((point) => {
if (point[dir] > pos) {
point[dir] = 2 * pos - point[dir];
}
});
POINTS = dedupe(POINTS);
POINTS = parse(POINTS);
});
console.log(POINTS);
const display = [];
for (let y = 0; y < 6; y++) {
display[y] = [];
for (let x = 0; x < 40; x++) {
display[y][x] = '.';
}
}
POINTS.forEach(({ x, y }) => {
display[y][x] = 'X';
});
console.log(display.map((row) => row.join('')).join('\n'));
}
function dedupe(POINTS) {
return Array.from(new Set(POINTS.map(({ x, y }) => `${x},${y}`)));
}
function parse(POINTS) {
return POINTS.map((line) => {
const [x, y] = line.split(',');
return { x: parseInt(x), y: parseInt(y) };
});
}
init();