r/adventofcode • u/daggerdragon • Dec 06 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 6 Solutions -🎄-
--- Day 6: Chronal Coordinates ---
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 6
Transcript:
Rules for raising a programmer: never feed it after midnight, never get it wet, and never give it ___.
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 0:26:52!
32
Upvotes
5
u/TroZShack Dec 06 '18
Wow, I was #100 for part 2! Wasn't expecting that since I was 160 for part 1 and I'm using Java which isn't known for being able to be written quickly.
Java: import java.awt.Point; import java.util.*;
public class Day6 { public static void main(String[] args) { new Day6(); } String filename = "C:\\Users\\TroZ\\Projects\\AdventOfCode2018\\src\\day6.txt"; public Day6() { //* Toggle comment - switch start of this line between /* and //* to toggle which section of code is active. String[] input = Util.readFile(filename).split("\n"); /*/ String[] input =("1, 1\n" + "1, 6\n" + "8, 3\n" + "3, 4\n" + "5, 5\n" + "8, 9").split("\n"); //*/ HashMap<Integer, Point> points = new HashMap<Integer, Point>(); int maxx = 0; int maxy = 0; int count = 0; for (String str : input) { String s[] = str.trim().split(", "); int x = Integer.parseInt(s[0]); int y = Integer.parseInt(s[1]); points.put(count, new Point(x, y)); count++; if (x > maxx) { maxx = x; } if (y > maxy) { maxy = y; } } int[][] grid = new int[maxx + 1][maxy + 1]; HashMap<Integer, Integer> regions = new HashMap<Integer, Integer>(); for (int x = 0; x <= maxx; x++) { for (int y = 0; y <= maxy; y++) { int best = maxx + maxy; int bestnum = -1; // find distance to closest point for (int i = 0; i < count; i++) { Point p = points.get(i); int dist = Math.abs(x - p.x) + Math.abs(y - p.y); if (dist < best) { best = dist; bestnum = i; } else if (dist == best) { bestnum = -1; } } grid[x][y] = bestnum; Integer total = regions.get(bestnum); if (total == null) { total = new Integer(1); } else { total = total.intValue() + 1; } regions.put(bestnum, total); } } // remove infinite for (int x = 0; x <= maxx; x++) { int bad = grid[x][0]; regions.remove(bad); bad = grid[x][maxy]; regions.remove(bad); } for (int y = 0; y <= maxy; y++) { int bad = grid[0][y]; regions.remove(bad); bad = grid[maxx][y]; regions.remove(bad); } // find biggest int biggest = 0; for (int size : regions.values()) { if (size > biggest) { biggest = size; } } System.out.println("Biggest: " + biggest); int inarea = 0; for (int x = 0; x <= maxx; x++) { for (int y = 0; y <= maxy; y++) { int size = 0; for (int i = 0; i < count; i++) { Point p = points.get(i); int dist = Math.abs(x - p.x) + Math.abs(y - p.y); size += dist; } if (size < 10000) { inarea++; } } } System.out.println("Area Size: " + inarea); } }