slight cleanup

This commit is contained in:
thornAvery 2021-12-06 10:29:06 +00:00
parent 9653f71512
commit 4a4041a8bd

View file

@ -2,12 +2,7 @@ module Main where
import Data.List import Data.List
main :: IO () -- this is a little gross
main = do
raw <- getLine
let input = (toSchool . collect . sort) $ map read $ words $ map (\c -> if c == ',' then ' ' else c) raw
putStrLn $ "day6a: " ++ (show $ sumSchool ((iterate transform input) !! 80))
putStrLn $ "day6b: " ++ (show $ sumSchool ((iterate transform input) !! 256))
data School = School data School = School
{ day0 :: Integer { day0 :: Integer
@ -21,6 +16,29 @@ data School = School
, day8 :: Integer , day8 :: Integer
} deriving Show } deriving Show
toSchool' :: [(Integer,Integer)] -> School -> School
toSchool' [] s = s
toSchool' ((i,n):is) s
| i == 0 = toSchool' is $ s { day0 = n }
| i == 1 = toSchool' is $ s { day1 = n }
| i == 2 = toSchool' is $ s { day2 = n }
| i == 3 = toSchool' is $ s { day3 = n }
| i == 4 = toSchool' is $ s { day4 = n }
| i == 5 = toSchool' is $ s { day5 = n }
| i == 6 = toSchool' is $ s { day6 = n }
| i == 7 = toSchool' is $ s { day7 = n }
| i == 8 = toSchool' is $ s { day8 = n }
| otherwise = error "index too large"
-- sorry i couldnt be bothered learning haskell arrays
main :: IO ()
main = do
raw <- getLine
let input = (toSchool . collect . sort) $ map read $ words $ map (\c -> if c == ',' then ' ' else c) raw
putStrLn $ "day6a: " ++ (show $ sumSchool ((iterate transform input) !! 80))
putStrLn $ "day6b: " ++ (show $ sumSchool ((iterate transform input) !! 256))
sumSchool :: School -> Integer sumSchool :: School -> Integer
sumSchool (School a b c d e f g h i) = sumSchool (School a b c d e f g h i) =
a + b + c + d + e + f + g + h + i a + b + c + d + e + f + g + h + i
@ -36,19 +54,5 @@ collect is = map f $ group is
then error "empty list" then error "empty list"
else ((head js), toInteger (length js)) else ((head js), toInteger (length js))
toSchool' :: [(Integer,Integer)] -> School -> School
toSchool' [] s = s
toSchool' ((i,n):is) s
| i == 0 = toSchool' is $ s { day0 = n }
| i == 1 = toSchool' is $ s { day1 = n }
| i == 2 = toSchool' is $ s { day2 = n }
| i == 3 = toSchool' is $ s { day3 = n }
| i == 4 = toSchool' is $ s { day4 = n }
| i == 5 = toSchool' is $ s { day5 = n }
| i == 6 = toSchool' is $ s { day6 = n }
| i == 7 = toSchool' is $ s { day7 = n }
| i == 8 = toSchool' is $ s { day8 = n }
| otherwise = error "index too large"
toSchool :: [(Integer,Integer)] -> School toSchool :: [(Integer,Integer)] -> School
toSchool is = toSchool' is (School 0 0 0 0 0 0 0 0 0) toSchool is = toSchool' is (School 0 0 0 0 0 0 0 0 0)