28 lines
793 B
Haskell
28 lines
793 B
Haskell
import Aoc
|
|
import Data.List ( nub )
|
|
import Data.Tuple ( swap )
|
|
import Control.Applicative ( (<|>) )
|
|
import Data.Maybe ( fromMaybe )
|
|
|
|
parseFile :: [String] -> [((String, String), Int)]
|
|
parseFile = map (\line ->
|
|
let [a, "to", b, "=", dist] = words line in
|
|
((a, b), read dist))
|
|
|
|
symLookup :: Eq a => (a, a) -> [((a,a), b)] -> Maybe b
|
|
symLookup a ls = lookup a ls <|> lookup (swap a) ls
|
|
|
|
process :: [((String, String), Int)] -> [Int]
|
|
process connections = map (totalLength . adjacents) (permutations cities)
|
|
where
|
|
cities = nub $ concatMap (unpair . fst) connections
|
|
totalLength = sum . map (fromMaybe undefined . (`symLookup` connections))
|
|
|
|
|
|
part1 :: [Int] -> Int
|
|
part1 = minimum
|
|
|
|
part2 :: [Int] -> Int
|
|
part2 = maximum
|
|
|
|
main = aocMain (process . parseFile) part1 part2 |