From f604270ea9cd76381bf514b80d0aba33cadc9ec1 Mon Sep 17 00:00:00 2001 From: Sloane Perrault Date: Wed, 21 Sep 2022 09:19:52 -0400 Subject: [PATCH] fix: remove out-of-bound/Maybe from day 2 part 2 --- 2020/02/README.md | 4 ++++ 2020/02/part2.hs | 15 +++------------ 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/2020/02/README.md b/2020/02/README.md index 0746f57..f2539c0 100644 --- a/2020/02/README.md +++ b/2020/02/README.md @@ -3,3 +3,7 @@ ## Part 1 Spent some time reading up on how to define a custom instance of `Read`. Pretty simple otherwise. Surprised `countElem` isn't in Prelude. + +## Part 2 + +Off-by-one errors were called out in the puzzle but I still made them. ~Solution can be simplified if I don't need to check for out-of-bounds issues.~ I did this. diff --git a/2020/02/part2.hs b/2020/02/part2.hs index 7ffbfc9..f19bdaf 100755 --- a/2020/02/part2.hs +++ b/2020/02/part2.hs @@ -25,21 +25,12 @@ solve = show . length . filter (isValidPasswordEntry . readPasswordEntry) . line readPasswordEntry :: String -> PasswordEntry readPasswordEntry = read -(!!?) :: [a] -> Int -> Maybe a -[] !!? _ = Nothing -(x:xs) !!? 0 = Just x -(_:xs) !!? idx = xs !!? (idx - 1) - -maybeToBool (Just a) = a -maybeToBool Nothing = False - xor :: Bool -> Bool -> Bool xor l r = l /= r isValidPasswordEntry (PasswordEntry pos1 pos2 char pass) = - (maybeToBool $ fmap (==char) char1) `xor` - (maybeToBool $ fmap (==char) char2) + (charAtPos1 == char) `xor` (charAtPos2 == char) where - char1 = pass !!? (pos1 - 1) - char2 = pass !!? (pos2 - 1) + charAtPos1 = pass !! (pos1 - 1) + charAtPos2 = pass !! (pos2 - 1)