-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.ts
31 lines (26 loc) · 950 Bytes
/
part2.ts
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
import * as fs from 'fs';
import { parsePoint, pointToString, drawPoints } from '../../utils';
const [rawDots, rawInstructions] = fs.readFileSync('input', 'utf8').split('\n\n');
let dots = new Set(rawDots.split('\n'));
const instructions = rawInstructions.split('\n').map((line) => {
let [axis, number] = line.replace('fold along ', '').split('=');
return { axis, number: Number(number) };
});
const fold = (repeat = 1) => {
for (let i = 0; i < repeat; i++) {
const instruction = instructions[i];
const newDots = new Set<string>();
for (const dot of dots) {
let newDot = dot;
const dotPosition = parsePoint(dot);
if (dotPosition[instruction.axis] > instruction.number) {
dotPosition[instruction.axis] = instruction.number * 2 - dotPosition[instruction.axis];
newDot = pointToString(dotPosition);
}
newDots.add(newDot);
}
dots = newDots;
}
};
fold(instructions.length);
drawPoints([...dots].map(parsePoint));