Skip to content

Commit

Permalink
day 5 part 1 done
Browse files Browse the repository at this point in the history
  • Loading branch information
Javran committed Jan 8, 2024
1 parent 1dc4d12 commit ef222fa
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/Javran/AdventOfCode/Y2023/Day5.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,29 @@ almanacP = do
many entryP
pure (seeds, rs)

mkMapper :: AlmanacMap -> Int -> Int
mkMapper xs = auxLookup
where
-- key: src, value: (dst, range)
m :: IM.IntMap (Int, Int)
m = IM.fromList do
(dst, src, rng) <- xs
pure (src, (dst, rng))

-- rely on split and maxView on left result to do our work
auxLookup i
| Just (dst, _) <- cur = dst
| Just ((src, (dst, rng)), _) <- IM.maxViewWithKey lm
, i < src + rng =
dst + (i - src)
| otherwise = i
where
(lm, cur, _) = IM.splitLookup i m

instance Solution Day5 where
solutionSolved _ = False
solutionRun _ SolutionContext {getInputS, answerShow} = do
xs <- consumeOrDie almanacP <$> getInputS
print xs
(seeds, maps) <- consumeOrDie almanacP <$> getInputS
let fs = fmap mkMapper maps
locs = foldl' (\xs f -> fmap f xs) seeds fs
answerShow (minimum locs)

0 comments on commit ef222fa

Please sign in to comment.