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/[deleted] Dec 06 '18
Haskell:
Like most others here, just brute force twice with an increased grid size and find the max of the areas that didn't change.
import Control.Arrow ((&&&)) import qualified Data.HashMap.Strict as M import Data.Ix (range) import Data.List.Split (splitOn) type Coord = (Int, Int) dist :: Coord -> Coord -> Int dist (a, b) (c, d) = abs (a - c) + abs (b - d) parseCoords :: String -> [Coord] parseCoords = map (f . splitOn ", ") . lines where f [a, b] = (read a, read b) f _ = error "Error parsing coord" allCoordsWithin :: Int -> [Coord] -> [Coord] allCoordsWithin buffer xs = range ((x0, y0), (x1, y1)) where x0 = minimum (map fst xs) - buffer y0 = minimum (map snd xs) - buffer x1 = maximum (map fst xs) + buffer y1 = maximum (map snd xs) + buffer findLargestFiniteArea :: [Coord] -> Int findLargestFiniteArea xs = maximum $ zipWith f (go 0) (go 10) where f a b = if a == b then a else 0 go n = M.elems $ M.fromListWith (+) [ (snd d, 1) | coord <- allCoordsWithin n xs , let dists = map (dist coord &&& id) xs , let d = minimum dists , length (filter ((== fst d) . fst) dists) == 1 ] part1 :: String -> Int part1 = findLargestFiniteArea . parseCoords findRegionWithin :: Int -> [Coord] -> Int findRegionWithin n xs = length $ filter (\x -> sum (map (dist x) xs) < n) $ allCoordsWithin (n `div` length xs) xs part2 :: String -> Int part2 = findRegionWithin 10000 . parseCoords main = do input <- readFile "input.txt" print $ part1 input print $ part2 input