Skip to content

Commit 676251f

Browse files
committed
feat: Day 25: Four-Dimensional Adventure (part 1)
1 parent 5181c5e commit 676251f

File tree

5 files changed

+200
-1
lines changed

5 files changed

+200
-1
lines changed

.github/badges/completion.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"schemaVersion": 1,
33
"label": "completion",
4-
"message": "34/50",
4+
"message": "35/50",
55
"color": "yellow"
66
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Collect stars by solving puzzles. Two puzzles will be made available on each day
3131
- [Day 15: Beverage Bandits](day-15-beverage-bandits/)
3232
- [Day 16: Chronal Classification](day-16-chronal-classification/)
3333
- [Day 18: Settlers of The North Pole](day-18-settlers-of-the-north-pole/)
34+
- [Day 25: Four-Dimensional Adventure](day-25-four-dimensional-adventure/)
3435

3536
## Running Tests
3637

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Day 25: Four-Dimensional Adventure
2+
3+
The reindeer's symptoms are getting worse, and neither you nor the white-bearded man have a solution. At least the reindeer has a warm place to rest: a small bed near where you're sitting.
4+
5+
As you reach down, the reindeer looks up at you, accidentally bumping a button on your wrist-mounted device with its nose in the process - a button labeled **"help"**.
6+
7+
"Hello, and welcome to the Time Travel Support Hotline! If you are lost in time and space, press 1. If you are trapped in a time paradox, press 2. If you need help caring for a sick reindeer, press 3. If you--"
8+
9+
**Beep.**
10+
11+
A few seconds later, you hear a new voice. "Hello; please state the nature of your reindeer." You try to describe the situation.
12+
13+
"Just a moment, I think I can remotely run a diagnostic scan." A beam of light projects from the device and sweeps over the reindeer a few times.
14+
15+
"Okay, it looks like your reindeer is very low on magical energy; it should fully recover if we can fix that. Let me check your timeline for a source.... Got one. There's actually a powerful source of magical energy about 1000 years forward from you, and at roughly your position, too! It looks like... hot chocolate? Anyway, you should be able to travel there to pick some up; just don't forget a mug! Is there anything else I can help you with today?"
16+
17+
You explain that your device isn't capable of going forward in time. "I... see. That's tricky. Well, according to this information, your device should have the necessary hardware to open a small portal and send some hot chocolate back to you. You'll need a list of **fixed points in spacetime**; I'm transmitting it to you now."
18+
19+
"You just need to align your device to the constellations of fixed points so that it can lock on to the destination and open the portal. Let me look up how much hot chocolate that breed of reindeer needs."
20+
21+
"It says here that your particular reindeer is-- this can't be right, it says there's only one like that in the universe! But THAT means that you're--" You disconnect the call.
22+
23+
The list of fixed points in spacetime (your puzzle input) is a set of four-dimensional coordinates. To align your device, acquire the hot chocolate, and save the reindeer, you just need to find the **number of constellations** of points in the list.
24+
25+
Two points are in the same **constellation** if their manhattan distance apart is **no more than 3** or if they can form a chain of points, each a manhattan distance no more than 3 from the last, between the two of them. (That is, if a point is close enough to a constellation, it "joins" that constellation.) For example:
26+
27+
```
28+
0,0,0,0
29+
3,0,0,0
30+
0,3,0,0
31+
0,0,3,0
32+
0,0,0,3
33+
0,0,0,6
34+
9,0,0,0
35+
12,0,0,0
36+
```
37+
38+
In the above list, the first six points form a single constellation: `0,0,0,0` is exactly distance `3` from the next four, and the point at `0,0,0,6` is connected to the others by being `3` away from `0,0,0,3`, which is already in the constellation. The bottom two points, `9,0,0,0` and `12,0,0,0` are in a separate constellation because no point is close enough to connect them to the first constellation. So, in the above list, the number of constellations is **`2`**. (If a point at `6,0,0,0` were present, it would connect `3,0,0,0` and `9,0,0,0`, merging all of the points into a single giant constellation instead.)
39+
40+
In this example, the number of constellations is `4`:
41+
42+
```
43+
-1,2,2,0
44+
0,0,2,-2
45+
0,0,0,-2
46+
-1,2,0,0
47+
-2,-2,-2,2
48+
3,0,2,-1
49+
-1,3,2,2
50+
-1,0,-1,0
51+
0,2,1,-2
52+
3,0,0,0
53+
```
54+
55+
In this one, it's `3`:
56+
57+
```
58+
1,-1,0,1
59+
2,0,-1,0
60+
3,2,-1,0
61+
0,0,3,1
62+
0,0,-1,-1
63+
2,3,-2,0
64+
-2,2,0,0
65+
2,-2,0,-1
66+
1,-1,0,-1
67+
3,2,0,2
68+
```
69+
70+
Finally, in this one, it's `8`:
71+
72+
```
73+
1,-1,-1,-2
74+
-2,-2,0,1
75+
0,2,1,3
76+
-2,3,-2,1
77+
0,2,3,-2
78+
-1,-1,1,-2
79+
0,-2,-1,0
80+
-2,2,3,-1
81+
1,2,2,0
82+
-1,-2,0,-2
83+
```
84+
85+
The portly man nervously strokes his white beard. It's time to get that hot chocolate.
86+
87+
**How many constellations are formed by the fixed points in spacetime?**
88+
89+
## References
90+
- https://adventofcode.com/2018/day/25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module.exports = (input) => {
2+
const points = input
3+
.split('\n')
4+
.map((line) => line.split(',').map((number) => parseInt(number)));
5+
6+
const visited = new Set();
7+
8+
function distance(v0, v1) {
9+
return v0.reduce((sum, _, i) => sum + Math.abs(v0[i] - v1[i]), 0);
10+
}
11+
12+
function search(vector) {
13+
let members = [vector];
14+
15+
for (let i = 0; i < points.length; i++) {
16+
if (distance(vector, points[i]) <= 3 && !visited.has(`${points[i].join(',')}`)) {
17+
visited.add(`${points[i].join(',')}`);
18+
19+
members = members.concat(search(points[i]));
20+
}
21+
}
22+
23+
return members;
24+
}
25+
26+
const constellations = [];
27+
28+
for (let i = 0; i < points.length; i++) {
29+
const vector = points[i];
30+
31+
if (!visited.has(`${vector.join(',')}`)) {
32+
visited.add(`${vector.join(',')}`);
33+
34+
const constellation = search(vector);
35+
36+
constellations.push(constellation);
37+
}
38+
}
39+
40+
return constellations.length;
41+
};
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const assert = require('assert');
2+
3+
const constellations = require('./constellations');
4+
5+
describe('Day 25: Four-Dimensional Adventure', () => {
6+
it('should determine number of constellations (example #1)', () => {
7+
const points =
8+
`0,0,0,0
9+
3,0,0,0
10+
0,3,0,0
11+
0,0,3,0
12+
0,0,0,3
13+
0,0,0,6
14+
9,0,0,0
15+
12,0,0,0`;
16+
17+
assert.strictEqual(constellations(points), 2);
18+
});
19+
20+
it('should determine number of constellations (example #2)', () => {
21+
const points =
22+
`-1,2,2,0
23+
0,0,2,-2
24+
0,0,0,-2
25+
-1,2,0,0
26+
-2,-2,-2,2
27+
3,0,2,-1
28+
-1,3,2,2
29+
-1,0,-1,0
30+
0,2,1,-2
31+
3,0,0,0`;
32+
33+
assert.strictEqual(constellations(points), 4);
34+
});
35+
36+
it('should determine number of constellations (example #3)', () => {
37+
const points =
38+
`1,-1,0,1
39+
2,0,-1,0
40+
3,2,-1,0
41+
0,0,3,1
42+
0,0,-1,-1
43+
2,3,-2,0
44+
-2,2,0,0
45+
2,-2,0,-1
46+
1,-1,0,-1
47+
3,2,0,2`;
48+
49+
assert.strictEqual(constellations(points), 3);
50+
});
51+
52+
it('should determine number of constellations (example #4)', () => {
53+
const points =
54+
`1,-1,-1,-2
55+
-2,-2,0,1
56+
0,2,1,3
57+
-2,3,-2,1
58+
0,2,3,-2
59+
-1,-1,1,-2
60+
0,-2,-1,0
61+
-2,2,3,-1
62+
1,2,2,0
63+
-1,-2,0,-2`;
64+
65+
assert.strictEqual(constellations(points), 8);
66+
});
67+
});

0 commit comments

Comments
 (0)