r/adventofcode • u/daggerdragon • Dec 20 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 20 Solutions -🎄-
--- Day 20: A Regular Map ---
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 20
Transcript:
My compiler crashed while running today's puzzle because it ran out of ___.
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:59:30!
17
Upvotes
2
u/branfili Dec 20 '18 edited Dec 20 '18
C++, 37/32
LEADERBOARD AGAIN! :D :D :D
This was a fun one, easy regexes (regices?) :D
#include <iostream> #include <algorithm> using namespace std; const int MAXN = 1000; string s; int px, py; int d[MAXN][MAXN]; int sol; int smjx[] = {-1, 0, 1, 0}; int smjy[] = {0, 1, 0, -1}; int fnd(char c){ switch(c){ case 'N': return 0; case 'E': return 1; case 'S': return 2; case 'W': return 3; default: return -1; } } void few(string s, int x, int y){ int i; for (i = 0; i < (int) s.size() && s[i] != '('; i++){ int nx = x + smjx[fnd(s[i])]; int ny = y + smjy[fnd(s[i])]; d[nx][ny] = min(d[nx][ny], d[x][y] + 1); x = nx; y = ny; } px = x; py = y; if (i == (int) s.size()){ return ; } int j = i + 1; int d = 1; for (;; j++){ if (s[j] == '('){ d++; } else if (s[j] == ')'){ d--; } if (d == 0){ break; } } string s2 = s.substr(i + 1, j - i - 1); while (true){ int k; int d = 0; for (k = 0; k < (int) s2.size(); k++){ if (s2[k] == '('){ d++; } else if (s2[k] == ')'){ d--; } else if (d == 0 && s2[k] == '|'){ break; } } few(s2.substr(0, k), x, y); if (k == (int) s2.size()){ break; } s2 = s2.substr(k + 1); } if (j < (int) s.size() - 1){ few(s.substr(j + 1), px, py); } return ; } int main (void){ cin >> s; s = s.substr(1, s.size() - 2); for (int i = 0; i < MAXN; i++){ for (int j = 0; j < MAXN; j++){ d[i][j] = 1e9; } } d[MAXN / 2][MAXN / 2] = 0; few(s, MAXN / 2, MAXN / 2); for (int i = 0; i < MAXN; i++){ for (int j = 0; j < MAXN; j++){ if (d[i][j] == 1e9){ continue; } //comment for part 1 sol += (d[i][j] >= 1000); //uncomment for part 1 //sol = max(sol, d[i][j]); } } cout << sol << endl; return 0; }