1
0
Fork 0

Trying some overlap logic in Day 1

main
Bill Ewanick 2023-12-02 13:38:22 -05:00
parent 2005e169f4
commit caf3d3b43d
1 changed files with 30 additions and 4 deletions

View File

@ -85,14 +85,40 @@ isNumP2 = flip elem (numberChars ++ numberWords)
-- Parsing for question -- Parsing for question
-- --
numberWords :: [String] letterChars :: String
numberWords = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] letterChars = ['a'..'z']
numberChars :: [String] numberChars :: [String]
numberChars = map show [0..9] numberChars = map show [0..9]
letterChars :: String numberWords :: [String]
letterChars = ['a'..'z'] numberWords = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
overlapsSpec :: [(String, String)]
overlapsSpec =
[ ("zero" , "one" )
, ("one" , "eight")
, ("two" , "one" )
, ("three", "eight")
, ("five" , "eight")
, ("seven", "nine" )
, ("eight", "two" )
, ("eight", "three")
, ("nine" , "eight")
]
overlapsDerived :: [(String, [String])]
overlapsDerived = zip numberWords $ map findOverlaps numberWords
findOverlaps :: String -> [String]
findOverlaps str = filter (\s -> last str == head s) numberWords
combineOverlaps :: (String, [String]) -> [String]
combineOverlaps (numStr, [] ) = []
combineOverlaps (numStr, over:laps) = (numStr ++ tail over) : combineOverlaps (numStr, laps)
overlaps :: [String]
overlaps = filter (/= "") $ concatMap combineOverlaps overlapsDerived
parseNumberWords :: ReadP String parseNumberWords :: ReadP String
parseNumberWords = choice $ map string numberWords parseNumberWords = choice $ map string numberWords