advent-of-code/2015/day09/day09.hs

28 lines
793 B
Haskell
Raw Normal View History

2024-12-03 06:47:58 +11:00
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