Skip to content

Commit

Permalink
Solve AoC 2018 Day 25 Part 1 & 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Gentleman1983 committed Feb 25, 2024
1 parent b5b5d54 commit 73ad92b
Show file tree
Hide file tree
Showing 18 changed files with 2,861 additions and 52 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ To run all solutions, simply run `./gradlew run`. If you want to run the solutio
| 2021 | Kotlin ![Kotlin](img/kotlin.png) | 0 ⭐ | [aoc2021](https://github.com/Gentleman1983/aoc2021) |
| 2020 | Kotlin ![Kotlin](img/kotlin.png) | 0 ⭐ | [aoc2020](https://github.com/Gentleman1983/advent-of-code/tree/main/aoc2020) |
| 2019 | JAVA ![JAVA](img/java.png) | 0 ⭐ | [aoc2019](https://github.com/Gentleman1983/advent-of-code/tree/main/aoc2019) |
| 2018 | JAVA ![JAVA](img/java.png), Kotlin ![Kotlin](img/kotlin.png) | 48| [aoc2018](https://github.com/Gentleman1983/advent-of-code/tree/main/aoc2018) |
| 2018 | JAVA ![JAVA](img/java.png), Kotlin ![Kotlin](img/kotlin.png) | 50| [aoc2018](https://github.com/Gentleman1983/advent-of-code/tree/main/aoc2018) |
| 2017 | JAVA ![JAVA](img/java.png), Scala ![Scala](img/scala.png) | 50 ⭐ | [aoc2017](https://github.com/Gentleman1983/advent-of-code/tree/main/aoc2017) |
| 2016 | JAVA ![JAVA](img/java.png), Kotlin ![Kotlin](img/kotlin.png), Groovy ![Groovy](img/groovy.png) | 50 ⭐ | [aoc2016](https://github.com/Gentleman1983/advent-of-code/tree/main/aoc2016) |
| 2015 | JAVA ![JAVA](img/java.png), Kotlin ![Kotlin](img/kotlin.png), Python ![Python](img/python.png) | 50 ⭐ | [aoc2015](https://github.com/Gentleman1983/advent-of-code/tree/main/aoc2015) |
| **SUM** | | **300** | |
| **SUM** | | **302** | |

120 changes: 120 additions & 0 deletions aoc2018/Day25.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# Day 25: Four-Dimensional Adventure
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.

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**".

"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--"

**Beep**.

A few seconds later, you hear a new voice. "Hello; please state the nature of your reindeer." You try to describe the
situation.

"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.

"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?"

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."

"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."

"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.

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.

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:
```
0,0,0,0
3,0,0,0
0,3,0,0
0,0,3,0
0,0,0,3
0,0,0,6
9,0,0,0
12,0,0,0
```
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.)

In this example, the number of constellations is `4`:
```
-1,2,2,0
0,0,2,-2
0,0,0,-2
-1,2,0,0
-2,-2,-2,2
3,0,2,-1
-1,3,2,2
-1,0,-1,0
0,2,1,-2
3,0,0,0
```
In this one, it's `3`:
```
1,-1,0,1
2,0,-1,0
3,2,-1,0
0,0,3,1
0,0,-1,-1
2,3,-2,0
-2,2,0,0
2,-2,0,-1
1,-1,0,-1
3,2,0,2
```
Finally, in this one, it's `8`:
```
1,-1,-1,-2
-2,-2,0,1
0,2,1,3
-2,3,-2,1
0,2,3,-2
-1,-1,1,-2
0,-2,-1,0
-2,2,3,-1
1,2,2,0
-1,-2,0,-2
```
The portly man nervously strokes his white beard. It's time to get that hot chocolate.

**How many constellations are formed by the fixed points in spacetime**?

# Part Two
A small glowing portal opens above the mug you prepared and just enough hot chocolate streams in to fill it. You suspect
the reindeer has never encountered hot chocolate before, but seems to enjoy it anyway. You hope it works.

It's time to start worrying about that **integer underflow in time itself** you
[set up a few days ago](https://adventofcode.com/2018/day/21). You check the status of the device: "Insufficient chronal
energy for activation. Energy required: **50 stars**."

The reindeer bumps the device with its nose.

"Energy required: **49 stars**."

You have enough stars to **[Trigger the Underflow]**.

You use all **fifty stars** to activate the underflow. As you begin to fall, the little reindeer looks up at you; its
nose begins to glow red.

You go back in time so far that you wrap around and end up back in your own time again! Oddly, history books contain
some new details that you don't recognize...
6 changes: 3 additions & 3 deletions aoc2018/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ For more information see https://adventofcode.com [2018](https://adventofcode.co
| 22 |![Kotlin](../img/kotlin.png) |![Kotlin](../img/kotlin.png) |
| 23 |![JAVA](../img/java.png) |![Kotlin](../img/kotlin.png) |
| 24 |![JAVA](../img/java.png) |![JAVA](../img/java.png) |
| 25 | ![JAVA](../img/java.png) | ![JAVA](../img/java.png) |
| **SUM** | **24** | **24** |
| 25 | ![JAVA](../img/java.png) | ![JAVA](../img/java.png) |
| **SUM** | **25** | **25** |

Total: 48
Total: 50

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package de.havox_design.aoc2018.day25;

import de.havox_design.aoc.utils.java.AoCFunctionality;

import java.util.List;

public class FourDimensionalAdventure implements AoCFunctionality {
private final List<Position4d> positions;

public FourDimensionalAdventure(String fileName) {
PositionParser parser = new PositionParser();
List<String> input = readData(fileName);

positions = input
.stream()
.map(parser::parseLine)
.toList();
}

public static long processTask1(String fileName) {
FourDimensionalAdventure instance = new FourDimensionalAdventure(fileName);
return instance.processTask1();
}

public static String processTask2(String fileName) {
FourDimensionalAdventure instance = new FourDimensionalAdventure(fileName);
return instance.processTask2();
}

public long processTask1() {
int sizeMinus1 = positions.size() - 1;
Integer[] parents = new Integer[positions.size()];
int constellations = positions.size();

for (int i = 0; i < sizeMinus1; i++) {
for (int j = i + 1; j < positions.size(); j++) {
if (positions.get(i).manhattanDistance(positions.get(j)) <= 3) {
int iParent = getParent(parents, i);
int jParent = getParent(parents, j);

if (iParent != jParent) {
parents[jParent] = iParent;
constellations--;
}
}
}
}

return constellations;
}

public String processTask2() {
return "Merry XMas!!!";
}

private int getParent(final Integer[] parents, int i) {
Integer parent = parents[i];

if (parent == null) {
return i;
}

parent = getParent(parents, parent);
parents[i] = parent;

return parent;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package de.havox_design.aoc2018.day25;

public record Position4d(int x, int y, int z, int s) {
public int manhattanDistance(Position4d other) {
return Math.abs(x - other.x) +
Math.abs(y - other.y) +
Math.abs(z - other.z) +
Math.abs(s - other.s);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package de.havox_design.aoc2018.day25;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PositionParser {
private static final String POSITION_PATTERN = "^(?<x>-?\\d+),\\s*(?<y>-?\\d+),\\s*(?<z>-?\\d+),\\s*(?<s>-?\\d+)$";

public Position4d parseLine(String line) {
Pattern pattern = Pattern.compile(POSITION_PATTERN);
Matcher matcher = pattern.matcher(line);

if (matcher.matches()) {
int x = Integer.parseInt(matcher.group("x").trim());
int y = Integer.parseInt(matcher.group("y").trim());
int z = Integer.parseInt(matcher.group("z").trim());
int s = Integer.parseInt(matcher.group("s").trim());

return new Position4d(x, y, z, s);
}
throw new IllegalArgumentException("Expected the input '" + line + "' to match pattern '" + POSITION_PATTERN + "'.");
}
}
Loading

0 comments on commit 73ad92b

Please sign in to comment.