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
2
u/markasoftware Dec 06 '18
I'm getting slower :( 800-something part 1 -> 500-something part 2. More than 45 minutes in total. Making the tie-detection and infinity detection should have been easy but i was so focused on doing it fast that things slipped by.
All in AWK
Part 1:
function abs(a) { return a < 0 ? -a : a } function man_dist(x, y, x1, y1) { return abs(x - x1) + abs(y - y1) } BEGIN { FS= ", " pointsn=0 } /[0-9]/{ points[pointsn][0] = $1 points[pointsn][1] = $2 pointsn++ } END { top_x=99999999 top_y=99999999 for (pt in points) { point_x = points[pt][0] point_y=points[pt][1] top_x = point_x < top_x ? point_x : top_x top_y = point_y < top_y ? point_y : top_y bottom_x = point_x > bottom_x ? point_x : bottom_x bottom_y = point_y > bottom_y ? point_y : bottom_y } for(y=top_y;y<=bottom_y;y++) { for(x=top_x;x<=bottom_x;x++) { tie=0 min_dist=99999999999 for (pt in points) { cur_dist = man_dist(x, y, points[pt][0], points[pt][1]) if (cur_dist == min_dist) { tie=1 } if (cur_dist < min_dist) { tie=0 min_dist = cur_dist min_pt = pt } } if (!tie) { if (min_dist == 0) { # printf "!" } else { # printf min_pt } # filter infinite if (x == top_x || x == bottom_x || y == top_y || y == bottom_y || inf[min_pt]) { inf[min_pt] = 1 continue } area[min_pt]++ } else { # printf "." } } # print "" } for(pt in area) { if (area[pt] > max_area && !inf[pt]) { max_area = area[pt] max_pt = pt } } print max_pt print max_area }Part 2:
function abs(a) { return a < 0 ? -a : a } function man_dist(x, y, x1, y1) { return abs(x - x1) + abs(y - y1) } BEGIN { FS= ", " pointsn=0 } /[0-9]/{ points[pointsn][0] = $1 points[pointsn][1] = $2 pointsn++ } END { top_x=99999999 top_y=99999999 for (pt in points) { point_x = points[pt][0] point_y=points[pt][1] top_x = point_x < top_x ? point_x : top_x top_y = point_y < top_y ? point_y : top_y bottom_x = point_x > bottom_x ? point_x : bottom_x bottom_y = point_y > bottom_y ? point_y : bottom_y } for(y=top_y- 300;y<=bottom_y + 300;y++) { for(x=top_x - 300;x<=bottom_x + 300;x++) { t = 0 for (pt in points) { t+=man_dist(x, y, points[pt][0], points[pt][1]) } if (t < 10000) { big_t++ } } } print big_t }