Compare commits

..

1 Commits

Author SHA1 Message Date
Stefan Risberg 7b5d011214 Initial release 2022-12-03 10:50:09 +01:00
+50
View File
@@ -0,0 +1,50 @@
module Annans (main') where
import qualified Data.ByteString.Lazy as BL
import Data.List (sort)
import Data.List.Split
import Data.Text.Encoding (decodeUtf8)
import Data.Text.Read (decimal)
import Prelude
largest3 :: [Int] -> [Int]
largest3 = \case
a : b : c : rest ->
let [a', b', c'] = sort [a, b, c]
in go a' b' c' rest
xs -> sort xs
where
-- a <= b <= c
go a b c [] = [a, b, c]
go !a !b !c (!x : xs)
| x >= c = go b c x xs
| x >= b = go b x c xs
| x >= a = go x b c xs
| otherwise = go a b c xs
parse :: BL.ByteString -> Int
parse =
(\case (Right (x, _)) -> x)
. decimal
. decodeUtf8
. BL.toStrict
parseAll :: BL.ByteString -> [[Int]]
parseAll =
map (map parse)
. filter (not . null)
. splitOn [BL.empty]
. BL.split 0x0a
main' :: IO ()
main' =
main'' "aoc_2022_day01_large_input.txt"
process :: BL.ByteString -> [Int]
process = largest3 . map sum . parseAll
main'' :: FilePath -> IO ()
main'' file = do
[x1, x2, x3] <- process <$> BL.readFile file
print $ x3
print $ x1 + x2 + x3