<commit_before>module Abyme.Universe.Validate where

import Control.Lens
import Data.List (tails)

import Abyme.Universe
import Abyme.Addressing

-- TODO: return a descriptive error
-- TODO: check ids of key and value match
-- TODO: harder? check universe is connected

pairs :: [a] -> [(a, a)]
pairs l = [(x,y) | (x:ys) <- tails l, y <- ys]

validateRegion :: Universe -> Region -> Bool
validateRegion u r@(Region rid pid _ ssh) = idsUnequal && shapesDontIntersect && everythingSitsOnSomething
  where idsUnequal = rid /= pid
        shapesDontIntersect = all (not . uncurry shapeIntersects) (pairs ssh)
        everythingSitsOnSomething = all (\s -> Just s == inhabitant u (squareLocation u s)) (constituentSquares u r)

validateUniverse :: Universe -> Bool
validateUniverse u = all (validateRegion u) (u ^.. universeRegions . traverse)
<commit_msg>Validate that a region is connected<commit_after>module Abyme.Universe.Validate where

import Control.Lens
import Data.List (tails)

import Abyme.Polyomino
import Abyme.Universe
import Abyme.Addressing

-- TODO: return a descriptive error
-- TODO: check ids of key and value match
-- TODO: harder? check universe is connected

pairs :: [a] -> [(a, a)]
pairs l = [(x,y) | (x:ys) <- tails l, y <- ys]

validateRegion :: Universe -> Region -> Bool
validateRegion u r@(Region rid pid _ ssh) = idsUnequal && shapesDontIntersect && regionIsConnected && everythingSitsOnSomething
  where idsUnequal = rid /= pid
        shapesDontIntersect = all (not . uncurry shapeIntersects) (pairs ssh)
        regionIsConnected = polyIsConnected $ regionCompositePoly r
        everythingSitsOnSomething = all (\s -> Just s == inhabitant u (squareLocation u s)) (constituentSquares u r)

validateUniverse :: Universe -> Bool
validateUniverse u = all (validateRegion u) (u ^.. universeRegions . traverse)

{'fraction_non_alphanumeric': 0.10620915032679738, 'fraction_numerical': 0.0, 'mean_word_length': 4.088642659279778, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2861721', 'n_tokens_mistral': 576, 'n_tokens_neox': 554, 'n_words': 194}