make day 8 more readable
This commit is contained in:
parent
8ef2996df0
commit
b130072c72
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue