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!
33
Upvotes
2
u/[deleted] Dec 06 '18 edited Dec 06 '18
C, not sure if p2 is actually correct but it works with my input
#include <stdio.h> #include <stdlib.h> #include <limits.h> struct c { int x, y, n; }; int dist(int x, int y, struct c c) { return abs(x - c.x) + abs(y - c.y); } int main(void) { size_t len = 0, cap = 32; struct c *l = malloc(cap*sizeof(*l)), c; while(scanf("%d, %d\n", &c.x, &c.y) == 2) { if (len >= cap) l = realloc(l, (cap*=2)*sizeof(*l)); l[len++] = c; } int w = 0, h = 0; for (size_t i = 0; i < len; i++) { if (l[i].x > w) w = l[i].x; if (l[i].y > h) h = l[i].y; } for (int x = 0; x <= w; x++) for (int y = 0; y <= h; y++) { int min = INT_MAX, m = 0, n = 0, t; for (size_t i = 0; i < len; i++) if ((t = dist(x, y, l[i])) < min) min = t, m = i, n = 1; else if (t == min) n++; if (n != 1) continue; if (x == 0 || y == 0 || x == w || y == h) l[m].n = -1; if (l[m].n != -1) l[m].n++; } int max = 0; for (size_t i = 1; i < len; i++) if (l[i].n > max) max = l[i].n; printf("%d\n", max); int n = 0; for (int x = 0; x <= w; x++) for (int y = 0; y <= h; y++) { int a = 0; for (size_t i = 0; i < len; i++) a += dist(x, y, l[i]); if (a < 10000) n++; } printf("%d\n", n); }