mirror of
https://codeberg.org/JasterV/aoc2024-haskell.git
synced 2026-04-26 18:10:05 +00:00
make sure that Matrix.groupWith sorts the values before grouping
This commit is contained in:
parent
ed7e95b59d
commit
fda3ec56bc
1 changed files with 5 additions and 4 deletions
|
|
@ -14,8 +14,8 @@ where
|
||||||
|
|
||||||
import Data.HashMap.Lazy (HashMap)
|
import Data.HashMap.Lazy (HashMap)
|
||||||
import qualified Data.HashMap.Lazy as HashMap
|
import qualified Data.HashMap.Lazy as HashMap
|
||||||
import Data.IntMap.Lazy (IntMap)
|
|
||||||
import qualified Data.IntMap.Lazy as IntMap
|
import qualified Data.IntMap.Lazy as IntMap
|
||||||
|
import Data.List (sortOn)
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
import Prelude hiding (lookup)
|
import Prelude hiding (lookup)
|
||||||
|
|
||||||
|
|
@ -48,12 +48,13 @@ lookupMultiple positions matrix = mapMaybe (`lookup` matrix) positions
|
||||||
|
|
||||||
{--
|
{--
|
||||||
Given a matrix of elements and a function mapping a position into an aggregation of its values,
|
Given a matrix of elements and a function mapping a position into an aggregation of its values,
|
||||||
group the elements by the aggregation result
|
group the elements by the aggregation result.
|
||||||
|
The values are grouped in order.
|
||||||
--}
|
--}
|
||||||
groupWith :: forall v. ((Int, Int) -> Int) -> Matrix v -> [[v]]
|
groupWith :: forall v. ((Int, Int) -> Int) -> Matrix v -> [[v]]
|
||||||
groupWith f (Matrix hmap) =
|
groupWith f (Matrix hmap) =
|
||||||
let intMap :: IntMap [v]
|
let sortedEntries = sortOn fst $ HashMap.toList hmap
|
||||||
intMap = HashMap.foldrWithKey (insertValue . f) IntMap.empty hmap
|
intMap = foldr (\(position, value) -> insertValue (f position) value) IntMap.empty sortedEntries
|
||||||
in IntMap.elems intMap
|
in IntMap.elems intMap
|
||||||
where
|
where
|
||||||
insertValue key value =
|
insertValue key value =
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue