-
Notifications
You must be signed in to change notification settings - Fork 1
/
day_5.ts
64 lines (55 loc) · 2.14 KB
/
day_5.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
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
import { loadInput, tally } from './helpers';
interface Coord { x: number; y: number; }
interface Line { start: Coord; end: Coord; }
const INPUT_PATTERN = /(\d+),(\d+) -> (\d+),(\d+)/;
function parseLine(line: string): Line {
const [_, x1, y1, x2, y2] = [...line.match(INPUT_PATTERN)];
return {
start: {x: parseInt(x1), y: parseInt(y1)},
end: {x: parseInt(x2), y: parseInt(y2)}
};
}
function coordsFromLine({start, end}: Line): Coord[] {
const coords: Coord[] = [];
let [miny, maxy] = [Math.min(start.y, end.y), Math.max(start.y, end.y)];
let [minx, maxx] = [Math.min(start.x, end.x), Math.max(start.x, end.x)];
if (start.x === end.x) {
// vertical
for (let y = miny; y <= maxy; y++) {
coords.push({x: start.x, y});
}
} else if (start.y === end.y) {
// horizontal
for (let x = minx; x <= maxx; x++) {
coords.push({x, y: start.y});
}
} else if (start.y - end.y + start.x - end.x == 0) {
// up and to the left
const rightAngleDistance = maxx - minx;
for (let n = 0; n <= rightAngleDistance; n++) {
coords.push({x: minx + n, y: maxy - n});
}
} else {
// down and to the left
const rightAngleDistance = maxx - minx;
for (let n = 0; n <= rightAngleDistance; n++) {
coords.push({x: minx + n, y: miny + n});
}
}
return coords;
}
// Need this because JS doesn't let you use objects as Map keys. outstanding.
function stringifyCoord({x, y}: Coord): string {
return `${x},${y}`;
}
function horizontalOrVertical({start: {x: sx, y: sy}, end: {x: ex, y: ey}}: Line): boolean {
return sx == ex || sy == ey;
}
function countOverlapCoords(lines: Line[]): number {
const coords = lines.map(coordsFromLine).reduce((a, b) => a.concat(b), []);
const counts = tally(coords.map(stringifyCoord));
return [...counts.values()].filter((count) => count > 1).length;
}
const lines = loadInput('day_5.input').split("\n").map(parseLine);
console.log("Part 1", countOverlapCoords(lines.filter(horizontalOrVertical)));
console.log("Part 2", countOverlapCoords(lines));