Avoid unsafe list indexing

This commit is contained in:
George Thomas 2025-12-09 00:02:54 +00:00
parent ea64a5af3e
commit 1e3ed16971
2 changed files with 12 additions and 2 deletions

View File

@ -36,6 +36,7 @@ module Pre (
Puzzle (..),
digit,
digitsToInt,
listIndex,
)
where
@ -49,6 +50,7 @@ import "base" Prelude as BasePrelude hiding (
minimum,
tail,
unzip,
(!!),
)
import Control.Applicative
@ -96,3 +98,11 @@ digit = fromIntegral . digitToInt <$> digitChar
digitsToInt :: (Integral a) => [a] -> Int
digitsToInt = snd . foldr (\b (p, acc) -> (10 * p, acc + fromIntegral b * p)) (1, 0)
listIndex :: Int -> [a] -> Maybe a
listIndex n =
if n < 0
then const Nothing
else \case
[] -> Nothing
x : xs -> if n == 0 then Just x else listIndex (n - 1) xs

View File

@ -23,8 +23,8 @@ puzzle =
. sortOn Down
. map length
. DS.toLists
. snd
. (!! n)
. maybe (error "not enough boxes") snd
. listIndex n
. connectBoxes
, uncurry . const $
TL.show