From b130072c723837f989fb4fb71139cb4d613650c6 Mon Sep 17 00:00:00 2001 From: Quinten Kock Date: Wed, 8 Dec 2021 17:56:54 +0100 Subject: [PATCH] make day 8 more readable --- 2021/day08.hs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/2021/day08.hs b/2021/day08.hs index b5c78db..d836638 100644 --- a/2021/day08.hs +++ b/2021/day08.hs @@ -50,14 +50,14 @@ testDigit perm digit = isJust $ signalsToDigit $ map (\x -> perm !! segToNum x) testPermutation :: [Int] -> [String] -> Bool testPermutation perm = all (testDigit perm) -testPermutations :: [[Int]] -> [String] -> [Int] -testPermutations perms digits = head $ mapMaybe (\p -> toMaybe p $ testPermutation p digits) perms +selectPermutation :: [[Int]] -> [String] -> [Int] +selectPermutation perms digits = head $ filter(`testPermutation` digits) perms -decodePermutations :: [[Int]] -> Input -> Int -decodePermutations p (signals,output) = read $ concatMap show digits +decode :: [[Int]] -> Input -> Int +decode p (signals,output) = read $ concatMap show digits where digits = map (fromJust . (signalsToDigit . map (\x -> perm !! segToNum x))) output - perm = testPermutations p signals + perm = selectPermutation p signals main :: IO () main = do @@ -66,6 +66,6 @@ main = do putStrLn $ "part 1: " ++ show (sum $ map countEasy input) let perms = permutations [0..6] - let valid_perms = map (decodePermutations perms) input + let valid_perms = map (decode perms) input -- mapM_ print valid_perms putStrLn $ "part 2: " ++ show (sum valid_perms)