Skip to content

Commit f26e7df

Browse files
committed
Solve day 05 challenge
1 parent ad7fc64 commit f26e7df

File tree

5 files changed

+752
-1
lines changed

5 files changed

+752
-1
lines changed

day02/answer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ export const multiplyAimDisplacements = (displacements) => {
5353
}
5454

5555
console.log("The result of multiplying the displacements is: ", multiplyDisplacements(formattedInput))
56-
console.log("The result of multiplying the displacements is: ", multiplyAimDisplacements(formattedInput))
56+
console.log("The result of multiplying the aim displacements is: ", multiplyAimDisplacements(formattedInput))

day05/answer.js

+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
import fs from "fs"
2+
3+
const readInput = fs.readFileSync(__dirname + '/input.txt', 'utf-8');
4+
5+
const formattedInput = readInput.trim().split('\n');
6+
7+
const formatEntry = (entry) => {
8+
const line = entry.split(' -> ')
9+
10+
const startingPoint = line[0].trim().split(',').map(x => Number(x))
11+
const endPoint = line[1].trim().split(',').map(y => Number(y))
12+
13+
return [startingPoint, endPoint]
14+
}
15+
16+
const hasCommonRoot = (start, end) => {
17+
if (start[0] === end[0] || start[1] === end[1]) {
18+
return true
19+
}
20+
return false
21+
}
22+
23+
const checkUncommonIndex = (start, end) => {
24+
if (start[0] !== end[0]) {
25+
return 0
26+
}
27+
return 1
28+
}
29+
30+
const findLargest = (arr) => {
31+
let largest = 0;
32+
for (let i = 0; i < arr.length; i++) {
33+
if (largest < arr[i] ) {
34+
largest = arr[i];
35+
}
36+
}
37+
return largest
38+
}
39+
40+
const getGridRange = (input) => {
41+
let xEntries = [];
42+
let yEntries = [];
43+
44+
for (let entry of input) {
45+
const [begin, stop] = formatEntry(entry)
46+
47+
xEntries.push(begin[0])
48+
yEntries.push(begin[1])
49+
xEntries.push(stop[0])
50+
yEntries.push(stop[1])
51+
}
52+
53+
const xMax = findLargest(xEntries)
54+
const yMax = findLargest(yEntries)
55+
56+
return [xMax, yMax]
57+
}
58+
59+
export const countPointsAboveTwo = (lines) => {
60+
61+
const [x, y] = getGridRange(lines)
62+
63+
const grid = Array(x + 1).fill().map(() => Array(y + 1).fill(0))
64+
65+
for (let lineValue of lines) {
66+
const [start, end] = formatEntry(lineValue)
67+
68+
if (!hasCommonRoot(start, end)) {
69+
continue;
70+
}
71+
72+
const index = checkUncommonIndex(start, end)
73+
74+
const commonIndex = index === 1 ? 0 : 1
75+
76+
let startIndex = Math.min(start[index], end[index])
77+
let endIndex = Math.max(start[index], end[index])
78+
79+
for (let i = startIndex; i <= endIndex; i++) {
80+
81+
if (commonIndex === 1) {
82+
grid[start[commonIndex]][i] += 1
83+
} else {
84+
grid[i][start[commonIndex]] += 1
85+
}
86+
87+
}
88+
}
89+
90+
const flattenGrid = grid.reduce(
91+
(previousValue, currentValue) => previousValue.concat(currentValue),
92+
[]
93+
)
94+
95+
const twoOrGreater = flattenGrid.filter( entry => Number(entry) >= 2 )
96+
97+
return twoOrGreater.length
98+
}
99+
100+
101+
export const countPointsAboveTwoDiagonal = (lines) => {
102+
103+
const [x, y] = getGridRange(lines)
104+
105+
const grid = Array(x + 1).fill().map(() => Array(y + 1).fill(0))
106+
107+
for (let lineValue of lines) {
108+
const [start, end] = formatEntry(lineValue)
109+
110+
if (hasCommonRoot(start, end)) {
111+
const index = checkUncommonIndex(start, end)
112+
113+
const commonIndex = index === 1 ? 0 : 1
114+
115+
let startIndex = Math.min(start[index], end[index])
116+
let endIndex = Math.max(start[index], end[index])
117+
118+
for (let i = startIndex; i <= endIndex; i++) {
119+
120+
if (commonIndex === 1) {
121+
grid[start[commonIndex]][i] += 1
122+
} else {
123+
grid[i][start[commonIndex]] += 1
124+
}
125+
}
126+
} else {
127+
const diff = Math.abs(start[0] - end[0])
128+
129+
for (let j = 0; j <= diff; j++) {
130+
if (start[0] < end[0] && start[1] < end[1]) {
131+
grid[start[1] + j][start[0] + j] += 1
132+
} else if (start[0] > end[0] && start[1] > end[1]) {
133+
grid[start[1] - j][start[0] - j] += 1
134+
} else if (start[0] < end[0] && start[1] > end[1]) {
135+
grid[start[1] - j][start[0] + j] += 1
136+
} else if (start[0] > end[0] && start[1] < end[1]) {
137+
grid[start[1] + j][start[0] - j] += 1
138+
}
139+
}
140+
}
141+
}
142+
143+
const flattenGrid = grid.reduce(
144+
(previousValue, currentValue) => previousValue.concat(currentValue),
145+
[]
146+
)
147+
148+
const twoOrGreater = flattenGrid.filter( entry => Number(entry) >= 2 )
149+
150+
return twoOrGreater.length
151+
}
152+
153+
console.log("The count of points greater than 1 is: ", countPointsAboveTwo(formattedInput))
154+
console.log("The count of points greater than 1 including diagonals is: ", countPointsAboveTwoDiagonal(formattedInput))

day05/challenge.txt

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
--- Day 5: Hydrothermal Venture ---
2+
3+
You come across a field of hydrothermal vents on the ocean floor! These vents constantly produce large, opaque clouds, so it would be best to avoid them if possible.
4+
5+
They tend to form in lines; the submarine helpfully produces a list of nearby lines of vents (your puzzle input) for you to review. For example:
6+
7+
0,9 -> 5,9
8+
8,0 -> 0,8
9+
9,4 -> 3,4
10+
2,2 -> 2,1
11+
7,0 -> 7,4
12+
6,4 -> 2,0
13+
0,9 -> 2,9
14+
3,4 -> 1,4
15+
0,0 -> 8,8
16+
5,5 -> 8,2
17+
Each line of vents is given as a line segment in the format x1,y1 -> x2,y2 where x1,y1 are the coordinates of one end the line segment and x2,y2 are the coordinates of the other end. These line segments include the points at both ends. In other words:
18+
19+
An entry like 1,1 -> 1,3 covers points 1,1, 1,2, and 1,3.
20+
An entry like 9,7 -> 7,7 covers points 9,7, 8,7, and 7,7.
21+
For now, only consider horizontal and vertical lines: lines where either x1 = x2 or y1 = y2.
22+
23+
So, the horizontal and vertical lines from the above list would produce the following diagram:
24+
25+
.......1..
26+
..1....1..
27+
..1....1..
28+
.......1..
29+
.112111211
30+
..........
31+
..........
32+
..........
33+
..........
34+
222111....
35+
36+
In this diagram, the top left corner is 0,0 and the bottom right corner is 9,9. Each position is shown as the number of lines which cover that point or . if no line covers that point. The top-left pair of 1s, for example, comes from 2,2 -> 2,1; the very bottom row is formed by the overlapping lines 0,9 -> 5,9 and 0,9 -> 2,9.
37+
38+
To avoid the most dangerous areas, you need to determine the number of points where at least two lines overlap. In the above example, this is anywhere in the diagram with a 2 or larger - a total of 5 points.
39+
40+
Consider only horizontal and vertical lines. At how many points do at least two lines overlap?
41+
42+
--- Part Two ---
43+
44+
Unfortunately, considering only horizontal and vertical lines doesn't give you the full picture; you need to also consider diagonal lines.
45+
46+
Because of the limits of the hydrothermal vent mapping system, the lines in your list will only ever be horizontal, vertical, or a diagonal line at exactly 45 degrees. In other words:
47+
48+
An entry like 1,1 -> 3,3 covers points 1,1, 2,2, and 3,3.
49+
An entry like 9,7 -> 7,9 covers points 9,7, 8,8, and 7,9.
50+
Considering all lines from the above example would now produce the following diagram:
51+
52+
1.1....11.
53+
.111...2..
54+
..2.1.111.
55+
...1.2.2..
56+
.112313211
57+
...1.2....
58+
..1...1...
59+
.1.....1..
60+
1.......1.
61+
222111....
62+
You still need to determine the number of points where at least two lines overlap. In the above example, this is still anywhere in the diagram with a 2 or larger - now a total of 12 points.
63+
64+
Consider all of the lines. At how many points do at least two lines overlap?

0 commit comments

Comments
 (0)