Compare commits
No commits in common. "01cdae0f7a7a5e15850f31a51d9c905054d02afe" and "7b5d01121474ad8a7306e8ce76afeda20ad89c23" have entirely different histories.
01cdae0f7a
...
7b5d011214
50
src/Annans.hs
Normal file
50
src/Annans.hs
Normal 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
|
||||||
Loading…
Reference in New Issue
Block a user