{-# Language QuasiQuotes #-}
module Main where
import Advent.Format (format)
import Data.List (sort)
main :: IO ()
IO ()
main =
do [Int]
input <- [format|2020 1 (%u%n)*|]
Int -> IO ()
forall a. Show a => a -> IO ()
print ([Int] -> Int -> Int
solve [Int]
input Int
2)
Int -> IO ()
forall a. Show a => a -> IO ()
print ([Int] -> Int -> Int
solve [Int]
input Int
3)
solve ::
[Int] ->
Int ->
Int
solve :: [Int] -> Int -> Int
solve [Int]
input Int
n = [Int] -> Int -> Int -> Int -> Int -> Int
solve' ([Int] -> [Int]
forall a. Ord a => [a] -> [a]
sort [Int]
input) Int
n Int
2020 Int
1 (String -> Int
forall a. HasCallStack => String -> a
error String
"no solution")
solve' ::
[Int] ->
Int ->
Int ->
Int ->
Int ->
Int
solve' :: [Int] -> Int -> Int -> Int -> Int -> Int
solve' [Int]
_ Int
0 Int
0 Int
p Int
_ = Int
p
solve' (Int
x:[Int]
xs) Int
n Int
s Int
p Int
e | Int
0 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n, Int
x Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
s = [Int] -> Int -> Int -> Int -> Int -> Int
solve' [Int]
xs (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) (Int
sInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
x) (Int
pInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
x) ([Int] -> Int -> Int -> Int -> Int -> Int
solve' [Int]
xs Int
n Int
s Int
p Int
e)
solve' [Int]
_ Int
_ Int
_ Int
_ Int
e = Int
e