-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5.js
64 lines (56 loc) · 1.71 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const fs = require("fs");
const assert = require("assert");
const _ = require("lodash");
const getOverlappingCountWithoutDiagonals = (vectors) => {
return getOverlappingCount(
vectors.filter(([{ x1, y1 }, { x2, y2 }]) => x1 == x2 || y1 == y2)
);
};
const getOverlappingCount = (vectors) => {
const getPoints = ([{ x1, y1 }, { x2, y2 }]) => {
const dirX = x1 == x2 ? undefined : x1 > x2 ? "-" : "+";
const dirY = y1 == y2 ? undefined : y1 > y2 ? "-" : "+";
const length =
Math.abs(x1 - x2) > Math.abs(y1 - y2)
? Math.abs(x1 - x2)
: Math.abs(y1 - y2);
return _.range(length + 1).map((i) => {
return {
x: dirX == undefined ? x1 : eval(`${x1}${dirX}${i}`),
y: dirY == undefined ? y1 : eval(`${y1}${dirY}${i}`),
};
});
};
const points = vectors.map(getPoints).flat();
return Object.values(
_.countBy(points, (point) => Object.values(point))
).filter((v) => v >= 2).length;
};
const parseInput = (input) =>
input
.split(/\n/)
.map((line) => line.split(" -> ").map((pair) => pair.split(",")))
.map((vector) =>
vector.map((point, index) => {
return {
[`x${index + 1}`]: Number(point[0]),
[`y${index + 1}`]: Number(point[1]),
};
})
);
const testInput = `0,9 -> 5,9
8,0 -> 0,8
9,4 -> 3,4
2,2 -> 2,1
7,0 -> 7,4
6,4 -> 2,0
0,9 -> 2,9
3,4 -> 1,4
0,0 -> 8,8
5,5 -> 8,2`;
assert(getOverlappingCountWithoutDiagonals(parseInput(testInput)) === 5);
assert(getOverlappingCount(parseInput(testInput)) === 12);
const input = parseInput(fs.readFileSync("day5.txt", "utf-8"));
/*
console.log(`Part 1: ${getOverlappingCountWithoutDiagonals(input)}`);
console.log(`Part 2: ${getOverlappingCount(input)}`);*/