From 7d34785a93486d2a481965a57f2a783d2795a52e Mon Sep 17 00:00:00 2001 From: eriedaberrie Date: Tue, 12 Dec 2023 20:25:25 -0800 Subject: [PATCH] 2023: reformat Haskell --- 2023/1/Main.hs | 22 ++++++++++++---------- 2023/12/Main1.hs | 14 +++++++------- 2023/7/Main.hs | 27 +++++++++++++++++++-------- 2023/8/Main1.hs | 6 +++--- 2023/8/Main2.hs | 9 +++++---- 2023/9/Main1.hs | 2 +- 2023/9/Main2.hs | 2 +- 7 files changed, 48 insertions(+), 34 deletions(-) diff --git a/2023/1/Main.hs b/2023/1/Main.hs index 6867d28..5dcaf7a 100644 --- a/2023/1/Main.hs +++ b/2023/1/Main.hs @@ -1,8 +1,10 @@ -import Data.Char (isDigit, digitToInt) -import Data.Function (on) -import Data.List (tails, findIndex, isPrefixOf, minimumBy) -import Data.Maybe (mapMaybe) -import Data.Bifunctor (first, bimap) +{-# LANGUAGE TupleSections #-} + +import Data.Char (isDigit, digitToInt) +import Data.Function (on) +import Data.List (tails, findIndex, isPrefixOf, minimumBy) +import Data.Maybe (mapMaybe) +import Data.Bifunctor (first, bimap) type LineOp = String -> Int @@ -15,19 +17,19 @@ wordMap = [ ("one", 1) , ("six", 6) , ("seven", 7) , ("eight", 8) - , ("nine", 9) - ] + , ("nine", 9)] fullWordMap :: [(String, Int)] -fullWordMap = wordMap ++ (zip =<< map show) [1..9] +fullWordMap = wordMap ++ (zip =<< map show) [1 .. 9] opLineBasic :: LineOp -opLineBasic l = (combineDigits `on` ($ digitToInt <$> filter isDigit l)) head last +opLineBasic l = + (combineDigits `on` ($ digitToInt <$> filter isDigit l)) head last firstNumInLine :: String -> [(String, Int)] -> Int firstNumInLine l = fst . minimumBy (compare `on` snd) . mapMaybe numIndex where - numIndex (str, num) = (num,) <$> findIndex (isPrefixOf str) (tails l) + numIndex (str, num) = (num, ) <$> findIndex (isPrefixOf str) (tails l) opLineAdvanced :: LineOp opLineAdvanced l = (combineDigits `on` uncurry firstNumInLine) diff --git a/2023/12/Main1.hs b/2023/12/Main1.hs index 7f16756..186564b 100644 --- a/2023/12/Main1.hs +++ b/2023/12/Main1.hs @@ -1,6 +1,6 @@ -import Control.Applicative (liftA2) -import Data.List (group) -import Data.List.Split (splitOn) +import Control.Applicative (liftA2) +import Data.List (group) +import Data.List.Split (splitOn) getConsecutives :: String -> [Int] getConsecutives = map length . filter ((== '#') . head) . group @@ -8,9 +8,8 @@ getConsecutives = map length . filter ((== '#') . head) . group allSprings :: String -> [String] allSprings = foldr (liftA2 (:) . getChars) [""] where - getChars c - | c == '?' = ['.', '#'] - | otherwise = [c] + getChars '?' = ['.', '#'] + getChars c = [c] solve :: [Int] -> String -> Int solve records = length . filter ((== records) . getConsecutives) . allSprings @@ -21,4 +20,5 @@ parseLine l = (map read . splitOn "," $ y, x) (x:y:_) = splitOn " " l main :: IO () -main = print . sum . map (uncurry solve . parseLine) . lines =<< readFile "data.txt" +main = print . sum . map (uncurry solve . parseLine) . lines + =<< readFile "data.txt" diff --git a/2023/7/Main.hs b/2023/7/Main.hs index 3a5746c..4330ab6 100644 --- a/2023/7/Main.hs +++ b/2023/7/Main.hs @@ -1,26 +1,37 @@ -import Data.Bifunctor (second) -import Data.List (group, sort, sortOn) +import Data.Bifunctor (second) +import Data.List (group, sort, sortOn) import qualified Data.HashMap.Strict as HMS cardOrder :: HMS.HashMap Char Int -cardOrder = HMS.fromList $ zip "AKQT98765432J" [0..] +cardOrder = HMS.fromList . zip "AKQT98765432J" $ [0 ..] getWorth :: String -> [Int] -getWorth cards = handType : map (cardOrder HMS.!) cards +getWorth cards = handType:map (cardOrder HMS.!) cards where noJs = filter (/= 'J') cards + jCount = 5 - length noJs - groupedCards = map length $ group $ sort noJs + + groupedCards = map length . group . sort $ noJs + handType = case length groupedCards of 0 -> 1 1 -> 1 - 2 -> if minimum groupedCards == 1 then 2 else 3 - 3 -> if maximum groupedCards + jCount == 3 then 4 else 5 + 2 -> if minimum groupedCards == 1 + then 2 + else 3 + 3 -> if maximum groupedCards + jCount == 3 + then 4 + else 5 4 -> 6 5 -> 7 solve :: [String] -> Int -solve = sum . zipWith (*) [1..] . map snd . sortOn (map negate . getWorth . fst) . map parseLine +solve = sum + . zipWith (*) [1 ..] + . map snd + . sortOn (map negate . getWorth . fst) + . map parseLine where parseLine = second read . splitAt 5 diff --git a/2023/8/Main1.hs b/2023/8/Main1.hs index d1ff320..adaaf9b 100644 --- a/2023/8/Main1.hs +++ b/2023/8/Main1.hs @@ -9,14 +9,14 @@ getDirection 'R' = snd countTimes :: String -> DataMap -> Int countTimes directions m = countTimes' directions "AAA" 0 where - countTimes' (d:ds) s - | s == "ZZZ" = id - | otherwise = countTimes' ds (getDirection d $ m HMS.! s) . succ + countTimes' _ "ZZZ" = id + countTimes' (d:ds) s = countTimes' ds (getDirection d $ m HMS.! s) . succ getDataMap :: [String] -> DataMap getDataMap = HMS.fromList . map parseLine where get3At x = take 3 . drop x + parseLine l = (get3At 0 l, (get3At 7 l, get3At 12 l)) main :: IO () diff --git a/2023/8/Main2.hs b/2023/8/Main2.hs index dcde066..d3959ef 100644 --- a/2023/8/Main2.hs +++ b/2023/8/Main2.hs @@ -1,5 +1,5 @@ +import Data.List (foldl1') import qualified Data.HashMap.Strict as HMS -import Data.List (foldl1') type DataMap = HMS.HashMap String (String, String) @@ -10,9 +10,9 @@ getDirection 'R' = snd countTimes :: String -> DataMap -> String -> Int countTimes directions m start = countTimes' directions start 0 where - countTimes' (d:ds) ss@(s:_) - | s == 'Z' = id - | otherwise = countTimes' ds (getDirection d $ m HMS.! ss) . succ + countTimes' _ ('Z':_) = id + countTimes' (d:ds) ss@(s:_) = countTimes' ds (getDirection d $ m HMS.! ss) + . succ solve :: String -> DataMap -> Int solve directions m = foldl1' lcm @@ -25,6 +25,7 @@ getDataMap :: [String] -> DataMap getDataMap = HMS.fromList . map parseLine where get3At x = reverse . take 3 . drop x + parseLine l = (get3At 0 l, (get3At 7 l, get3At 12 l)) main :: IO () diff --git a/2023/9/Main1.hs b/2023/9/Main1.hs index de0e191..70dbea6 100644 --- a/2023/9/Main1.hs +++ b/2023/9/Main1.hs @@ -1,4 +1,4 @@ -import Data.List.Split (splitOn) +import Data.List.Split (splitOn) solve :: [Int] -> Int solve [] = 0 diff --git a/2023/9/Main2.hs b/2023/9/Main2.hs index 08cd166..96c6d9e 100644 --- a/2023/9/Main2.hs +++ b/2023/9/Main2.hs @@ -1,4 +1,4 @@ -import Data.List.Split (splitOn) +import Data.List.Split (splitOn) solve :: [Int] -> Int solve [] = 0