Trying some overlap logic in Day 1
parent
2005e169f4
commit
caf3d3b43d
|
@ -85,14 +85,40 @@ isNumP2 = flip elem (numberChars ++ numberWords)
|
|||
-- Parsing for question
|
||||
--
|
||||
|
||||
numberWords :: [String]
|
||||
numberWords = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
|
||||
letterChars :: String
|
||||
letterChars = ['a'..'z']
|
||||
|
||||
numberChars :: [String]
|
||||
numberChars = map show [0..9]
|
||||
|
||||
letterChars :: String
|
||||
letterChars = ['a'..'z']
|
||||
numberWords :: [String]
|
||||
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 = choice $ map string numberWords
|
||||
|
|
Loading…
Reference in New Issue