Overengineered Day3

This commit is contained in:
Martin Potier 2022-12-14 16:59:06 +02:00
parent 6807ab3133
commit 2f85311df3
No known key found for this signature in database
GPG key ID: D4DD957DBA4AD89E
5 changed files with 381 additions and 0 deletions

View file

@ -14,6 +14,7 @@ library
exposed-modules:
AoC.Day1
AoC.Day2
AoC.Day3
other-modules:
Paths_adventofcode
hs-source-dirs:
@ -22,6 +23,8 @@ library
build-depends:
attoparsec
, base ==4.*
, extra
, split
, text
default-language: Haskell2010
@ -36,5 +39,7 @@ executable adventofcode
adventofcode
, attoparsec
, base ==4.*
, extra
, split
, text
default-language: Haskell2010

View file

@ -2,6 +2,7 @@
import AoC.Day1 as Day1
import AoC.Day2 as Day2
import AoC.Day3 as Day3
import qualified Data.Text.IO as T (readFile)
@ -17,3 +18,10 @@ main = do
putStrLn $ show $ Day2.solveA input2
putStrLn $ show $ Day2.solveB "A Y\nB X\nC Z"
putStrLn $ show $ Day2.solveB input2
input3 <- T.readFile "./input/day3.txt"
let demoInput3 = "vJrwpWtwJgWrhcsFMMfFFhFp\njqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL\nPmmdzqPrVvPwwTWBwg\nwMqvLMZHhHMvwLHjbvcjnnSBnvTQFn\nttgJtRGJQctTZtZT\nCrZsJsPPZsGzwwsLwLmpwMDw"
putStrLn $ show $ Day3.solveA demoInput3
putStrLn $ show $ Day3.solveA input3
putStrLn $ show $ Day3.solveB demoInput3
putStrLn $ show $ Day3.solveB input3

300
input/day3.txt Normal file
View file

@ -0,0 +1,300 @@
qFdBBvtHHfvRlfvsqldvqjPpQmnQmjnjjjTRTLGRNG
ZCWhhCsJCzSJzSbzgsmPTGNNPPNGjgLTLjgn
WJZsbJMwJcszJcScwhVltFwBFBlqddvFdHDfqq
crtTsGTtqFThGQGCrsjTwdNJwpRdnJJwffRClpSf
PWVBPVHLvHHVgvZWBzmPpnfRSJJRQnSRflRPSNSl
gmzBzDgzmZQWLDLLgVmDrqGhsscrqDMGhcqtqcFr
Hsbcd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wjpGfpmrZfrFprrRGpwZfp
HmdNWCmDMVvQPDgqJs
GGFtSngQLfnSnQffgPnRgFRGRwmRJvwbBbJDwjvTbjrwhJvJ
WHClslcNNWcqNWlCZdcHsVrThBwBjbhDTDBhrvDZJTwm
NWVqqcHHNpsNcNVdVlhCMlHQQMQQzLfzQPttFGPMLSLgtF

View file

@ -6,12 +6,15 @@ dependencies:
- base == 4.*
- text
- attoparsec
- split
- extra
library:
source-dirs: src
exposed-modules:
- AoC.Day1
- AoC.Day2
- AoC.Day3
executable:
source-dirs: executable

65
src/AoC/Day3.hs Normal file
View file

@ -0,0 +1,65 @@
{-# LANGUAGE OverloadedStrings #-}
module AoC.Day3 where
import Data.Text (Text)
import Data.Maybe (fromMaybe)
import Data.Monoid (Sum(..), getSum)
import Data.List.Split (chunksOf)
import Data.Tuple.Extra (uncurry3)
import qualified Data.Text as T
valueMap :: [(Char, Int)]
valueMap = zip (['a'..'z']++['A'..'Z']) [1..]
breakInTwoEqualLengthSubStrings :: Text -> (Text,Text)
breakInTwoEqualLengthSubStrings txt = T.splitAt (T.length txt `div` 2) txt
findCommonItem :: Text -> Text -> Char
findCommonItem _ "" = undefined
findCommonItem compartment1 compartment2 =
if (c `T.elem` compartment1)
then c
else findCommonItem compartment1 (T.tail compartment2)
where
c = T.head compartment2
solveAOnOne :: Text -> Int
solveAOnOne = fromMaybe undefined
. flip lookup valueMap
. uncurry findCommonItem
. breakInTwoEqualLengthSubStrings
solveA :: Text -> Int
solveA = getSum . mconcat . map (Sum . solveAOnOne) . T.lines
--------------------------------------------------------------------------------
-- Part 2
findCommonItem3 :: Text -> Text -> Text -> Char
findCommonItem3 c1 c2 c3 = T.head $ go (go c1 c2) c3
where
go :: Text -> Text -> Text
go t1 = T.foldl (\acc c -> if c `T.elem` t1 then T.cons c acc else acc) ""
tupleMeUp3 :: [[Text]] -> [(Text,Text,Text)]
tupleMeUp3 = go []
where
go :: [(Text,Text,Text)] -> [[Text]] -> [(Text,Text,Text)]
go tuples [] = tuples
go tuples ([t1, t2, t3]:xs) = go ((t1,t2,t3):tuples) xs
go _ _ = undefined
solveBOnOne :: (Text,Text,Text) -> Int
solveBOnOne = fromMaybe undefined
. flip lookup valueMap
. uncurry3 findCommonItem3
solveB :: Text -> Int
solveB = getSum
. mconcat
. map (Sum . solveBOnOne)
. tupleMeUp3
. chunksOf 3
. T.lines