-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pipemaze.hs
1262 lines (1079 loc) · 57.8 KB
/
Pipemaze.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# LANGUAGE TupleSections, NamedFieldPuns, BinaryLiterals, TemplateHaskell, CPP, ScopedTypeVariables, NumericUnderscores, FlexibleInstances #-}
{-# LANGUAGE StrictData, BangPatterns #-}
{-# OPTIONS_GHC -O #-}
{-# OPTIONS_GHC -fexpose-all-unfoldings #-} -- no observable effect from these
{-# OPTIONS_GHC -fspecialise-aggressively #-} -- no observable effect from these
{-# LANGUAGE RankNTypes #-} -- for maybeSet
{-# OPTIONS_GHC -Wall -Wno-name-shadowing -Wno-unused-do-bind -Wno-type-defaults -Wno-missing-signatures #-}
{-# OPTIONS_GHC -Wno-unused-top-binds #-} -- complains about unused lenses :/
-- json
{-# LANGUAGE DeriveGeneric #-}
{-|
Module : Pipemaze
Description : solves pipe mazes
Copyright : (c) siers
License : GPL-3
Maintainer : [email protected]
Stability : experimental
Portability : POSIX
A dynamic solver of pipe mazes with an internal 'priority' queue based on scores, position and priority creation order and much more.
-}
module Pipemaze (
-- * High-level description
-- $description
-- * Islands
-- $islands
-- * Bugs and improvements
-- $bugs
-- * Types
Direction, Rotation, Pix, Cursor, Fursor, MMaze(..), Piece(..), Choices, PartId, Continue(..)
, Priority, Continues, Components(..), Unwind(..), Progress(..), Island(..), IslandSolution(..)
, Bounds, SolveMode(..), Configuration(..), Solver, SolverT
-- * Maze operations
, parse, mazeStore, mazeBounded, mazeCursor, mazeFursor, mazeRead, mazeModify
, mazeClone, mazeSolve, mazeDelta, mazeFDelta, mazeEquate, mazePop, partEquate
-- * Tracing and rendering
, renderColor, renderStr, renderImage'
, traceBoard
-- * Pixel model
, directions, rotations, charMap, pixMap, pixRotations, pixDirections, directionsPix, toPix, toChar, rotate
-- * Pixel solving
, pixValid, validateDirection, pieceChoices
-- * Component indexing
, compInsert, compRemove, compEquate, compAlive, compConnected, compCounts
-- * Continue operations
, deltaContinue, prioritizeDeltas, rescoreContinue, prioritizeContinue, prioritizeContinues
, pieceDead, findContinue, popContinue
-- * Island computations
, FillNext, flood, islandize, islandConnectivityRefinement, islandChoices, islands
-- * Backtracker
, solveContinue, backtrack, solve'
-- * Metasolver
, islandChoicesParallel, solveDetParallel, initProgress, solve
-- * Main
, verify, storeBad, rotateStr, configuration, pļāpātArWebsocketu, solveFile, main
) where
-- solver
-- Lens TemplateHaskell
import Control.Lens.Internal.FieldTH (makeFieldOptics, LensRules(..))
import Language.Haskell.TH.Syntax (mkName, nameBase)
import Control.Lens.TH (DefName(..), lensRules)
-- Solver
import Algebra.PartialOrd (PartialOrd(..))
import Control.Concurrent (getNumCapabilities)
import Control.Lens (Setter', (&), (%~), set, _1, _2, _head, _Just)
import Control.Monad.Extra (allM, whenM)
import Control.Monad.IO.Class (MonadIO(..), liftIO)
import Control.Monad (join, filterM, void, unless, when, mfilter, replicateM, (<=<))
import Control.Monad.Primitive (RealWorld)
import Control.Monad.Reader (MonadReader(..), Reader, ReaderT(..), ask, asks, withReaderT, mapReaderT)
import Control.Monad.Trans.State.Strict (StateT(..))
import Data.Char (ord)
import Data.Foldable (traverse_, for_, foldlM, fold)
import Data.Function (on)
import Data.Functor.Identity (Identity(..), runIdentity)
import Data.IntMap.Strict (IntMap)
import Data.IntSet (IntSet)
import Data.List.Extra (nubOrd, groupSort, groupSortOn, splitOn, chunksOf, intersperse, transpose)
import Data.Map.Strict (Map, (!))
import Data.Maybe (fromMaybe, fromJust, isJust, maybeToList)
import Data.Monoid (Sum(..))
import Data.Set (Set)
import Data.Traversable (for)
import Data.Tuple (swap)
import Data.Vector.Storable.Mutable (IOVector)
import Data.Word (Word8)
-- import Debug.Trace (trace)
import Foreign.Storable.Generic
import Graphics.Image.Interface (thaw, MImage, freeze, write)
import Graphics.Image (writeImage, makeImageR, Pixel(..), toPixelRGB, VU(..), RGB)
import Numeric (showHex)
import qualified Control.Monad.Trans.State.Strict as State
import qualified Data.Bits as Bit
import qualified Data.IntMap.Strict as IntMap
import qualified Data.IntSet as IntSet
import qualified Data.List as List
import qualified Data.Map.Strict as Map
import qualified Data.POSet as POSet
import qualified Data.Set as Set
import qualified Data.Vector.Storable as V
import qualified Data.Vector.Storable.Mutable as MV
import System.Clock (getTime, Clock(Monotonic), diffTimeSpec, toNanoSecs, TimeSpec)
import System.Environment (lookupEnv, getArgs)
import System.Directory (createDirectoryIfMissing)
import Text.Printf (printf)
import Data.Time.Clock (getCurrentTime)
import Data.Time.Format (formatTime, defaultTimeLocale)
-- parallel
import Control.Concurrent.ParallelIO.Global (parallelInterleaved)
-- json for debug outputs
import Data.Aeson (ToJSON(..))
-- import qualified Data.Aeson as Aeson
-- import qualified Data.ByteString.Lazy.Char8 as LBS
import GHC.Generics (Generic)
-- Main IO
import Data.Text (Text)
import Network.Socket (withSocketsDo)
import qualified Data.Text as T
import qualified Network.WebSockets as WS
import System.IO (hFlush, stdout)
-- t a = seq (trace (show a) a) a
-- t' l a = seq (trace (show l ++ ": " ++ show a) a) a
-- tM :: (Functor f, Show a) => f a -> f a
-- tM = fmap t
-- $description
-- The goal of the solver is to find the 'Rotation' of each 'Pix' in the grid to create a connected undirected graph.
-- Just like in this game here: <https://www.puzzle-pipes.com/>
--
-- * Each cursor has four nearby cursors – top, right, bottom, left, which wil be refered to as 'directions' or 'deltas'.
-- * The 'Cursor's in one square's distance in a 'direction' with respect to some cursor @c@ may also be called 'deltas'.
-- * The number of valid rotations of a piece are encoded in 'Choices'.
-- * Each 'Piece' contains initial 'Choices' which are bit-packed info 'initChoices',
-- * Distinct graphs are being refered to as /components/ and each has a distinct 'PartId' computed by the smallest 'Fursor' of the graph.
-- <https://en.wikipedia.org/wiki/Component_(graph_theory)>
-- * All 'Component\''s open ends are refered to as 'Continue's,
-- which then later get stored in 'Continue' to make a piece "active", making 'Piece' information is no longer necessary.
-- * The cursors are stored as flat indexes ('Fursor') to make comparison computations faster ('IntMap'). Conversions to 'Cursor'
-- are possible via 'mazeCursor', 'mazeFursor'.
-- * 'Progress' holds main data of the backtracker, the only mutable data is 'MMaze', which stores correctly solved 'Pix' and
-- 'partId's which get updated as graphs become joined.
--
-- Solving each piece gives you hints about surrounding pieces,
-- so solving them by diagonals ('rescoreContinue') is more effective than solving in an arbitrary order.
-- If the connectivity of the pieces is efficiently computed ('PartId' \/ 'partEquate'),
-- the "open ends" ('Continues') have a good prioritization and the disconnected solves are efficiently (computed
-- and then) discarded ('pieceDead' \/ 'compAlive'), you can solve around 98% of the level 6 maze determinstically.
-- $islands
-- Island is a patch of unsolved pieces, each one has its own number of solutions.
-- Solving it by backtracking makes the search space multiply after guessing one island after another in a single 'Progress'.
-- Instead, you can force the backtracker to only count the solutions of a single island with 'cBounds'.
-- If you group the solutions ('islandConnectivityRefinement'), many islands have a single best or equivalent solution.
-- 'solveTrivialIslands' runs the backtracker for all "simple" islands, then recomputes all islands, then repeats.
-- The even simpler 'solveIslandStatic' solves all the pieces that remain the same in all 'IslandSolution's.
-- Running either solves level 6 very fast and 'solveIslandStatic' (+ 'solve') is a little faster.
--
-- Grouping in 'islandConnectivityRefinement' takes place in two steps: group by equal 'Components',
-- refine 'icConnections' by their partition 'PartialOrd'.
-- $bugs
-- * `partId`/`origin` is a little awkward, but `pipe`/`char` is even more so
-- * 'Continue's can be removed, if their old versions are added in 'Unwind's
-- * A separate priority list could be mae for choices = 1
-- * All 'IslandSolution's are getting recomputed after each determinstic solve, which can be fixed, but it's already very fast
-- * 'IslandSolution' could be recomputable without running main solver,
-- just by checking that nothing gets disconnected after running it through all the 'Unwind's of last solve.
-- * Backtracking on 'IslandSolution' could be implemented
-- * 'IslandSolution's could be chosen by just checking that the 'icConnections' connect some graph constructed from islands.
-- * 'IslandSolution' could have a heuristic for the number of solutions without solving all
-- solutions by solving in breadth-wise first choices – '[Progress]' and only then depth-wise.
-- | Directions: top 0, right 1, bottom 2, left 3
type Direction = Int
-- | The set of rotation values are the same as directions.
type Rotation = Int
-- | The maze symbol (has four edges) bit-packed in 'charMap' as @2^d@ per direction, mirrored in @shiftL 4@ to help bit rotation
--
-- > X – ╋ -- 4 edges
-- > I – ━, ┃ -- 2 edges
-- > L – ┏, ┛, ┓, ┗ -- 2 edges
-- > T – ┣, ┫, ┳, ┻ -- 3 edges
-- > i – ╸, ╹, ╺, ╻ -- 1 edge
type Pix = Word8
type Cursor = (Int, Int)
-- | Flat cursors, for @f :: FCursor, (x, y) :: Cursor, f = x + y * width@. Through `Choices`, bound checking is cached, so deltas are just ±1 or ±width.
type Width = Int
type Fursor = Int
-- | unlawful instance
instance Eq (IOVector Piece) where _ == _ = True
-- | unlawful instance
instance Ord (IOVector Piece) where _ <= _ = True
-- | Mutable maze operated on by functions in section /"Maze operations"/
data MMaze = MMaze
{ board :: IOVector Piece -- ^ flat MVector with implied 2d structure via 'Cursor'/'Fursor' + index computations
, width :: Int
, height :: Int
, size :: Int
, sizeLen :: Int -- ^ leading char count for @printf %0ni@ format @(~logBase 10 size + 1.5)@
, level :: Int
, trivials :: [Fursor] -- ^ cursors of the edge and @X@ pieces which have only one valid rotation
, mazeId :: String -- ^ 'board's data scrambled into a 4-byte hexadecimal field
, time :: TimeSpec
} deriving (Eq, Ord, Generic)
data Piece = Piece
{ pipe :: Pix
, solved :: Bool
, partId :: PartId -- ^ meaningless if not connected
, connected :: Bool -- ^ connected when pointed to by a 'solved' piece
, initChoices :: Choices
} deriving (Show, Eq, Ord, Generic)
instance GStorable Piece
-- | 'Choices' is bit-packed info related to the valid rotations of a picce.
-- In MSB order: (valid) rotation count 2b, invalid rotation directions 4b (unused), solved requirements 4b, solved neighbours 4b
type Choices = Int
(choicesSolveds, choicesInvalid, choicesCount) = (0, 4, 8)
-- | Continue represents the piece that should be solved next according to 'Priority', which is an open end of a component
-- (or starts one). Created in 'initProgress' or 'deltaContinue'.
data Continue = Continue
{ cursor :: Fursor
, char :: Pix -- ^ from Piece at 'cursor'
, origin :: PartId -- ^ component id, to be used with 'partEquate'
, score :: Int -- ^ see 'rescoreContinue'
, created :: Int -- ^ any unique id to make score order total ('Ord' requirement), taken from 'iter'
, island :: Int -- ^ \> 0 if island
, area :: Int -- ^ island score, 0 if not an island
, choices :: Choices
} deriving (Show, Eq, Ord, Generic)
-- | 'PartId' distinguishes the graph component by their smallest known 'Cursor' by its 'Ord' (unique),
-- so it is the same as its cursor initially. They're marked in 'origin' ahead of 'solved's.
-- 'PartId' in 'origin' is only to be used through 'partEquate', because 'origin' isn't being
-- updated after components have connected.
type PartId = Fursor
-- | 'Continue' priority queue, inserted by 'prioritizeContinue', found by 'findContinue', popped by 'popContinue'.
type Priority = IntMap Fursor
-- | Primary storage of 'Continue' data
type Continues = IntMap Continue
-- | The index of components' continues by their 'PartId' (which are always up-to-date).
data Components
= Components (IntMap Int) -- ^ marginally faster, but less info
| Components' (IntMap IntSet)
deriving (Show, Eq, Ord, Generic)
type Space = [([(Continue, Progress)], [Unwind])]
-- | For backtracking on the mutable 'MMaze' and for extracting hints.
data Unwind
= UnSolve Fursor Pix Pix -- ^ 'Pix' before, after (for deployHint)
| UnEquate Fursor Bool PartId -- ^ 'connected', 'PartId' after
deriving (Show, Eq, Ord, Generic)
data Progress = Progress
{ iter :: Int -- ^ the total number of backtracking iterations (incl. failed ones)
, depth :: Int -- ^ number of solves, so also the length of unwinds/space
, priority :: Priority -- ^ priority queue for next guesses (tree, not a heap, because reprioritizing is required)
, continues :: Continues -- ^ Primary continue store, pointed to by 'priority' (all 'Continue's within must be unique by their cursor)
, components :: Components -- ^ component continue counts (for quickly computing disconnected components via `compAlive`)
, space :: Space -- ^ backtracking's "rewind" + unexplored solution stack; an item per a solve. pop when (last 'space' == [])
, maze :: MMaze
} deriving (Eq, Ord, Generic)
type PrioCompCont = (Priority, Components, Continues)
-- | unlawful
instance Show Progress where
show Progress{depth, iter} =
"Progress" ++ show (depth, '/', iter)
type Bounds = Maybe (Fursor -> Bool)
bounded :: Bounds -> Fursor -> Bool
bounded b c = all ($ c) b
-- | Amalgamation of the flags "determinstic", "save history" and "deprioritize unbounded continues"
-- (it's for parallelism, see 'rescoreContinue').
data SolveMode = SolveNormal | SolveDeterministic | SolveIslandDeterministic | SolveParallel deriving (Show, Eq, Ord)
solveDeterministic SolveNormal = True
solveDeterministic _ = False
solveWithHistory SolveNormal = True
solveWithHistory SolveIslandDeterministic = False -- could be True if you plan to solve islands directly (reply unwinds)
solveWithHistory _ = False
data Configuration = Configuration
{ cDebug :: Int
, cDebugFreq :: Int
, cPixSize :: Int
, cLifespan :: Int
, cMode :: SolveMode
, cBounds :: Bounds -- ^ forces the solver to stay within bounds
, cBench :: Bool
, cImageDir :: String
, cNumCap :: Int
} -- deriving (Show, Eq, Ord, Generic)
type SolverT = ReaderT Configuration IO
type Solver = Reader Configuration
-- | Island is the patch of unsolved pieces surrounded by solved pieces, computed by 'flood' in 'islands'.
data Island = Island
{ iId :: Int
, iSize :: Int
, iConts :: [Continue]
, iBounds :: IntSet
, iSolutions :: [IslandSolution]
-- ^ all possible combinations (partitioned by partition equivalence), with hints to force solver choose that solve
, iChoices :: Int -- ^ same, but without details
} deriving (Show, Eq, Ord, Generic)
-- | IslandSolution represent a solution for an island with a representative progress.
-- The 'icConnections' are a partition of the components the island joined.
-- Partitions have a partial ordering called _refinement_ with which you can group 'iSolutions'.
-- <https://en.wikipedia.org/wiki/Partition_of_a_set#Refinement_of_partitions>
data IslandSolution = IslandSolution
-- { icProgess :: Progress
{ icConnections :: [Set PartId] -- the surrounding 'PartId' partition (induces a PartialOrd)
, icComponents :: IntMap Int
, icHints :: [Unwind] -- ^ forces the backtracker to choose this solution
} deriving (Show, Eq, Ord, Generic)
instance PartialOrd IslandSolution where
IslandSolution{icConnections=as} `leq` IslandSolution{icConnections=bs} =
all (flip any bs . Set.isSubsetOf) as
makeFieldOptics lensRules { _fieldToDef = \_ _ -> (:[]) . TopName . mkName . (++ "L") . nameBase } ''MMaze
makeFieldOptics lensRules { _fieldToDef = \_ _ -> (:[]) . TopName . mkName . (++ "L") . nameBase } ''Piece
makeFieldOptics lensRules { _fieldToDef = \_ _ -> (:[]) . TopName . mkName . (++ "L") . nameBase } ''Continue
makeFieldOptics lensRules { _fieldToDef = \_ _ -> (:[]) . TopName . mkName . (++ "L") . nameBase } ''Progress
makeFieldOptics lensRules { _fieldToDef = \_ _ -> (:[]) . TopName . mkName . (++ "L") . nameBase } ''Configuration
makeFieldOptics lensRules { _fieldToDef = \_ _ -> (:[]) . TopName . mkName . (++ "L") . nameBase } ''Island
toSolverT = mapReaderT (pure . runIdentity)
determinstically = withReaderT (set cModeL SolveDeterministic)
determinsticallyI = withReaderT (set cModeL SolveIslandDeterministic) -- for islands, see withHistory
confDefault = Configuration
{ cDebug = 0
, cDebugFreq = 10377
, cPixSize = 3
, cLifespan = - 1
, cMode = SolveNormal
, cBounds = Nothing
, cBench = False
, cImageDir = "images/"
, cNumCap = 1
}
-- | unlawful
instance Show MMaze where
-- | unlawful instance
show _ = "MMaze"
instance ToJSON Piece
instance ToJSON Continue
instance ToJSON Components
instance ToJSON Island
instance ToJSON Unwind
instance ToJSON IslandSolution
-- writeFile "out" . LBS.unpack . Aeson.encode . toJSON $ solutions
-- | https://hackage.haskell.org/package/monad-extras-0.6.0/docs/src/Control-Monad-Extra.html#iterateMaybeM
-- | Monadic equivalent to 'iterate', which uses Maybe to know when to terminate.
iterateMaybeM :: Monad m => Int -> (a -> m (Maybe a)) -> a -> m (Bool, [a])
iterateMaybeM 0 _ _ = pure (True, [])
iterateMaybeM n f x = fmap (False, ) $
maybe (return []) (\x' -> ((x':) . snd) `fmap` iterateMaybeM (n - 1) f x') =<< f x
{--- MMaze and Matrix operations ---}
parse :: String -> IO MMaze
parse input = do
time <- getTime Monotonic
maze <- (\b -> MMaze b width height size zeros level [] mazeId time) <$> V.thaw (V.fromList (map snd board))
(\m -> trivialsL (const (trivials m)) m) =<< boardL (const (setDeltas maze)) =<< pure maze
where
mazeId = showHex (foldr (\a b -> (ord a + b) `mod` (2 ^ 16)) 0 input) ""
rect :: [[Pix]] = filter (not . null) . map (map toPix) . lines $ input
board = zipWith (curry piece) [0..] . join $ rect
piece (fc, p) = (fc, Piece p False fc False 0)
(width, height) = (length (head rect), length rect)
size = width * height
zeros = floor (logBase 10 (fromIntegral size) + 1.5)
level = fromMaybe 7 (lookup size [(64,1), (500,2), (2_500,3), (20_000,4), (120_000,5), (1_000_000,6)])
setDeltas m = V.thaw . V.fromList =<< traverse (\(fc, p) -> initChoicesL (const (choices m (fc, p))) p) board
choices m (fc, _p) = do
let c = mazeCursor width fc
choices <- pieceChoices m c
let next = filter (not . mazeBounded' width height . mazeDelta c) directions
pure (choices + directionsPix next)
trivials :: MMaze -> IO [Fursor]
trivials MMaze{board} = map fst . filter trivial . zip [0..] . V.toList <$> V.freeze board
trivial (_, Piece{pipe, initChoices}) = pipe == 0b11111111 || Bit.shiftR initChoices choicesCount < 2
mazeStore :: MonadIO m => MMaze -> String -> m ()
mazeStore m label = liftIO (writeFile label =<< renderStr m)
{-# INLINE mazeBounded #-}
mazeBounded :: MMaze -> Cursor -> Bool
mazeBounded MMaze{width, height} = mazeBounded' width height
mazeBounded' :: Int -> Int -> Cursor -> Bool
mazeBounded' width height (!x, !y) = x >= 0 && y >= 0 && width > x && height > y
vectorLists :: Storable a => Int -> Int -> V.Vector a -> [[a]]
vectorLists width height board = [ [ board V.! (x + y * width) | x <- [0..width - 1] ] | y <- [0..height - 1] ]
{-# INLINE mazeCursor #-}
mazeCursor :: Width -> Fursor -> Cursor
mazeCursor width = swap . flip quotRem width
{-# INLINE mazeFursor #-}
mazeFursor :: Width -> Cursor -> Fursor
mazeFursor w (x, y) = x + y * w
{-# INLINE mazeRead #-}
mazeRead :: MonadIO m => MMaze -> Fursor -> m Piece
mazeRead MMaze{board} fc = liftIO (MV.unsafeRead board fc)
{-# INLINE mazeModify #-}
mazeModify :: MonadIO m => MMaze -> (Piece -> Piece) -> Fursor -> m ()
mazeModify MMaze{board} f fc = liftIO $ MV.unsafeModify board f fc
mazeClone :: MonadIO m => MMaze -> m MMaze
mazeClone = liftIO . boardL MV.clone
{-# INLINE mazeSolve #-}
mazeSolve :: MonadIO m => MMaze -> Continue -> m Unwind
mazeSolve MMaze{board} Continue{char=after, cursor} = do
p@Piece{pipe=before} <- liftIO (MV.unsafeRead board cursor)
liftIO $ MV.unsafeWrite board cursor p { pipe = after, solved = True }
pure (UnSolve cursor before after)
{-# INLINE mazeDelta #-}
mazeDelta :: Cursor -> Direction -> Cursor
mazeDelta (x, y) 0 = (x, y - 1)
mazeDelta (x, y) 1 = (x + 1, y)
mazeDelta (x, y) 2 = (x, y + 1)
mazeDelta (x, y) 3 = (x - 1, y)
mazeDelta _ _ = error "wrong direction"
{-# INLINE mazeFDelta #-}
mazeFDelta :: Int -> Fursor -> Direction -> Fursor
mazeFDelta w f 0 = f - w
mazeFDelta _ f 1 = f + 1
mazeFDelta w f 2 = f + w
mazeFDelta _ f 3 = f - 1
mazeFDelta _ _ _ = error "wrong direction"
mazeDeltasWalls :: MMaze -> Cursor -> IO [(Piece, Direction)]
mazeDeltasWalls m c = traverse (mazeDeltaWall m c) directions
{-# INLINE mazeDeltaWall #-}
mazeDeltaWall :: MMaze -> Cursor -> Direction -> IO (Piece, Direction)
mazeDeltaWall m@MMaze{width} c dir =
if mazeBounded m delta
then (, dir) <$> mazeRead m (x + y * width)
else pure (Piece 0 True 0 True 0, dir)
where delta@(x, y) = mazeDelta c dir
{-# INLINE mazeEquate #-}
-- | Connects 'PartId's on the board
mazeEquate :: MonadIO m => MMaze -> PartId -> [Fursor] -> m [Unwind]
mazeEquate MMaze{board} partId cursors = liftIO $
for cursors $ \cursor -> do
p@Piece{connected, partId=partId_} <- liftIO (MV.unsafeRead board cursor)
liftIO $ MV.unsafeWrite board cursor p { partId, connected = True }
pure (UnEquate cursor connected partId_)
{-# INLINE mazePop #-}
mazePop :: MonadIO m => MMaze -> Unwind -> m ()
mazePop m (UnSolve c pipe _) = mazeModify m (\p -> p { pipe, solved = False }) c
mazePop m (UnEquate c connected partId) = mazeModify m (\p -> p { partId, connected }) c
-- | Looks up the fixed point of 'PartId' (i.e. when it points to itself)
{-# INLINE partEquate #-}
partEquate :: MonadIO m => MMaze -> PartId -> m PartId
partEquate maze v = loop' =<< find v
where
find f = (\Piece{connected, partId} -> if connected then partId else f) <$> mazeRead maze f
loop' v' = (\found -> if v' == v || v' == found then pure v' else loop' found) =<< find v'
{--- Rendering, tracing ---}
-- | Generate uncolorized output
renderStr :: MMaze -> IO String
renderStr MMaze{board, width, height} =
unlines . map (concatMap (return . toChar . pipe)) . vectorLists width height . V.convert <$> V.freeze board
{- HLINT ignore renderImageN -}
renderImageN :: MonadIO m => Int -> String -> MMaze -> Continues -> m ()
renderImageN pixSize fn maze@MMaze{width, height} continues = liftIO $ seq continues $ do
mcanvas <- thaw canvas :: IO (MImage RealWorld VU RGB Double)
traverse_ (drawPiece mcanvas) grid
writeImage fn =<< freeze mcanvas
where
(pixW, pixH) = (pixSize, pixSize)
border = pixSize
canvas = makeImageR VU ((width + 2) * pixW, (height + 2) * pixH) $ const (PixelRGB 0 0 0)
grid = (,) <$> [0..width - 1] <*> [0..height - 1]
colorHash :: Cursor -> Double
colorHash (x, y) =
let
n = ((83 * fromIntegral x) / (37 * fromIntegral (y + 2)))
unfloor m = m - fromIntegral (floor m)
in unfloor n
drawPiece :: MImage RealWorld VU RGB Double -> Cursor -> IO ()
drawPiece image (x, y) = do
let fc = x + y * width
Piece{pipe, partId, solved} <- mazeRead maze fc
ch <- colorHash . mazeCursor width <$> partEquate maze partId
let cont = IntMap.lookup fc continues
let colo = maybe ch (\c -> if island c == 2 then 0.25 else 0.6) cont
let satu = if solved then 0.8 else (if isJust cont then 0.8 else 0)
let inte = if solved then 0.5 else (if isJust cont then 1 else 0.3)
let fill = if not solved && pipe == 0b11111111 then PixelRGB 1 1 1 else toPixelRGB $ PixelHSI colo satu inte
write' pixSize pipe fill
where
write' :: Int -> Pix -> Pixel RGB Double -> IO ()
write' 3 pipe fill = do
write image (border + x * pixW + 1, border + y * pixH + 1) fill
for_ (pixDirections pipe) $ \d ->
when (Bit.testBit pipe d) $
write image (mazeDelta (border + x * pixW + 1, border + y * pixH + 1) d) fill
write' 1 _ fill = write image (border + x * pixW, border + y * pixH) fill
write' _ _ _ = error "pixSize bad"
renderImage :: String -> MMaze -> Continues -> SolverT ()
renderImage s m c = asks cPixSize >>= \ps -> renderImageN ps s m c
-- | The output format is: @images/lvl%i-%s-%0*i-%s.png level mazeId (sizeLen iter) name@
renderImage' :: String -> Progress -> SolverT Progress
renderImage' name p@Progress{maze=maze@MMaze{sizeLen}, iter, continues} =
(p <$) . whenM (asks (not . cBench)) $ do
dir <- asks cImageDir
renderImage (printf (dir ++ "%0*i-%s.png") sizeLen iter name) maze continues
renderColorProgress :: MonadIO m => Maybe Continue -> Progress -> m String
renderColorProgress _ Progress{maze=maze@MMaze{board, width, height}} = liftIO $ do
lines <- vectorLists width height . V.convert <$> V.freeze board
unlines . map concat <$> traverse (traverse fmt) lines
where
colorHash = (`mod` 70) . (+15) . (\(x, y) -> x * 67 + y * 23)
fmt Piece{pipe, partId, solved} = do
color <- mfilter (const solved) . Just . colorHash . mazeCursor width <$> partEquate maze partId
pure $ case color of
Just color -> printf "\x1b[38;5;%im%c\x1b[39m" ([24 :: Int, 27..231] !! color) (toChar pipe)
_ -> [toChar pipe]
-- | Print unicode maze with colorized ANSI escape sequences to stdout.
renderColor :: MonadIO m => MMaze -> m ()
renderColor = liftIO . (putStrLn <=< renderColorProgress Nothing . Progress 0 0 IntMap.empty IntMap.empty (Components IntMap.empty) [])
-- | Tracing at each @freq=@ step with @debug=@ environment variables.
--
-- Modes: 1. print stats \/ 2. print maze with terminal escape code codes \/ 3. as 2., but with clear-screen before \/
-- 4. as 1., but with image output \/ 5. as 4., but only after islands have started
traceBoard :: Continue -> Progress -> SolverT Progress
traceBoard current progress@Progress{iter=iter', depth, maze=maze@MMaze{size}} = do
Configuration{cDebug, cDebugFreq} <- ask
islands <-
if iter `mod` max 1 (cDebugFreq `div` islandSlowdown) == 0
then any ((> 0) . island) <$> toSolverT (findContinue progress)
else pure False
progress <$ tracer cDebug (max 1 (cDebugFreq `div` (if islands then islandSlowdown else 1))) islands
where
(iter, islandSlowdown) = (iter' - 1, 50)
tracer :: Int -> Int -> Bool -> SolverT ()
tracer mode freq islandish
| iter `mod` freq == 0 && mode == 0 = liftIO $ putStrLn solvedStr
| iter `mod` freq == 0 && mode == 1 = liftIO $ renderStr maze >>= putStrLn
| iter `mod` freq == 0 && mode == 2 = liftIO $ traceStr >>= putStrLn
| iter `mod` freq == 0 && mode == 3 = liftIO $ traceStr >>= putStrLn . (clear ++)
| iter `mod` freq == 0 && mode == 4 = tracer 0 freq False >> void (renderImage' "trace" progress)
| iter `mod` freq == 0 && mode == 5 = if islandish then tracer 4 freq True else pure ()
| otherwise = pure ()
perc = fromIntegral depth / fromIntegral size * 100 :: Double
ratio = fromIntegral iter / fromIntegral depth :: Double
solvedStr = printf "\x1b[2Ksolved: %02.2f%%, ratio: %0.2f\x1b[1A" perc ratio
clear = "\x1b[H\x1b[2K" -- move cursor 1,1; clear line
traceStr = renderColorProgress (Just current) progress
{--- Model ---}
directions = [0, 1, 2, 3]
-- | > directions = rotations = [0, 1, 2, 3]
rotations = directions
charMapEntries :: [(Char, Pix)]
charMapEntries = map (_2 %~ (mirrorNibble . directionsPix))
[ ('╹', [0])
, ('┗', [0,1])
, ('┣', [0,1,2])
, ('╋', [0,1,2,3])
, ('┻', [0,1,3])
, ('┃', [0,2])
, ('┫', [0,2,3])
, ('┛', [0,3])
, ('╺', [1])
, ('┏', [1,2])
, ('┳', [1,2,3])
, ('━', [1,3])
, ('╻', [2])
, ('┓', [2,3])
, ('╸', [3])
, (' ', []) -- chars outside the map
]
where mirrorNibble = (\n -> n + Bit.shiftL n 4) :: Pix -> Pix
charMap :: Map Char Pix
charMap = Map.fromList charMapEntries
pixMap :: Map Pix Char
pixMap = Map.fromList $ map swap charMapEntries
{-# INLINE pixRotations #-}
-- | This accounts for some piece's rotational symmetry
pixRotations :: Pix -> [Rotation]
pixRotations 0b00000000 = [0]
pixRotations 0b11111111 = [0]
pixRotations 0b10101010 = [0, 1]
pixRotations 0b01010101 = [0, 1]
pixRotations _ = rotations
{-# INLINE pixDirections #-}
pixDirections :: Bit.Bits p => p -> [Direction]
pixDirections b = foldMap (\n -> [n | b `Bit.testBit` n]) [0, 1, 2, 3]
{-# INLINE pixNDirections #-}
pixNDirections :: Bit.Bits p => p -> [Direction]
pixNDirections b = foldMap (\n -> [n | not (b `Bit.testBit` n)]) [0, 1, 2, 3]
{-# INLINE directionsPix #-}
directionsPix :: Integral i => [Direction] -> i
directionsPix = getSum . foldMap (Sum . (2 ^))
toPix = (charMap !) :: Char -> Pix
toChar = (pixMap !) :: Pix -> Char
{-# INLINE rotate #-}
-- | Rotates the 'Pix' to left by n 'Rotation's
rotate :: Rotation -> Pix -> Pix
rotate = flip Bit.rotateL
{--- Solver bits: per-pixel stuff ---}
-- given current pixel at rotation, does it match the pixel at direction from it?
{-# INLINE pixValid #-}
pixValid :: (Pix, Pix, Rotation, Direction) -> Bool
pixValid (!this, !that, !rotation, !direction) =
not $ (rotate rotation this `Bit.xor` rotate 2 that) `Bit.testBit` direction
{-# INLINE validateDirection #-}
validateDirection :: Pix -> Rotation -> (Piece, Direction) -> Bool
validateDirection this rotation (Piece{pipe=that, solved}, direction) = do
not solved || pixValid (this, that, rotation, direction)
{-# INLINE validateRotation #-}
validateRotation :: Pix -> [(Piece, Direction)] -> Rotation -> Bool
validateRotation this deltas rotation = all (validateDirection this rotation) deltas
{-# INLINE validateRotationM #-}
validateRotationM :: MMaze -> Cursor -> Pix -> Rotation -> IO Bool
validateRotationM maze cursor this rotation =
(fmap (validateDirection this rotation) . mazeDeltaWall maze cursor) `allM` directions
{-# INLINE pieceChoices #-}
-- | Compute initial rotation fields for a piece's 'Choices'
pieceChoices :: MMaze -> Cursor -> IO Choices
pieceChoices maze@MMaze{width, height} cur@(x, y) = do
Piece{pipe} <- mazeRead maze (mazeFursor width cur)
if edge || length (pixRotations pipe) < 4
then do
valids <- foldMap choiceBits <$> filterM (validateRotationM maze cur pipe) (pixRotations pipe)
pure . flip Bit.shiftL choicesInvalid . Bit.xor 0b1111 . getSum $ valids
else pure (Bit.shiftL 4 choicesCount)
where
edge = (x + 1) `mod` width < 2 || ((y + 1) `mod` height < 2)
choiceBits d = Sum (Bit.bit 4 + Bit.bit d)
forceChoice :: Pix -> Pix -> Choices -> Choices
forceChoice forced pix choices =
let
rotatation = fromJust (List.find (\r -> rotate r pix == forced) rotations)
exceptSolveds = Bit.shiftL 0b1111 choicesSolveds
in
(exceptSolveds Bit..&. choices)
+ Bit.shiftL 1 choicesCount
+ Bit.shiftL (0b1111 `Bit.xor` Bit.bit rotatation) choicesInvalid
{-# INLINE forcePiece #-}
forcePiece :: Pix -> Piece -> Piece
forcePiece dst p@Piece{pipe=src} = (initChoicesL %~ forceChoice dst src) p
{-# INLINE forceContinue #-}
forceContinue :: Pix -> Continue -> Continue
forceContinue dst c@Continue{char=src} = (choicesL %~ forceChoice dst src) c
forceHints :: Continues -> Progress -> [Unwind] -> SolverT Progress
forceHints continues p@Progress{maze} = foldlM deployHint p
where
deployHint p (UnSolve c _ pix) =
if IntMap.member c continues
then toSolverT (prioritizeContinue p c (forceContinue pix . fromJust))
else p <$ mazeModify maze (forcePiece pix) c
deployHint p _ = pure p
{--- Solver bits: components ---}
{-# INLINE compInsert #-}
compInsert :: Continue -> Components -> Components
compInsert Continue{origin} (Components c) = Components (IntMap.insertWith (+) origin 1 c)
compInsert Continue{origin, cursor} (Components' c) = Components' (IntMap.insertWith IntSet.union origin (IntSet.singleton cursor) c)
{-# INLINE compRemove #-}
compRemove :: PartId -> Fursor -> Components -> Components
compRemove origin _cursor (Components c) = Components (IntMap.update (Just . subtract 1) origin c)
compRemove origin cursor (Components' c) = Components' (IntMap.update (Just . IntSet.delete cursor) origin c)
{-# INLINE compEquate #-}
compEquate :: PartId -> [PartId] -> Components -> Components
compEquate hub connections c = equate c
where
{-# INLINE equate #-}
equate (Components c) = Components $ equate' Sum getSum c
equate (Components' c) = Components' $ equate' id id c
{-# INLINE equate' #-}
equate' :: Monoid m => (a -> m) -> (m -> a) -> IntMap a -> IntMap a
equate' lift drop c = IntMap.insertWith (\a b -> drop (lift a <> lift b)) hub (drop sum) removed
where (sum, removed) = foldr (flip (extract lift)) (mempty, c) connections
{-# INLINE extract #-}
extract lift (sum, m) part = IntMap.alterF ((, Nothing) . mappend sum . foldMap lift) part m
{-# INLINE compAlive #-}
compAlive :: PartId -> Components -> Bool
compAlive k (Components c) = (Just 1 ==) $ IntMap.lookup k c
compAlive k (Components' c) = (Just 1 ==) . fmap IntSet.size $ IntMap.lookup k c
{-# INLINE compConnected #-}
compConnected :: PartId -> Components -> [Fursor]
compConnected k (Components' c) = foldMap IntSet.toList (IntMap.lookup k c)
compConnected _ _ = []
compCounts :: Components -> IntMap Int
compCounts (Components c) = c
compCounts (Components' c) = IntMap.map IntSet.size c
{--- Solver bits: continues ---}
{-# INLINE deltaContinue #-}
deltaContinue :: Continue -> Int -> Fursor -> Direction -> Piece -> Maybe Continue -> Continue
deltaContinue Continue{char, origin, island, area} id c from Piece{pipe, initChoices} prev = do
let pointed = char `Bit.testBit` from
let origin' = if pointed then origin else c
let island' = min 4 (island * 2) -- islands get bumped once to set area, next time to solve islands whole
let dir = (from + 2) `mod` 4
let initChoices' = maybe initChoices choices prev
let validRot = pixNDirections (initChoices' `Bit.shiftR` choicesInvalid)
let invalids = filter (\r -> pointed /= Bit.testBit (rotate r pipe) dir) validRot
let choices' :: Int = foldr (\d s -> s - Bit.bit choicesCount + Bit.bit (choicesInvalid + d)) initChoices' invalids
let solveds = Bit.bit (dir + choicesSolveds)
-- let require = fromEnum pointed `Bit.shiftL` (dir + choicesRequire) -- not used
Continue c pipe origin' 0 id island' area (choices' Bit..|. solveds)
{-# INLINE prioritizeDeltas #-}
-- | Calls 'prioritizeContinue' on nearby pieces (delta = 1)
prioritizeDeltas :: Width -> Progress -> Continue -> SolverT Progress
prioritizeDeltas width p@Progress{iter, maze} continue@Continue{cursor=cur, choices} = do
(toSolverT . prioritizeContinues p) <=< for (zip [0..] (pixNDirections choices)) $ \(i, d) -> do
piece <- mazeRead maze (mazeFDelta width cur d)
let delta = mazeFDelta width cur d
pure (delta, deltaContinue continue (iter * 4 + i) delta d piece)
{-# INLINE rescoreContinue #-}
-- | Recalculates the 'Continue's score, less is better (because of 'IntMap.deleteFindMin' in 'findContinue').
--
-- > score = (0 - island << 17 + (choices << (15 - choicesCount)) + x + y) << 32 + created
rescoreContinue :: Bounds -> Width -> Continue -> Continue
rescoreContinue bounds width c@Continue{cursor, choices=choicesBits, island, area, created} = set scoreL score c
where
-- score = choices + x + y -- interesting for animations for smaller levels
score = (0 + bound << 34 - island << 27 + area << 15 + (choices << (12 - choicesCount)) + x + y) << 28 + created
bound = if bounded bounds cursor then 0 else 1
choices = choicesBits Bit..&. (0b11 << choicesCount)
(<<) = Bit.shiftL
(x, y) = mazeCursor width cursor
{-# INLINE prioritizeContinue' #-}
prioritizeContinue' :: Width -> PrioCompCont -> Fursor -> (Maybe Continue -> Continue) -> Solver PrioCompCont
prioritizeContinue' width (p, cp, ct) c get =
ReaderT $ \Configuration{cBounds} -> Identity $ found cBounds (IntMap.lookup c ct)
where
found :: Bounds -> Maybe Continue -> PrioCompCont
found bounds Nothing =
let new = rescoreContinue bounds width (get Nothing)
in (IntMap.insert (score new) c p, compInsert new cp, IntMap.insert c new ct)
found bounds (Just old@Continue{cursor, created, choices=choicesO}) =
if score new < score old || choicesN /= choicesO
then (IntMap.insert (score new) cursor . IntMap.delete (score old) $ p, cp, IntMap.insert c new ct)
else (p, cp, ct)
where new@Continue{choices=choicesN} = rescoreContinue bounds width (get (Just old)) { created }
{-# INLINE prioritizeContinues #-}
-- | Inserts or reprioritizes 'Continue'
prioritizeContinues :: Progress -> [(Fursor, Maybe Continue -> Continue)] -> Solver Progress
prioritizeContinues progress@Progress{maze=MMaze{width}, priority, continues, components} reprios =
putback <$> foldlM prio (priority, components, continues) reprios
where
putback (p, cp, cn) = progress { priority = p, components = cp, continues = cn }
prio acc (c, get) = prioritizeContinue' width acc c get
{-# INLINE prioritizeContinue #-}
prioritizeContinue :: Progress -> Fursor -> (Maybe Continue -> Continue) -> Solver Progress
prioritizeContinue p = curry (prioritizeContinues p . pure)
{-# INLINE pieceDead #-}
-- | Check if 'Continue' is about to become separated from the rest of the graph.
pieceDead :: MonadIO m => MMaze -> Components -> Fursor -> Pix -> Choices -> m Bool
pieceDead _ _ _ 0b00000000 _ = pure False
pieceDead maze components cur pix choices = do
thisPart <- partEquate maze . partId =<< mazeRead maze cur
pure (compAlive thisPart components && stuck)
where stuck = 0 == ((0b1111 Bit..&. pix) Bit..&. Bit.complement (fromIntegral choices))
-- | Pops `priority` by `score`, deletes from `continues`.
{-# INLINE findContinue #-}
findContinue :: Progress -> Solver (Maybe Continue)
findContinue Progress{priority, continues} = do
ReaderT $ \Configuration{cMode, cBounds} -> Identity $ do
cursor <- mfilter (bounded cBounds) (snd <$> IntMap.lookupMin priority)
mfilter
(\Continue{choices} -> solveDeterministic cMode || (2 > Bit.shiftR choices choicesCount))
(cursor `IntMap.lookup` continues)
{-# INLINE popContinue #-}
-- | Pops next 'Continue' from queue.
popContinue :: Progress -> Progress
popContinue p@Progress{priority=pr, continues=c} = p { priority, continues = IntMap.delete cursor c }
where ((_, cursor), priority) = IntMap.deleteFindMin pr
{--- Backtracking solver ---}
-- | Solves a valid piece, mutates the maze and sets unwind.
-- Inefficient access: partEquate reads the same data as islands reads.
-- (All functions within this function are inlined)
solveContinue :: Progress -> Continue -> SolverT Progress
solveContinue
progress@Progress{maze=maze@MMaze{width}, components = components_}
continue@Continue{cursor, char, origin = origin_} = do
thisPart <- partEquate maze origin_
unwindThis <- mazeSolve maze continue
let directDeltas = map (mazeFDelta width cursor) $ pixDirections char
neighbours <- fmap (nubOrd . (thisPart :)) . traverse (partEquate maze) $ directDeltas
let origin = minimum neighbours
let components = compEquate origin (filter (/= origin) neighbours) (compRemove thisPart cursor components_)
unwindEquate <- mazeEquate maze origin neighbours
traceBoard continue . (iterL %~ (+1)) . (depthL %~ (+1))
. (spaceL . _head %~ (, unwindThis : unwindEquate) . fst)
=<< prioritizeDeltas width progress { components } continue { origin }
-- | The initial 'Progress', 'space' stack, 'Progress' and 'MMaze' backtracking operations.
-- This returns a progress with the first available 'Continue' from 'space' or Nothing.
-- If 'space' is empty, it gets popped, 'mazePop' gets called and it tries again until 'space' is empty.
backtrack :: MonadIO m => Progress -> m (Maybe (Progress, Continue))
backtrack Progress{space=[]} = pure Nothing
backtrack p@Progress{space=(([], []):space)} =
backtrack p { space }
backtrack Progress{space=(((continue, p):guesses, []):space), maze, iter} = do
pure (Just (p { maze, iter, space = (guesses, []) : space }, continue))
backtrack p@Progress{space=((guesses, unwind):space), maze} = do
traverse_ (mazePop maze) unwind
backtrack p { space = (guesses, []) : space }
-- | Solves pieces by backtracking, stops when the maze is solved or constraints met.
solve' :: Progress -> SolverT (Maybe Progress)
solve' p@Progress{depth, maze=MMaze{size}} | depth == size = pure (Just p) -- remove this to compute all solutions
solve' progress@Progress{depth, maze=maze@MMaze{size}, components} = do
Configuration{cLifespan, cMode} <- ask
guesses <- liftIO . foldMap (guesses progress) . maybeToList =<< toSolverT (findContinue progress)
guess <- backtrack . (spaceL %~ if null guesses then id else ((guesses, []) :)) =<< pure progress
guess <- pure $ guess & _Just . _1 . spaceL %~ (if solveWithHistory cMode then id else init)
progress <- traverse (uncurry (solveContinue . popContinue)) guess
unbounded <- null . join <$> toSolverT (traverse findContinue progress)
let stop = depth == size - 1 || cLifespan == 0 || unbounded
withReaderT (cLifespanL %~ subtract 1) $ next stop progress
where
next True = pure
next False = fmap join . traverse solve'
guesses :: MonadIO m => Progress -> Continue -> m [(Continue, Progress)]
guesses progress continue@Continue{cursor, char, choices} = do
let rotations = pixNDirections (Bit.shiftR choices choicesInvalid)
rotations <- filterDisconnected (map (\r -> (cursor, rotate r char, choices)) rotations)
pure (map (\(_, pipe, _) -> (set charL pipe continue, progress)) rotations)
filterDisconnected :: MonadIO m => [(Fursor, Pix, Choices)] -> m [(Fursor, Pix, Choices)]
filterDisconnected = filterM $ \(cur, pix, choices) -> do
disconnected <- pieceDead maze components cur pix choices
pure ((depth == size - 1) || not disconnected)
{--- Island computations ---}
-- | The generic /paint/ of the 'flood' fill.
type FillNext m s = MMaze -> Cursor -> Piece -> [(Piece, Direction)] -> StateT s m [Cursor]
-- | Four-way flood fill with 'FillNext' as the "paint". The initial piece is assumed to be valid FillNext.
flood :: MonadIO m => Monoid s => FillNext m s -> MMaze -> Cursor -> m (Set Cursor, s)
flood n m = flip runStateT mempty . flood' n m Set.empty . return
where
flood' :: MonadIO m => FillNext m s -> MMaze -> Set Cursor -> [Cursor] -> StateT s m (Set Cursor)
flood' _ _ visited [] = pure visited
flood' fillNext maze@MMaze{width=w} visited (cursor@(x, y):next) = do
this <- liftIO (mazeRead maze (x + y * w))
more <- fillNext maze cursor this =<< liftIO (mazeDeltasWalls maze cursor)
let next' = filter (not . flip Set.member visited) more ++ next
flood' fillNext maze (Set.insert cursor visited) next'
-- | Set low priority to all continues with island = 1.
islandize :: Progress -> SolverT Progress
islandize p@Progress{continues} = toSolverT $ do
prioritizeContinues p (map (, mapContinue) (IntSet.toList (IntMap.keysSet continues)))
where mapContinue = set areaL 999 . set islandL 1 . fromJust
islandConnectivityRefinement :: [IslandSolution] -> [IslandSolution]
islandConnectivityRefinement = POSet.lookupMax . POSet.fromList . map head . groupSortOn icComponents
{- HLINT ignore islandChoices "Redundant bang pattern" -}
-- | Computes and set 'iChoices'/'iSolutions' for the island, but also modifies maze with 'icHints' if len choices == 1.
islandChoices :: MMaze -> Progress -> Island -> SolverT Island
islandChoices _ Progress{components=Components _} _ = error "not enough info, unlikely"
islandChoices maze' p@Progress{maze, components=Components' compInit} i@Island{iBounds} = do
!(capped, solutions) <- iterateMaybeM 1000 (solution . fst) . (, []) =<< toSolverT (islandProgress p i maze')
!solutions <- pure (islandConnectivityRefinement . join . map snd $ solutions)
when capped $ liftIO (MV.unsafeCopy (board maze') (board maze)) -- this copies much more than it needs to, but rarely
pure (i & set iChoicesL (length solutions) & set iSolutionsL solutions)
where
constrain c = c { cLifespan = - 1, cBounds = Just (`IntSet.member` iBounds), cBench = True }
solution :: Progress -> SolverT (Maybe (Progress, [IslandSolution]))
solution p = withReaderT constrain (solve' p) >>= traverse (\p -> (p, ) . pure <$> islandSolution p)
islandSolution :: MonadIO m => Progress -> m IslandSolution
islandSolution Progress{components=Components _} = error "not enough info, unlikely"
islandSolution Progress{maze, components=comp@(Components' compJoin), space} = do
compEquated <- traverse (\p -> (, p) <$> partEquate maze p) $ compDiff compInit compJoin
pure (IslandSolution (compParts compEquated) (compCounts comp) (snd =<< space))
where
compDiff a b = IntSet.toList (on IntSet.difference IntMap.keysSet a b)
compParts = map (Set.fromList . uncurry (:)) . groupSort
islandProgress _ Island{iConts=[]} _ = error "impossible because iConts is result of `group'"
islandProgress p Island{iConts=(Continue{cursor}:_)} maze =
prioritizeContinue (p { maze, space = [] }) cursor (set islandL 2 . fromJust)
islands :: MonadIO m => Progress -> m [Island]
islands Progress{maze=maze@MMaze{width}, continues} = do
snd <$> foldIsland perIsland (map (mazeCursor width . cursor . snd) . IntMap.toList $ continues)
-- pure (islands, foldMap (\Island{iId, iConts = cs} -> IntMap.fromList $ (, iId) <$> map cursor cs) islands)
where
foldIsland perIsland continues =
(\acc -> foldlM acc (Set.empty, []) continues) $ \acc@(visited, _) cursor ->