{-# Language QuasiQuotes #-}
module Main where
import Advent (format)
import Data.List (delete)
main :: IO ()
IO ()
main =
do [[Int]]
xs <- [format|2017 2 (%u&%t%n)*|]
Int -> IO ()
forall a. Show a => a -> IO ()
print ([Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (([Int] -> Int) -> [[Int]] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map [Int] -> Int
checksum1 [[Int]]
xs))
Int -> IO ()
forall a. Show a => a -> IO ()
print ([Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (([Int] -> Int) -> [[Int]] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map [Int] -> Int
checksum2 [[Int]]
xs))
checksum1 :: [Int] -> Int
checksum1 :: [Int] -> Int
checksum1 [Int]
xs = [Int] -> Int
forall a. Ord a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum [Int]
xs Int -> Int -> Int
forall a. Num a => a -> a -> a
- [Int] -> Int
forall a. Ord a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum [Int]
xs
checksum2 :: [Int] -> Int
checksum2 :: [Int] -> Int
checksum2 [Int]
xs =
[Int] -> Int
forall a. HasCallStack => [a] -> a
head [ Int
q | Int
x <- [Int]
xs, Int
y <- Int -> [Int] -> [Int]
forall a. Eq a => a -> [a] -> [a]
delete Int
x [Int]
xs, (Int
q,Int
0) <- [Int
x Int -> Int -> (Int, Int)
forall a. Integral a => a -> a -> (a, a)
`divMod` Int
y] ]