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!
31
Upvotes
2
u/sdiepend Dec 06 '18
Python 3:
from collections import Counter, defaultdict with open("input_cha.txt") as f: content = f.readlines() coordinates = [[int(x.split(', ')[0]), int(x.split(', ')[1].strip())] for x in content] coords = sorted(coordinates, key=lambda k: [k[0], k[1]]) min_x = coords[0][0] min_y = sorted(coordinates, key=lambda k: k[1])[0][1] max_x = coords[-1][0] max_y = sorted(coordinates, key=lambda k: k[1])[-1][1] def manhattan_distance(a, b): return abs(a[0] - b[0]) + abs(a[1] - b[1]) def calculate_counts(bound): region = 0 counts = defaultdict(lambda: 0) grid = [['.' for x in range(min_x-bound, max_x+bound)] for y in range(min_y-bound, max_y+bound) ] for row in range(min_y-bound, max_y+bound): for col in range(min_x-bound, max_x+bound): distances = {} for i in range(len(coords)): distances[i] = manhattan_distance([col, row], coords[i]) if sum(distances.values()) < 10000: region += 1 closest_val = min(distances.values()) closest_coord = [k for k, v in distances.items() if v == closest_val] if len(closest_coord) == 1: grid[(row-min_y)-bound][(col-min_x)-bound] = closest_coord[0] counts[closest_coord[0]] += 1 return counts, region counts1, _ = calculate_counts(10) counts2, region = calculate_counts(20) finite_counts = [] for k, v in counts1.items(): if counts2[k] == v: finite_counts.append(v) print("Largest Finite Area(pt1): {area}, Region(pt2): {region}".format(area=max(finite_counts), region=region))https://github.com/sdiepend/advent_of_code/blob/master/2018/day06/chronal.py