2BM2H55IPQLSAY6YAM3H7IIHDHR2SDA7CVDXMPLT34FPUGKGNI7AC
ERSH4NEQUKSU6CLZN6B5WUNH37MW6G3C43YI2BDJSXWSQ5Q7Q3HAC
LD3SIGINEZTGCHE7CKMGX55KNKMQ6GIFT35UNPO4YDEB32BK46TAC
KMLYRD266AY5MJ6QQ5AVADZETDJPUU26E2W6VBHSESNEO7BKJW5QC
66SF6NA6WQF5U3B3CP2QWMCAZUL5DLQI3ASE2EOKCLTEU4PXHDSQC
4QPDDW46NZLFA2GZ4YFOLEMHHP5BPHLBOTSZMOJ3UMGRQ4DH3N7QC
KHIWQ533KB3UVDZJ6SQLT5YKEJYINDQVP2VVFOGQGDCVLG6BLO4AC
CQUB2A3OVJACRIRDLZNAO6XTFJX45ZNUJQU57A3Q4O6L6BT5ULIAC
SEPLAL77TB3T7FLHDDUQVJ6HOMWHV7AULLVCKUYUOZYNA5T7WO2QC
import Data.NonEmptyList (NonEmptyList)
import qualified Data.NonEmptyList as NonEmptyList
import Data.IntMap (IntMap)
import qualified Data.IntMap as IMap
import Data.NonEmptyList (NonEmptyList)
import qualified Data.NonEmptyList as NonEmptyList
addLocation primer location [] = [IndexItem (primer, NonEmptyList.singleton location)]
addLocation primer@(Primer {_volume = vol'}) location (x@(IndexItem (Primer {_volume = vol}, _)):xs)
| vol == vol' = (x & _IndexItem . _2 %~ NonEmptyList.push location):xs
| otherwise = x:addLocation primer location xs
addLocation primer location map =
let vol = fromVolume $ primer ^. volume
f (IndexItem (p, ls)) (IndexItem (_, ls')) = IndexItem (p, NonEmptyList.merge ls ls')
in IMap.insertWith f vol (IndexItem (primer, NonEmptyList.singleton location)) map