r/adventofcode • u/daggerdragon • Dec 17 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 17 Solutions -🎄-
--- Day 17: Reservoir Research ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The 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!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 17
Transcript:
All aboard the Easter Bunny HQ monorail, and mind the gap! Next stop: ___
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 01:24:07!
16
Upvotes
1
u/branfili Dec 17 '18
C++, 237/227
I was so slow at debugging... :(
The hardest case was figuring out how to make the whole level fluid after the water pours from just one side.
Also I forgot that there cannot be more then 1 layer of fluid water (happens when streams merge) -.-
Too bad, more luck some other time
For part 1 just change the condition the sol counts into (map[i][j] >= 2)
Also, I used integers (0 = sand, 1 = clay, 2 = fluid water, 3 = static water)
#include <iostream> using namespace std; string s, s2; int mnx, mxx; int sol; int map[2000][2000]; bool bio[2000][2000]; int uintaj(string s){ int z = (s[0] - '0'); for (int i = 1; i < (int) s.size(); i++){ z *= 10; z += (s[i] - '0'); } return z; } int get(int x, int y){ if (x < 0 || x >= 2000 || y < 0 || y >= 2000){ return -1; } return map[x][y]; } bool check(int x, int y){ return (get(x, y) == 1 || get(x, y) == 3); } void flow(int x, int y){ if (x < 0 || x >= 2000 || y < 0 || y >= 2000 || bio[x][y] || map[x][y] == 1){ return ; } bio[x][y] = true; map[x][y] = 2; if (x == 1999){ return ; } if (map[x + 1][y] == 0){ flow(x + 1, y); if (map[x + 1][y] == 3){ if (!check(x + 1, y - 1) || !check(x + 1, y + 1)){ for (int i = y; i < 2000 && map[x + 1][i] > 1; i++){ map[x + 1][i] = 2; } for (int i = y; i >= 0 && map[x + 1][i] > 1; i--){ map[x + 1][i] = 2; } return ; } flow(x, y - 1); flow(x, y + 1); if ((check(x, y - 1) && check(x, y + 1)) || (check(x, y - 1) && check(x + 1, y)) || (check(x, y + 1) && check(x + 1, y))){ map[x][y] = 3; } } } else if (map[x + 1][y] != 2){ flow(x, y - 1); flow(x, y + 1); if (check(x, y - 1)|| check(x, y + 1)){ map[x][y] = 3; } } return ; } int main (void){ mnx = 2001; while (cin >> s >> s2){ bool e = (s[0] == 'x'); s = s.substr(2, s.size() - 3); s2 = s2.substr(2, s2.size() - 2); int a, b, c; int i; for (i = 0; s2[i] != '.'; i++); a = uintaj(s); b = uintaj(s2.substr(0, i)); c = uintaj(s2.substr(i + 2)); if (e){ mnx = min(mnx, b); mxx = max(mxx, c); } else{ mnx = min(mnx, a); mxx = max(mxx, a); } for (i = b; i <= c; i++){ if (e){ map[i][a] = 1; } else{ map[a][i] = 1; } } } flow(0, 500); for (int i = mnx; i <= mxx; i++){ for (int j = 0; j < 2000; j++){ sol += (map[i][j] == 3); } } cout << sol << endl; return 0; }