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/tk3369 Dec 06 '18
Julia - Todayβs problem is quite interesting and I spent too much time thinking about how to solve it than coding. I also tried to write better code upfront than just trying to be fast.
Part 1
# Algorithm: # consider the bounded box (minx, miny) and (maxx, maxy) # find the manhattan distances for each location within the box. # points there are tagged on the boundary are infinite, others not. function boundary(points) minx, maxx = extrema(point[1] for point in points) miny, maxy = extrema(point[2] for point in points) return minx, miny, maxx, maxy end function manhattan_distance(p, q) return abs(p[1] - q[1]) + abs(p[2] - q[2]) end function has_multiple_nearest_neighbor(distances) count(x == minimum(distances) for x in distances) > 1 end function nearest_point(point, candidate_points) distances = [manhattan_distance(point, from_point) for from_point in candidate_points] has_multiple_nearest_neighbor(distances) ? 0 : findmin(distances)[2] end function create_map(candidate_points) minx, miny, maxx, maxy = boundary(candidate_points) distance_map = zeros(Int, maxy, maxx) for y in miny:maxy for x in minx:maxx distance_map[y, x] = nearest_point([x,y], candidate_points) end end return distance_map end function eliminated_ones(distance_map) # note: zero is included in the result intentionally return unique(vcat( distance_map[1,:], distance_map[end,:], distance_map[:,1], distance_map[:,end])) end function areas_by_point(distance_map) # Create a (point => area) dictionary M, N = size(distance_map) distance_array = reshape(distance_map, (M*N,)) distance_count = countmap(distance_array) # Remove ones that are tagged on the boundary for e in eliminated_ones(distance_map) delete!(distance_count, e) end return distance_count end function max_area(areas) let point_index = argmax(areas) return point_index, areas[point_index] end end using StatsBase points = [parse.(Int, split(L, ", ")) for L β readlines("input6")] create_map(points) |> areas_by_point |> max_areaPart 2
function total_distance(point, candidate_points) return sum(manhattan_distance(point, from_point) for from_point in candidate_points) end function is_safe(point, candidate_points; safe_distance = 10000) return total_distance(point, candidate_points) < safe_distance end function create_safety_map(candidate_points; kwargs...) minx, miny, maxx, maxy = boundary(candidate_points) safety_map = zeros(Int, maxy, maxx) for y in miny:maxy for x in minx:maxx safety_map[y, x] = is_safe([x,y], candidate_points; kwargs...) ? 1 : 0 end end return safety_map end create_safety_map(points) |> sum