Regex...
This commit is contained in:
31
app/3.hs
31
app/3.hs
@ -1,15 +1,32 @@
|
||||
{-# LANGUAGE LambdaCase #-}
|
||||
module Main where
|
||||
|
||||
import Data.Functor
|
||||
import Text.Regex.TDFA
|
||||
import Data.List.Split
|
||||
|
||||
parse = undefined
|
||||
e :: [String] -> [(Int,Int)]
|
||||
e = ((\case
|
||||
[a,b] -> (read a, read b)
|
||||
err -> error $ "Parsing failed on: " ++ show err
|
||||
) . splitWhen (== ',') . init . drop 4 <$>)
|
||||
|
||||
solve1 = undefined
|
||||
parse1 :: String -> [(Int,Int)]
|
||||
parse1 = e . getAllTextMatches . (=~ "mul\\([[:digit:]]+,[[:digit:]]+\\)")
|
||||
|
||||
solve2 = undefined
|
||||
|
||||
parse2 :: String -> [(Int,Int)]
|
||||
parse2 =
|
||||
let h [] = []
|
||||
h a = let (t,t') = span (/= "don't()") a in t ++ g t'
|
||||
g [] = []
|
||||
g a = h . dropWhile (== "do()") $ dropWhile (/= "do()") a in
|
||||
e . filter (/= "do()") . h .
|
||||
getAllTextMatches . (=~ "mul\\([[:digit:]]+,[[:digit:]]+\\)|do\\(\\)|don't\\(\\)")
|
||||
|
||||
solve :: [(Int,Int)] -> Int
|
||||
solve = foldr ((+) . uncurry (*)) 0
|
||||
|
||||
main :: IO ()
|
||||
main = readFile "inputs/3" <&> parse >>= \i ->
|
||||
print (solve1 i) >>
|
||||
print (solve2 i)
|
||||
main = readFile "inputs/3" >>= \i ->
|
||||
print (solve $ parse1 i) >>
|
||||
print (solve $ parse2 i)
|
||||
|
Reference in New Issue
Block a user