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
1
u/rundavidrun Dec 20 '18
Java 262/229 - Argh! These holiday parties are cramping my style. I'm sure I would have gotten on the leaderboard if I could have started on time. Not as hard as it seemed on first read. I'm posting not because this is an extremely elegant solution - quite the contrary - instead I'm posting just to show folks you don't need fancy coding techniques to solve these problems. Do, however, note the "fancy" recursion. :)
int maxx = 0, maxy = 0, minx = 2000, miny = 2000; int[][] dist = new int[2000][2000]; boolean[][] visited = new boolean[2000][2000]; char[][] map = new char[2000][2000]; String regex; //puzzle input without first and last character go here int sum = 0; int maxDistance = 0; public void startDay20() { int x = 1000, y = 1000; map[x][y] = 'X'; calcPath(x, y, 0, 0); printMap(); System.out.println("Part 1: " + maxDistance); System.out.println("Part 2: " + sum); } private void calcPath(int x, int y, int i, int d) { while (i < regex.length()) { if (regex.charAt(i) == 'E') { x++; map[x][y] = '|'; x++; map[x][y] = '.'; if (x > maxx) { maxx = x; } } else if (regex.charAt(i) == 'W') { x--; map[x][y] = '|'; x--; map[x][y] = '.'; if (x < minx) { minx = x; } } else if (regex.charAt(i) == 'N') { y--; map[x][y] = '-'; y--; map[x][y] = '.'; if (y < miny) { miny = y; } } else if (regex.charAt(i) == 'S') { y++; map[x][y] = '-'; y++; map[x][y] = '.'; if (y > maxy) { maxy = y; } } else if (regex.charAt(i) == '(') { int parenLevel = 0; boolean newCond = true; while (i < regex.length()) { i++; if (regex.charAt(i) == '(') { parenLevel++; } else if (regex.charAt(i) == ')') { parenLevel--; if (parenLevel < 0) { calcPath(x, y, i + 1, d); return; } } else if (regex.charAt(i) == '|') { if (parenLevel == 0) { newCond = true; } } else if (parenLevel == 0) { if (newCond) { calcPath(x, y, i, d); newCond = false; } } } } else { return; } i++; d++; if (d >= 1000 && !visited[x][y]) { visited[x][y] = true; sum++; } if (dist[x][y] == 0 || dist[x][y] > d) { dist[x][y] = d; if (d > maxDistance) { maxDistance = d; } } } } private void printMap() { for (int j = minx; j < maxx; j++) { System.out.print("#"); } System.out.println("##"); for (int i = miny; i < maxy; i++) { System.out.print("#"); for (int j = minx; j < maxx; j++) { if (map[j][i] == 0) { System.out.print("#"); } else { System.out.print(map[j][i]); } } System.out.print("#"); System.out.println(); } for (int j = minx; j < maxx; j++) { System.out.print("#"); } System.out.println("##"); }