day4,5: update - use parser combinators

not the cleanest, im still learning parsing in hs

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2022-12-05 23:39:03 +05:30
parent eb9ae79611
commit 38201248c4
3 changed files with 47 additions and 52 deletions

17
Lib.hs
View File

@@ -1,17 +0,0 @@
module Lib (chunks, split) where
chunks :: Int -> [a] -> [[a]]
chunks n xs
| null xs = []
| otherwise = head : chunks n tail
where (head, tail) = splitAt n xs
split :: Eq a => a -> [a] -> [[a]]
split del =
foldr
( \c (x : xs) ->
if c == del
then [] : x : xs
else (c : x) : xs
)
[[]]

45
day4.hs
View File

@@ -1,29 +1,34 @@
import Lib (split) import Data.Char (isDigit)
import Text.ParserCombinators.ReadP
( ReadP,
char,
many1,
readP_to_S,
satisfy,
)
main :: IO () main :: IO ()
main = do main = do
input <- readFile "day4.in" input <- map (fst . last . readP_to_S parse) . lines <$> readFile "day4.in"
putStr "Q1: " putStr "Q1: "
print $ q1 input print $ q1 input
putStr "Q2: " putStr "Q2: "
print $ q2 input print $ q2 input
q1 :: String -> Int parse :: ReadP ((Int, Int), (Int, Int))
q1 = parse = do
length a <- readInt
. filter char '-'
( \x -> b <- readInt
let [[a, b], [c, d]] = map (map (read :: String -> Int) . split '-') $ split ',' x char ','
in a >= c && b <= d || c >= a && d <= b c <- readInt
) char '-'
. lines d <- readInt
return ((a, b), (c, d))
where
readInt :: ReadP Int
readInt = read <$> many1 (satisfy isDigit)
q2 :: String -> Int q1, q2 :: [((Int, Int), (Int, Int))] -> Int
q2 = q1 = length . filter (\((a, b), (c, d)) -> a >= c && b <= d || c >= a && d <= b)
length q2 = length . filter (\((a, b), (c, d)) -> b >= c && a <= d)
. filter
( \x ->
let [[a, b], [c, d]] = map (map (read :: String -> Int) . split '-') $ split ',' x
in b >= c && a <= d || d >= a && c <= b
)
. lines

37
day5.hs
View File

@@ -1,20 +1,22 @@
import Lib (split) import Data.Char (isDigit)
import Data.List (transpose)
import Data.Maybe (catMaybes)
import Text.ParserCombinators.ReadP
main :: IO () main :: IO ()
main = do main = do
input <- readFile "day5.in" input <- lines <$> readFile "day5.in"
let [crates, cmds] = split "" $ lines input let (crates, cmds) = break null input
let cratesList = init $ parseCrates $ init crates let cratesList = map catMaybes $ transpose $ map (fst . last . readP_to_S parseCrates) $ init crates
let cmdsList = parseCmds cmds let cmdsList = map ((\(n, a, b) -> (n, a - 1, b - 1)) . fst . last . readP_to_S parseCmd) $ tail cmds
print cratesList
putStr "Q1: " putStr "Q1: "
print $ q1 cratesList cmdsList print $ q1 cratesList cmdsList
putStr "Q2: " putStr "Q2: "
print $ q2 cratesList cmdsList print $ q2 cratesList cmdsList
q1 :: [[Char]] -> [(Int, Int, Int)] -> [Char] q1, q2 :: [[Char]] -> [(Int, Int, Int)] -> [Char]
q1 crates cmds = map head $ foldl (\x xs -> moveCrates x xs True) crates cmds q1 crates cmds = map head $ foldl (\x xs -> moveCrates x xs True) crates cmds
q2 :: [[Char]] -> [(Int, Int, Int)] -> [Char]
q2 crates cmds = map head $ foldl (\x xs -> moveCrates x xs False) crates cmds q2 crates cmds = map head $ foldl (\x xs -> moveCrates x xs False) crates cmds
-- Computers are fast, really, not optimising right now -- Computers are fast, really, not optimising right now
@@ -26,12 +28,17 @@ moveCrates crates (n, a, b) rev =
replace :: Int -> [a] -> a -> [a] replace :: Int -> [a] -> a -> [a]
replace i xs x = take i xs ++ [x] ++ drop (i + 1) xs replace i xs x = take i xs ++ [x] ++ drop (i + 1) xs
parseCrates :: [[Char]] -> [[Char]] parseCrates :: ReadP [Maybe Char]
parseCrates ([] : _) = [[]] parseCrates = sepBy parseRow (char ' ')
parseCrates crates = parseCrate crates : parseCrates (map (drop 4) crates)
where where
parseCrate :: [[Char]] -> [Char] parseRow :: ReadP (Maybe Char)
parseCrate = foldr (\(_ : x : _ : _) xs -> if x /= ' ' then x : xs else xs) [] parseRow = (Just <$> (char '[' *> get <* char ']')) +++ (Nothing <$ string " ")
parseCmds :: [[Char]] -> [(Int, Int, Int)] parseCmd :: ReadP (Int, Int, Int)
parseCmds = foldr (\x xs -> let y = split ' ' x in (read $ y !! 1, read (y !! 3) - 1, read (y !! 5) - 1) : xs) [] parseCmd =
(,,) <$> (string "move " *> getInt)
<*> (string " from " *> getInt)
<*> (string " to " *> getInt)
where
getInt :: ReadP Int
getInt = read <$> many1 (satisfy isDigit)