X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=advent11p.hs;h=cdcf0307ba2735c1dc04fcadb0a67ea086e1cd80;hb=4f70d5ef99ffcbeee55ccffdb3464a9bb947f3ab;hp=6e4e47d83d8ca97c9641f7a458a84239baa0bb0d;hpb=94acd6cb30c5ef1b1b6f364da9f91e2e00618bf8;p=advent-of-code-16.git diff --git a/advent11p.hs b/advent11p.hs index 6e4e47d..cdcf030 100644 --- a/advent11p.hs +++ b/advent11p.hs @@ -1,3 +1,9 @@ +-- Using the idea of canonical representation of buildings from +-- https://andars.github.io/aoc_day11.html by Andrew Foote, +-- plus my extension of represening the pairs as an integer. + +-- This version is A* search, using a priority queue for the agenda. + import Data.List (subsequences, (\\), sort, sortOn, nub, findIndices) import Data.Ord (comparing) import Data.Char (isDigit) @@ -7,7 +13,6 @@ import qualified Data.PQueue.Prio.Min as P data Item = Generator String | Microchip String deriving (Show, Eq) type Floor = [Item] data Building = Building Int [Floor] deriving (Show, Eq) --- data CBuilding = CBuilding Int [(Int, Int)] deriving (Show, Eq) data CBuilding = CBuilding Int Integer deriving (Show, Eq) data Agendum = Agendum {current :: Building, trail :: [CBuilding], cost :: Int} type Agenda = P.MinPQueue Int Agendum @@ -106,9 +111,7 @@ candidates agendum closed = newCandidates candidate = current agendum previous = trail agendum succs = legalSuccessors $ successors candidate - -- nonloops = (succs \\ previous) \\ closed - excludable = previous ++ closed - nonloops = filter (\s -> not $ (canonical s) `elem` excludable) succs + nonloops = filter (\s -> not $ (canonical s) `elem` closed) succs newCandidates = map (\a -> (cost a, a)) $ map (\n -> makeAgendum n) nonloops makeAgendum new = Agendum {current = new, trail = (canonical candidate):previous,