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 :: [Int] -> [String] -> Bool
|
||||||
testPermutation perm = all (testDigit perm)
|
testPermutation perm = all (testDigit perm)
|
||||||
|
|
||||||
testPermutations :: [[Int]] -> [String] -> [Int]
|
selectPermutation :: [[Int]] -> [String] -> [Int]
|
||||||
testPermutations perms digits = head $ mapMaybe (\p -> toMaybe p $ testPermutation p digits) perms
|
selectPermutation perms digits = head $ filter(`testPermutation` digits) perms
|
||||||
|
|
||||||
decodePermutations :: [[Int]] -> Input -> Int
|
decode :: [[Int]] -> Input -> Int
|
||||||
decodePermutations p (signals,output) = read $ concatMap show digits
|
decode p (signals,output) = read $ concatMap show digits
|
||||||
where
|
where
|
||||||
digits = map (fromJust . (signalsToDigit . map (\x -> perm !! segToNum x))) output
|
digits = map (fromJust . (signalsToDigit . map (\x -> perm !! segToNum x))) output
|
||||||
perm = testPermutations p signals
|
perm = selectPermutation p signals
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
|
|
@ -66,6 +66,6 @@ main = do
|
||||||
putStrLn $ "part 1: " ++ show (sum $ map countEasy input)
|
putStrLn $ "part 1: " ++ show (sum $ map countEasy input)
|
||||||
|
|
||||||
let perms = permutations [0..6]
|
let perms = permutations [0..6]
|
||||||
let valid_perms = map (decodePermutations perms) input
|
let valid_perms = map (decode perms) input
|
||||||
-- mapM_ print valid_perms
|
-- mapM_ print valid_perms
|
||||||
putStrLn $ "part 2: " ++ show (sum valid_perms)
|
putStrLn $ "part 2: " ++ show (sum valid_perms)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue