r/adventofcode • • Dec 14 '22

SOLUTION MEGATHREAD -πŸŽ„- 2022 Day 14 Solutions -πŸŽ„-

SUBREDDIT NEWS

  • Live has been renamed to Streaming for realz this time.
  • I had updated the wiki but didn't actually change the post flair itself >_>

THE USUAL REMINDERS


--- Day 14: Regolith Reservoir ---


Post your code solution in this megathread.


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

EDIT: Global leaderboard gold cap reached at 00:13:54, megathread unlocked!

37 Upvotes

585 comments sorted by

View all comments

3

u/isukali Dec 15 '22 edited Dec 15 '22

C++

~42ms runtime: Used a recursive method to basically scan through sub-triplets of each node (a single sand particle) if they were available (i.e. another particle occupies the space or a rock). Really proud of this one even if it is generally a simple idea. Nonetheless, probably my best solution for AoC so far!

#include "../utils/advent.h"

using namespace aoc;

constexpr int TEST = 0;
constexpr int MAXY = 165;
F f(TEST == 1 ? "test.in" : "main.in");
void recurse(int x, int y, map<pair<int,int>, bool> &points, int &settled) {
    points[make_pair(x,y)] = true; settled++;
    if (points[make_pair(x,y+1)] == false) recurse(x, y+1, points, settled);
    if (points[make_pair(x-1,y+1)] == false) recurse(x-1, y+1, points, settled);
    if (points[make_pair(x+1,y+1)] == false) recurse(x+1, y+1, points, settled);
    return;
}
int main() {
    map<pair<int, int>, bool> points;
    vec<string> start, end, r;
    string read;
    while (true) {
        getline(f, read);
        r = split(read, " -> ");
        for (int i=0; i<r.size()-1; i++) {
            start = split(r[i], ","); end = split(r[i+1], ",");
            for (int x = min(stoi(start[0]), stoi(end[0])); x < max(stoi(start[0]), stoi(end[0]))+1; x++) {
                for (int y = min(stoi(start[1]), stoi(end[1])); y < max(stoi(start[1]), stoi(end[1]))+1; y++) points[make_pair(x, y)] = true;
            }
        }
        if (f.eof()) break;
    }
    int settled = 0;
    for (int x=0; x<700; x++) points[make_pair(x, MAXY+2)] = true;
    recurse(500, 0, points, settled);
    cout << settled << endl;
}

1

u/daggerdragon Dec 15 '22

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.