Skip to content

Commit

Permalink
use internalize.
Browse files Browse the repository at this point in the history
it wasn't implemented with this purpose, but it does what we
want. though we might want to assure of this property in future.
  • Loading branch information
Javran committed Feb 12, 2024
1 parent c8e8b43 commit a055223
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 22 deletions.
11 changes: 9 additions & 2 deletions src/Javran/AdventOfCode/Misc.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,15 @@ rotateRightBy n offset xs = zs <> ys
(ys, zs) = splitAt (n - offset) xs

{-
Builds one-to-one mappings between a known set of values (typically Strings)
and Int.
Builds one-to-one mappings between a known set of distinct values
(typically Strings) and Int.
If each element of the input list is already unique, this mapping
should be order-preserving.
(TODO: quickcheck on this property)
Resulting indices are 0-based.
-}
internalize :: Ord a => [a] -> (a -> Int, Int -> a)
internalize xs = ((m M.!), (arr Arr.!))
Expand Down
24 changes: 4 additions & 20 deletions src/Javran/AdventOfCode/Y2023/Day18.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import Data.Containers.ListUtils (nubOrd)
import qualified Data.DList as DL
import qualified Data.IntMap.Strict as IM
import Data.List
import qualified Data.Map.Strict as M
import Data.List.Ordered (nubSort)
import Data.Monoid
import qualified Data.Sequence as Seq
import qualified Data.Set as S
import qualified Data.Vector as V
import Javran.AdventOfCode.GridSystem.RowThenCol.Uldr
import Javran.AdventOfCode.Misc (internalize)
import Javran.AdventOfCode.Prelude
import Linear.Affine (Point (..), origin, (.+^))
import Linear.V2 (V2 (..))
Expand Down Expand Up @@ -171,22 +171,6 @@ constructPath ps = do
"Does not go back to origin: " <> show l
pure (clockwise, corners)

{-
Establishes an bijection between a list of sorted `Ord a`
(represented by `Set`) and zero-based index.
This allows us to scaled values in one dimension down
to consecutive indices.
Note that return values are partial functions.
-}
mkOrdMap :: Ord a => S.Set a -> (a -> Int, Int -> a)
mkOrdMap xs = ((m M.!), (vec V.!))
where
vs = S.toAscList xs
vec = V.fromList vs
m = M.fromAscList (zip vs [0 ..])

{-
This type indicates that we are working with
a scaled down version of coorindates.
Expand All @@ -204,8 +188,8 @@ coordPack :: [Pt] -> (Pt -> CoordP, CoordP -> Pt)
coordPack coords = (f, g)
where
(rs0, cs0) = unzip $ fmap (\(P (V2 r c)) -> (r, c)) coords
(rF, rG) = mkOrdMap (S.fromList rs0)
(cF, cG) = mkOrdMap (S.fromList cs0)
(rF, rG) = internalize $ nubSort rs0
(cF, cG) = internalize $ nubSort cs0

f (P (V2 r c)) = CoordP (rF r, cF c)
g (CoordP (r, c)) = P (V2 (rG r) (cG c))
Expand Down

0 comments on commit a055223

Please sign in to comment.