Commit b1be6772 by Stuart Kurtz

Day 8 complete

parent e2d69a60
Showing with 37 additions and 33 deletions
......@@ -83,6 +83,7 @@ executable aoc-08a
executable aoc-08b
import : aoc-common
main-is : AOC-08b.hs
build-depends : containers
executable aoc-09a
import : aoc-common
......
module Main where
import Data.List ((\\))
-- import Data.Foldable (for_)
-- import Text.Printf
import Data.List ((\\),sort)
import qualified Data.Map as M
import Data.Map ((!))
import Data.Tuple (swap)
data Segment = A | B | C | D | E | F | G
deriving (Eq,Show)
deriving (Eq,Ord,Show)
type Code = [Segment]
repAssocs :: [(Char,Segment)]
......@@ -28,7 +29,7 @@ codeAssocs = fmap fromRep <$>
, (6,"abdefg")
, (7,"acf")
, (8,"abcdefg")
, (9,"abcefg")
, (9,"abcdfg")
]
charCode :: Int -> Code
......@@ -43,37 +44,39 @@ readNote :: String -> Note
readNote str = Note (fromRep <$> take 10 ws) (fromRep <$> drop 11 ws) where
ws = words str
score :: Note -> Int
score (Note _ out) = length . filter isUniqueCode $ out
isUniqueCode :: Code -> Bool
isUniqueCode code = length code `elem` map codeLength [1,4,7,8] where
codeLength = length . charCode
oneCode :: [Code] -> Code
oneCode = head . filter ((==2) . length)
fourCode :: [Code] -> Code
fourCode = head . filter ((==4) . length)
sevenCode :: [Code] -> Code
sevenCode = head . filter ((==3) . length)
eightCode :: [Code] -> Code
eightCode = head . filter ((==7) . length)
descramble:: [Code] -> [Segment]
descramble cs = [a] where
one = oneCode cs
four = fourCode cs
seven = sevenCode cs
eight = eightCode cs
a = head $ seven \\ one
descramble cs = a ++ b ++ c ++ d ++ e ++ f ++ g where
getLength n = head . filter ((==n) . length)
abcdefg = [A,B,C,D,E,F,G] -- '8'
compliment = (abcdefg \\)
cf = getLength 2 cs -- '1'
acf = getLength 3 cs-- '7'
bcdf = getLength 4 cs -- '4'
a = acf \\ cf
eg = (compliment a) \\ bcdf
bd = bcdf \\ cf
[six1,six2,six3] = filter ((==6) . length) cs
cde = compliment six1 ++ compliment six2 ++ compliment six3
b = bd \\ cde
d = bd \\ b
g = eg \\ cde
e = eg \\ g
c = (cde \\ e) \\ d
f = cf \\ c
decode :: [Code] -> [Segment] -> Int
decode obs = (intMap !) . sort . (map (descrambleMap !)) where
descrambleMap = M.fromList $ zip (descramble obs) [A,B,C,D,E,F,G]
intMap = M.fromList . map swap $ codeAssocs
processNote :: Note -> Int
processNote note = unRep digits where
decoder = decode (observations note)
digits = map decoder (output note)
unRep ds = sum $ zipWith (*) (iterate (*10) 1) (reverse ds)
main :: IO ()
main = do
notes <- map readNote . lines <$> readFile "data/seven-segment.txt"
print . sum . map score $ notes
print . sum . map processNote $ notes
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment