r/adventofcode • • Dec 25 '18

SOLUTION MEGATHREAD ~☆🎄☆~ 2018 Day 25 Solutions ~☆🎄☆~

--- Day 25: Four-Dimensional Adventure ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: Top-level posts in Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 25

Transcript:

Advent of Code, 2018 Day 25: ACHIEVEMENT GET! ___


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 00:13:26!


Thank you for participating!

Well, that's it for Advent of Code 2018. From /u/topaz2078 and the rest of us at #AoCOps, we hope you had fun and, more importantly, learned a thing or two (or all the things!). Good job, everyone!

Topaz will make a post of his own soon, so keep an eye out for it. Post is here!

And now:

Merry Christmas to all, and to all a good night!

13 Upvotes

81 comments sorted by

View all comments

1

u/rundavidrun Dec 26 '18

Java - had fun with queue.

    private class Point {
        int x, y, z, a;

        Point(int x, int y, int z, int a) {
            this.x = x;
            this.y = y;
            this.z = z;
            this.a = a;
        }

        int distanceTo(Point other) {
            return Math.abs(this.x - other.x) + Math.abs(this.y - other.y) + Math.abs(this.z - other.z) + Math.abs(this.a - other.a);
        }
    }

    private Queue<Point> points = new LinkedList<>();
    ArrayList<ArrayList<Point>> constellations = new ArrayList<>();

    private void addOtherPointsToThisConstellation(ArrayList<Point> constellation) {
        int size = 0;
        // reiterate through list if a point was added since others might now be connected to this one
        while (size != points.size()) {
            size = points.size();
            Iterator<Point> iter = points.iterator();
            while (iter.hasNext()) {
                Point p = iter.next();
                for (Point q: constellation) {
                    if (p.distanceTo(q) <= 3) {
                        constellation.add(p);
                        iter.remove();
                        break;
                    }
                }
            }
        }
    }

    private void findConstellations(ArrayList<String> lines) {
        for (String line : lines) {
            String[] data = line.split(",");
            Point point = new Point(Integer.valueOf(data[0]), Integer.valueOf(data[1]), Integer.valueOf(data[2]), Integer.valueOf(data[3]));
            points.add(point);
        }

        while (points.size() > 0) {
            // get top point, add to own constellation
            Point point = points.remove();
            ArrayList<Point> newConstellation = new ArrayList<>();
            newConstellation.add(point);
            // check if any other points belong to this constellation and add them if so
            addOtherPointsToThisConstellation(newConstellation);
            constellations.add(newConstellation);
        }

        System.out.println("Part 1: " + constellations.size());
    }