incremental

This commit is contained in:
Maxime Vorwerk
2024-09-20 11:11:15 +01:00
parent 3412f92a0b
commit 8d4128b85b
6 changed files with 55 additions and 40 deletions

View File

@@ -1,11 +1,11 @@
module Main (main) where
import PolyCube
import Children
import Text.Read (readMaybe)
import System.Exit ( ExitCode(ExitFailure), exitWith )
import System.Environment (getArgs)
import qualified Data.HashMap.Strict as Map
import Combinatorics
main :: IO ()
main = do
@@ -34,41 +34,7 @@ evaluateChildren :: Int -> PolyCube -> Map.HashMap [Offset3D] (Int, [Coord3D]) -
evaluateChildren maxSize polycube = loop (getChildren (maxSize - size polycube) polycube) where
loop :: [PolyCube] -> Map.HashMap [Offset3D] (Int, [Coord3D]) -> Map.HashMap [Offset3D] (Int, [Coord3D])
loop [] d = d
loop ((PolyCube o la s):pcs) d = if any (flip Map.member d . getOffsets) o
loop ((PolyCube o la s):pcs) d = if any (\x -> Map.member (getOffsets x) d) o
then loop pcs d
else loop pcs $ evaluateChildren maxSize (PolyCube o la s) $ Map.insert (getOffsets $ head o) (s, head o) d
getChildren :: Int -> PolyCube -> [PolyCube]
getChildren 0 _ = []
getChildren i (PolyCube o la s) = foldr condGrowth [] $ foldr (\x acc -> tuples x possibleGrowth ++ acc) [] [1..i] where
condGrowth :: [Coord3D] -> [PolyCube] -> [PolyCube]
condGrowth gs ps = if any (\x -> elem x $ head o) gs
then ps
else PolyCube (zipWith (\x n -> insertAllSorted x $ rotateCubes gs n) o [1..24]) gs (s + length gs) : ps
transformations :: [Offset3D]
transformations = [
Offset3D (-1, 0, 0),
Offset3D (0, -1, 0),
Offset3D (0, 0, -1),
Offset3D (1, 0, 0),
Offset3D (0, 1, 0),
Offset3D (0, 0, 1)]
applyTransformations :: Coord3D -> [Offset3D] -> [Coord3D]
applyTransformations _ [] = []
applyTransformations p (x:xs) = applyOffset p x:applyTransformations p xs
possibleGrowth :: [Coord3D]
possibleGrowth = foldr (\x acc -> insertAllSorted acc (applyTransformations x transformations)) [] la
insertAllSorted :: (Ord a) => [a] -> [a] -> [a]
insertAllSorted [] [] = []
insertAllSorted [] [v] = [v]
insertAllSorted xs [] = xs
insertAllSorted xs (y:ys) = insertAllSorted (insertSorted xs y) ys
insertSorted :: (Ord a) => [a] -> a -> [a]
insertSorted [] v = [v]
insertSorted (x:xs) v
| v < x = v:x:xs
| v > x = x:insertSorted xs v
| otherwise = x:xs