1 -- Using the idea of canonical representation of buildings from
2 -- https://andars.github.io/aoc_day11.html by Andrew Foote,
3 -- plus my extension of represening the pairs as an integer.
5 -- This version is A* search, using a list for the agenda.
7 import Data.List (subsequences, (\\), sort, sortOn, nub, findIndices)
8 import Data.Ord (comparing)
9 import Data.Char (isDigit)
11 data Item = Generator String | Microchip String deriving (Show, Eq)
13 data Building = Building Int [Floor] deriving (Show, Eq)
14 data CBuilding = CBuilding Int Integer deriving (Show, Eq)
15 data Agendum = Agendum {current :: Building, trail :: [CBuilding], cost :: Int}
17 instance Ord Item where
18 compare (Generator a) (Generator b) = compare a b
19 compare (Microchip a) (Microchip b) = compare a b
20 compare (Generator _) (Microchip _) = LT
21 compare (Microchip _) (Generator _) = GT
23 instance Ord Building where
24 compare b1 b2 = comparing estimateCost b1 b2
26 building1 = Building 0 [
27 (sort [Generator "polonium", Generator "thulium",
28 Microchip "thulium", Generator "promethium", Generator "ruthenium",
29 Microchip "ruthenium", Generator "cobalt", Microchip "cobalt"]),
30 (sort [Microchip "polonium", Microchip "promethium"]),
35 building0 = Building 0 [
36 (sort [Generator "polonium", Generator "thulium",
37 Microchip "thulium", Generator "promethium"]),
38 (sort [Microchip "polonium", Microchip "promethium"]),
43 building2 = Building 0 [
44 (sort [Generator "polonium", Generator "thulium",
45 Microchip "thulium", Generator "promethium", Generator "ruthenium",
46 Microchip "ruthenium", Generator "cobalt", Microchip "cobalt",
47 Generator "elerium", Microchip "elerium",
48 Generator "dilithium", Microchip "dilithium"]),
49 (sort [Microchip "polonium", Microchip "promethium"]),
55 buildingTest = Building 0 [
56 sort([Microchip "hydrogen", Microchip "lithium"]),
57 [Generator "hydrogen"],
58 [Generator "lithium"],
61 canonical :: Building -> CBuilding
62 canonical (Building f floors) = CBuilding f (read $ filter (isDigit) $ show $ sort pairs)
63 where names = nub $ map (\(Generator n) -> n) $ filter (isGenerator) $ concat floors
64 floorOf (Generator g) = head (findIndices
65 (\fl -> (Generator g) `elem` fl)
67 floorOf (Microchip g) = head (findIndices
68 (\fl -> (Microchip g) `elem` fl)
70 pairs = foldl (\ps n -> (floorOf (Generator n), floorOf (Microchip n)):ps) [] names
81 part1 = print $ length $ trail $ aStar (initAgenda building1) []
84 part2 = print $ length $ trail $aStar (initAgenda building2) []
86 initAgenda :: Building -> [Agendum]
87 initAgenda b = [Agendum {current = b, trail=[], cost = estimateCost b}]
90 aStar :: [Agendum] -> [CBuilding] -> Agendum
91 aStar [] _ = Agendum {current=buildingTest, trail=[], cost=0}
92 aStar (currentAgendum:agenda) closed =
93 if isGoal reached then currentAgendum
94 else if creached `elem` closed
95 then aStar agenda closed
96 else aStar newAgenda (creached:closed)
98 reached = current currentAgendum
99 creached = canonical reached
102 agenda ++ (candidates currentAgendum closed)
105 candidates :: Agendum -> [CBuilding] -> [Agendum]
106 candidates agendum closed = newCandidates
108 candidate = current agendum
109 previous = trail agendum
110 succs = legalSuccessors $ successors candidate
111 excludable = previous ++ closed
112 nonloops = filter (\s -> not $ (canonical s) `elem` excludable) succs
113 newCandidates = map (\n -> makeAgendum n) nonloops
114 makeAgendum new = Agendum {current = new,
115 trail = (canonical candidate):previous,
116 cost = estimateCost new + length previous + 1}
118 isGoal :: Building -> Bool
119 isGoal (Building f floors) =
120 f+1 == height && (all (null) $ take f floors)
121 where height = length floors
123 isLegal :: Building -> Bool
124 isLegal (Building f floors) =
127 not (any (isGenerator) floor)
130 where floor = floors!!f
131 pairs = [(i, j) | i <- floor, j <- floor, isGenerator i]
132 safePair (Generator e, Microchip f) = e == f
133 safePair (Generator _, Generator _) = False
135 isGenerator :: Item -> Bool
136 isGenerator (Generator _) = True
137 isGenerator (Microchip _) = False
139 successors :: Building -> [Building]
140 successors (Building f floors) = [updateBuilding f floors nf is | nf <- nextFloors, is <- items]
143 items = filter (\is -> length is == 1 || length is == 2) $ subsequences floor
144 nextFloors = if f == 0 then [1]
145 else if f+1 == length floors then [f-1]
148 legalSuccessors :: [Building] -> [Building]
149 legalSuccessors = filter (isLegal)
151 updateBuilding :: Int -> [Floor] -> Int -> [Item] -> Building
152 updateBuilding oldF oldFloors newF items = Building newF newFloors
153 where newFloors = map (updateFloor) $ zip [0..] oldFloors
155 | f == oldF = sort $ fl \\ items
156 | f == newF = sort $ items ++ fl
159 estimateCost :: Building -> Int
160 estimateCost (Building _ floors) =
161 sum $ map (\(c, f) -> c * length f) $ zip [0..] $ reverse floors