forked from tpatel/advent-of-code-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day05.js
98 lines (87 loc) · 2.67 KB
/
day05.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const fs = require("fs");
const segments = fs
.readFileSync("day05.txt", { encoding: "utf-8" }) // read day??.txt content
.replace(/\r/g, "") // remove all \r characters to avoid issues on Windows
.split("\n") // Split on newline
.filter(Boolean) // Remove empty lines
.map((line) => {
const [from, to] = line.split(" -> ").map((point) => {
const [x, y] = point.split(",").map(Number);
return { x, y };
});
return {
from,
to,
};
});
function part1() {
const filteredSegments = segments.filter(
(s) => s.from.x === s.to.x || s.from.y === s.to.y
);
let count = 0;
const memory = new Map();
function addPoint(key) {
let content = memory.get(key);
if (!content) {
content = 0;
}
content++;
if (content === 2) {
count++;
}
memory.set(key, content);
}
for (const segment of filteredSegments) {
// go from start to end
const isHorizontal = segment.from.y === segment.to.y;
let currentPoint = { x: segment.from.x, y: segment.from.y }; // {...segment.from}
// for each point in the segment, add it to memory
while (currentPoint.x !== segment.to.x || currentPoint.y !== segment.to.y) {
addPoint([currentPoint.x, currentPoint.y].join(`,`));
if (isHorizontal) {
currentPoint.x += currentPoint.x < segment.to.x ? 1 : -1;
} else {
currentPoint.y += currentPoint.y < segment.to.y ? 1 : -1;
}
}
addPoint([currentPoint.x, currentPoint.y].join(`,`));
}
console.log(count);
}
part1();
function part2() {
let count = 0;
const memory = new Map();
function addPoint(key) {
let content = memory.get(key);
if (!content) {
content = 0;
}
content++;
if (content === 2) {
count++;
}
memory.set(key, content);
}
for (const segment of segments) {
// go from start to end
const isHorizontal = segment.from.y === segment.to.y;
const isVertical = segment.from.x === segment.to.x;
let currentPoint = { x: segment.from.x, y: segment.from.y }; // {...segment.from}
// for each point in the segment, add it to memory
while (currentPoint.x !== segment.to.x || currentPoint.y !== segment.to.y) {
addPoint([currentPoint.x, currentPoint.y].join(`,`));
if (isHorizontal) {
currentPoint.x += currentPoint.x < segment.to.x ? 1 : -1;
} else if (isVertical) {
currentPoint.y += currentPoint.y < segment.to.y ? 1 : -1;
} else {
currentPoint.x += currentPoint.x < segment.to.x ? 1 : -1;
currentPoint.y += currentPoint.y < segment.to.y ? 1 : -1;
}
}
addPoint([currentPoint.x, currentPoint.y].join(`,`));
}
console.log(count);
}
part2();