Skip to content

Commit d14921a

Browse files
authored
Merge pull request #6 from romellem/day-6
Day 6
2 parents 2cd760b + dda9596 commit d14921a

File tree

6 files changed

+428
-1
lines changed

6 files changed

+428
-1
lines changed

6/README.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Answers
2+
3+
| Part 1 | Part 2 |
4+
|--------|--------|
5+
| 4233 | 45290 |
6+
7+
## --- Day 6: Chronal Coordinates ---
8+
9+
The device on your wrist beeps several times, and once again you feel like you're falling.
10+
11+
"Situation critical," the device announces. "Destination indeterminate. Chronal interference detected. Please specify new target coordinates."
12+
13+
The device then produces a list of coordinates (your puzzle input). Are they places it thinks are safe or dangerous? It recommends you check manual page 729. The Elves did not give you a manual.
14+
15+
_If they're dangerous,_ maybe you can minimize the danger by finding the coordinate that gives the largest distance from the other points.
16+
17+
Using only the [Manhattan distance](https://en.wikipedia.org/wiki/Taxicab_geometry), determine the _area_ around each coordinate by counting the number of [integer](https://en.wikipedia.org/wiki/Integer) X,Y locations that are _closest_ to that coordinate (and aren't _tied in distance_ to any other coordinate).
18+
19+
Your goal is to find the size of the _largest area_ that isn't infinite. For example, consider the following list of coordinates:
20+
21+
1, 1
22+
1, 6
23+
8, 3
24+
3, 4
25+
5, 5
26+
8, 9
27+
28+
29+
If we name these coordinates `A` through `F`, we can draw them on a grid, putting `0,0` at the top left:
30+
31+
..........
32+
.A........
33+
..........
34+
........C.
35+
...D......
36+
.....E....
37+
.B........
38+
..........
39+
..........
40+
........F.
41+
42+
43+
This view is partial - the actual grid extends infinitely in all directions. Using the Manhattan distance, each location's closest coordinate can be determined, shown here in lowercase:
44+
45+
aaaaa.cccc
46+
aAaaa.cccc
47+
aaaddecccc
48+
aadddeccCc
49+
..dDdeeccc
50+
bb.deEeecc
51+
bBb.eeee..
52+
bbb.eeefff
53+
bbb.eeffff
54+
bbb.ffffFf
55+
56+
57+
Locations shown as `.` are equally far from two or more coordinates, and so they don't count as being closest to any.
58+
59+
In this example, the areas of coordinates A, B, C, and F are infinite - while not shown here, their areas extend forever outside the visible grid. However, the areas of coordinates D and E are finite: D is closest to 9 locations, and E is closest to 17 (both including the coordinate's location itself). Therefore, in this example, the size of the largest area is _17_.
60+
61+
_What is the size of the largest area_ that isn't infinite?
62+
63+
-----------------
64+
65+
## --- Part Two ---
66+
67+
On the other hand, _if the coordinates are safe_, maybe the best you can do is try to find a _region_ near as many coordinates as possible.
68+
69+
For example, suppose you want the sum of the [Manhattan distance](https://en.wikipedia.org/wiki/Taxicab_geometry) to all of the coordinates to be _less than 32_. For each location, add up the distances to all of the given coordinates; if the total of those distances is less than 32, that location is within the desired region. Using the same coordinates as above, the resulting region looks like this:
70+
71+
..........
72+
.A........
73+
..........
74+
...###..C.
75+
..#D###...
76+
..###E#...
77+
.B.###....
78+
..........
79+
..........
80+
........F.
81+
82+
83+
In particular, consider the highlighted location `4,3` located at the top middle of the region. Its calculation is as follows, where `abs()` is the [absolute value](https://en.wikipedia.org/wiki/Absolute_value) function:
84+
85+
* Distance to coordinate A: `abs(4-1) + abs(3-1) = 5`
86+
* Distance to coordinate B: `abs(4-1) + abs(3-6) = 6`
87+
* Distance to coordinate C: `abs(4-8) + abs(3-3) = 4`
88+
* Distance to coordinate D: `abs(4-3) + abs(3-4) = 2`
89+
* Distance to coordinate E: `abs(4-5) + abs(3-5) = 3`
90+
* Distance to coordinate F: `abs(4-8) + abs(3-9) = 10`
91+
* Total distance: `5 + 6 + 4 + 2 + 3 + 10 = 30`
92+
93+
Because the total distance to all coordinates (`30`) is less than 32, the location is _within_ the region.
94+
95+
This region, which also includes coordinates D and E, has a total size of _16_.
96+
97+
Your actual region will need to be much larger than this example, though, instead including all locations with a total distance of less than _10000_.
98+
99+
_What is the size of the region containing all locations which have a total distance to all given coordinates of less than 10000?_

6/input.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
181, 47
2+
337, 53
3+
331, 40
4+
137, 57
5+
200, 96
6+
351, 180
7+
157, 332
8+
113, 101
9+
285, 55
10+
189, 188
11+
174, 254
12+
339, 81
13+
143, 61
14+
131, 155
15+
239, 334
16+
357, 291
17+
290, 89
18+
164, 149
19+
248, 73
20+
311, 190
21+
54, 217
22+
285, 268
23+
354, 113
24+
318, 191
25+
182, 230
26+
156, 252
27+
114, 232
28+
159, 299
29+
324, 280
30+
152, 155
31+
295, 293
32+
194, 214
33+
252, 345
34+
233, 172
35+
272, 311
36+
230, 82
37+
62, 160
38+
275, 96
39+
335, 215
40+
185, 347
41+
134, 272
42+
58, 113
43+
112, 155
44+
220, 83
45+
153, 244
46+
279, 149
47+
302, 167
48+
185, 158
49+
72, 91
50+
264, 67

6/part-one.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const distance = require('manhattan');
4+
const flat = require('array.prototype.flat');
5+
6+
let raw_input = fs.readFileSync(path.resolve(__dirname, './input.txt'), 'utf8');
7+
8+
// Last filter is to remove any empty lines
9+
let input = raw_input.split('\n').filter(n => n);
10+
11+
let coordinates = input.map(p => p.split(',').map(n => +n));
12+
13+
let flattened = flat(coordinates);
14+
let largest = Math.max.apply(null, flattened);
15+
16+
let grid = Array(largest + 2)
17+
.fill()
18+
.map(n => {
19+
return Array(largest + 2).fill(-1);
20+
});
21+
22+
// Loop through grid and mark spots that are coordinates
23+
coordinates.forEach((coord, index) => {
24+
let [i, j] = coord;
25+
grid[i][j] = 'C' + index;
26+
});
27+
28+
// Iterate each coord in grid, and measure its distance from each point in our list.
29+
// Keep track of the smallest one. If tie, mark as null
30+
for (let i = 0; i < grid.length; i++) {
31+
let row = grid[i];
32+
for (let j = 0; j < grid.length; j++) {
33+
let cell = row[j];
34+
if (typeof cell !== 'string') {
35+
// Not a coord, measure it
36+
let lengths = coordinates.map((coord, index) => {
37+
let d = distance(coord, [i, j]);
38+
return {
39+
index: index,
40+
dist: d,
41+
};
42+
});
43+
44+
lengths.sort((a, b) => {
45+
if (a.dist < b.dist) return -1;
46+
else if (a.dist > b.dist) return 1;
47+
else return 0;
48+
});
49+
50+
let first = lengths[0];
51+
let second = lengths[1];
52+
53+
if (first.dist === second.dist) {
54+
// tied distance, mark as null
55+
grid[i][j] = null;
56+
} else {
57+
grid[i][j] = first.index;
58+
}
59+
}
60+
}
61+
}
62+
63+
// Now, get all edges
64+
let edges = [];
65+
for (let i = 0; i < grid.length; i++) {
66+
for (let dir = 0; dir < 4; dir++) {
67+
if (dir === 0) {
68+
// top row
69+
edges.push(grid[0][i]);
70+
} else if (dir === 1) {
71+
// right col
72+
edges.push(grid[i][grid.length - 1]);
73+
} else if (dir === 2) {
74+
// bottom row col
75+
edges.push(grid[grid.length - 1][i]);
76+
} else {
77+
// left col
78+
edges.push(grid[i][0]);
79+
}
80+
}
81+
}
82+
83+
let unique_edges_list = [...new Set(edges)];
84+
unique_edges_list = unique_edges_list.filter(n => typeof n === 'number');
85+
let unique_edges = {};
86+
unique_edges_list.forEach(e => (unique_edges[e] = true));
87+
88+
let total_area = {};
89+
90+
// Loop through grid and remove all edge nodes (they are infinite area)
91+
for (let i = 0; i < grid.length; i++) {
92+
for (let j = 0; j < grid.length; j++) {
93+
let cell = grid[i][j];
94+
if (typeof cell === 'number') {
95+
if (unique_edges[cell]) {
96+
// Remove any index with infinite area
97+
grid[i][j] = null;
98+
} else {
99+
if (!total_area[cell]) {
100+
total_area[cell] = 0;
101+
}
102+
103+
total_area[cell] += 1;
104+
}
105+
}
106+
}
107+
}
108+
109+
let areas = Object.entries(total_area);
110+
areas.sort((a, b) => {
111+
if (a[1] < b[1]) return -1;
112+
else if (a[1] > b[1]) return 1;
113+
else return 0;
114+
});
115+
116+
let max = areas.pop();
117+
118+
// Plus 1 because we need to count the original coordinate!!
119+
console.log(max[1] + 1);

6/part-two.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const distance = require('manhattan');
4+
const flat = require('array.prototype.flat');
5+
6+
let raw_input = fs.readFileSync(path.resolve(__dirname, './input.txt'), 'utf8');
7+
8+
// Last filter is to remove any empty lines
9+
let input = raw_input.split('\n').filter(n => n);
10+
11+
let coordinates = input.map(p => p.split(',').map(n => +n));
12+
13+
let flattened = flat(coordinates);
14+
let largest = Math.max.apply(null, flattened);
15+
16+
let grid = Array(largest + 2)
17+
.fill()
18+
.map(n => {
19+
return Array(largest + 2).fill(-1);
20+
});
21+
22+
// Loop through grid and mark spots that are coordinates
23+
coordinates.forEach((coord, index) => {
24+
let [i, j] = coord;
25+
grid[i][j] = 'C' + index;
26+
});
27+
28+
const MIN_DIST = 10000;
29+
30+
let min_dist_count = 0;
31+
for (let i = 0; i < grid.length; i++) {
32+
let row = grid[i];
33+
for (let j = 0; j < grid.length; j++) {
34+
let cell = row[j];
35+
if (typeof cell !== 'string') {
36+
// Not a coord, measure it
37+
let lengths = coordinates.map((coord, index) => {
38+
return distance(coord, [i, j]);
39+
});
40+
41+
let sum = lengths.reduce((a, b) => a + b);
42+
if (sum < MIN_DIST) {
43+
grid[i][j] = 'close';
44+
min_dist_count++;
45+
}
46+
}
47+
}
48+
}
49+
50+
// Loop through coords, and find ones that are surrounded by "found" areas
51+
coordinates.forEach(coord => {
52+
let [i, j] = coord;
53+
let t = grid[i - 1][j];
54+
let r = grid[i][j + 1];
55+
let b = grid[i + 1][j];
56+
let l = grid[i][j - 1];
57+
58+
const C = 'close';
59+
60+
if (t === C && r === C && b === C && l === C) {
61+
min_dist_count++;
62+
}
63+
});
64+
65+
console.log(min_dist_count);

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,9 @@
1212
},
1313
"keywords": [],
1414
"author": "",
15-
"license": "MIT"
15+
"license": "MIT",
16+
"dependencies": {
17+
"array.prototype.flat": "^1.2.1",
18+
"manhattan": "^1.0.0"
19+
}
1620
}

0 commit comments

Comments
 (0)