data Display = Display [String] [String] -- patterns, output
deriving (Eq, Show)
+data Segment = Seg1 | Seg2 | Seg3 | Seg4 | Seg5 | Seg6 | Seg7
+ deriving (Eq, Ord, Show, Enum, Bounded)
+
type Encoding = M.Map Char Segment
type DigitSegments = M.Map (S.Set Segment) Char
-data Segment = Seg1 | Seg2 | Seg3 | Seg4 | Seg5 | Seg6 | Seg7
- deriving (Eq, Ord, Show, Enum, Bounded)
-
+-- some constants
+segmentNames :: [Char]
+segmentNames = "abcdefg"
+
+segments :: [Segment]
+segments = [Seg1 .. Seg7]
+
+digitSegments :: DigitSegments
+digitSegments = M.fromList
+ [ (S.fromList [Seg1, Seg2, Seg3, Seg5, Seg6, Seg7], '0')
+ , (S.fromList [Seg3, Seg6], '1')
+ , (S.fromList [Seg1, Seg3, Seg4, Seg5, Seg7], '2')
+ , (S.fromList [Seg1, Seg3, Seg4, Seg6, Seg7], '3')
+ , (S.fromList [Seg2, Seg3, Seg4, Seg6], '4')
+ , (S.fromList [Seg1, Seg2, Seg4, Seg6, Seg7], '5')
+ , (S.fromList [Seg1, Seg2, Seg4, Seg5, Seg6, Seg7], '6')
+ , (S.fromList [Seg1, Seg3, Seg6], '7')
+ , (S.fromList [Seg1, Seg2, Seg3, Seg4, Seg5, Seg6, Seg7], '8')
+ , (S.fromList [Seg1, Seg2, Seg3, Seg4, Seg6, Seg7], '9')
+ ]
+
main :: IO ()
main =
decodeOneDisplay display = findCode allocation display
where allocation = allocate display
-segmentNames :: [Char]
-segmentNames = "abcdefg"
-segments :: [Segment]
-segments = [Seg1 .. Seg7]
-
-digitSegments :: DigitSegments
-digitSegments = M.fromList
- [ (S.fromList [Seg1, Seg2, Seg3, Seg5, Seg6, Seg7], '0')
- , (S.fromList [Seg3, Seg6], '1')
- , (S.fromList [Seg1, Seg3, Seg4, Seg5, Seg7], '2')
- , (S.fromList [Seg1, Seg3, Seg4, Seg6, Seg7], '3')
- , (S.fromList [Seg2, Seg3, Seg4, Seg6], '4')
- , (S.fromList [Seg1, Seg2, Seg4, Seg6, Seg7], '5')
- , (S.fromList [Seg1, Seg2, Seg4, Seg5, Seg6, Seg7], '6')
- , (S.fromList [Seg1, Seg3, Seg6], '7')
- , (S.fromList [Seg1, Seg2, Seg3, Seg4, Seg5, Seg6, Seg7], '8')
- , (S.fromList [Seg1, Seg2, Seg3, Seg4, Seg6, Seg7], '9')
- ]
allocate :: Display -> Encoding
allocate (Display examples _) = head $ validEncodings
findCode :: Encoding -> Display -> Int
findCode encoding (Display _ codes) = read $ findDigits encoding codes
-
-
-- Parse the input file
displaysP = displayP `sepBy` endOfLine