From: Neil Smith Date: Wed, 8 Dec 2021 14:27:08 +0000 (+0000) Subject: Todying X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=3261b86eb5cefb9512475ee19d6ef216905a7d6a;p=advent-of-code-21.git Todying --- diff --git a/advent08/Main.hs b/advent08/Main.hs index 6e5c419..55a5f14 100644 --- a/advent08/Main.hs +++ b/advent08/Main.hs @@ -14,12 +14,33 @@ import qualified Data.Set as S 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 = @@ -46,25 +67,7 @@ decodeOneDisplay :: Display -> Int 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 @@ -90,8 +93,6 @@ findDigits encoding codes = map (findDigit encoding) codes findCode :: Encoding -> Display -> Int findCode encoding (Display _ codes) = read $ findDigits encoding codes - - -- Parse the input file displaysP = displayP `sepBy` endOfLine