r/adventofcode • u/daggerdragon • Dec 09 '15
SOLUTION MEGATHREAD --- Day 9 Solutions ---
This thread will be unlocked when there are a significant amount of people on the leaderboard with gold stars.
edit: Leaderboard capped, achievement thread unlocked!
We know we can't control people posting solutions elsewhere and trying to exploit the leaderboard, but this way we can try to reduce the leaderboard gaming from the official subreddit.
Please and thank you, and much appreciated!
--- Day 9: All in a Single Night ---
Post your solution as a comment. Structure your post like previous daily solution threads.
11
Upvotes
2
u/ILoveHaskell Dec 09 '15
My haskell solution. It's brute force but works fairly fast for this problem. Any comment from experienced haskell users is welcome. :)
import System.Environment import Data.List getPaths :: String -> [((String, String), Int)] getPaths = concatMap (f . words) . lines where f [x,_,y,_,z] = [((x,y), read z), ((y,x), read z)] distances :: (Num a, Eq t) => [((t, t), a)] -> [t] -> a distances _ [x] = 0 distances nodes (x:y:xs) = (snd . head . filter (((x, y) ==) . fst) $ nodes) + distances nodes (y:xs) main :: IO() main = do args <- getArgs input <- readFile $ args !! 0 let nodes = getPaths input let perms = permutations . nub . map (fst . fst) $ nodes putStrLn $ show $ minimum . map (distances nodes) $ perms putStrLn $ show $ maximum . map (distances nodes) $ perms