From: Mart Lubbers Date: Tue, 22 Dec 2020 15:09:28 +0000 (+0100) Subject: 22 X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;ds=sidebyside;p=aoc20.git 22 --- diff --git a/22/input b/22/input new file mode 100644 index 0000000..4046004 --- /dev/null +++ b/22/input @@ -0,0 +1,53 @@ +Player 1: +45 +10 +43 +46 +25 +36 +16 +38 +30 +15 +26 +34 +9 +2 +44 +1 +4 +40 +5 +24 +49 +3 +41 +19 +13 + +Player 2: +28 +50 +37 +20 +6 +42 +32 +47 +39 +22 +14 +7 +21 +17 +27 +8 +48 +11 +23 +12 +18 +35 +29 +33 +31 diff --git a/22/one.icl b/22/one.icl new file mode 100644 index 0000000..7182c60 --- /dev/null +++ b/22/one.icl @@ -0,0 +1,42 @@ +module one + +import StdEnv, StdDebug +import Data.Either + +input :: ([Int], [Int]) +input = + ([45,10,43,46,25,36,16,38,30,15,26,34,9,2,44,1,4,40,5,24,49,3,41,19,13] + ,[28,50,37,20,6,42,32,47,39,22,14,7,21,17,27,8,48,11,23,12,18,35,29,33,31] + ) + +//input :: ([Int], [Int]) +//input = +// ([ 9, 2, 6, 3, 1] +// ,[ 5, 8, 4, 7, 10] +// ) + +Start = (uncurry combat input, uncurry rcombat input [] []) + +combat :: [Int] [Int] -> Either Int Int +combat [] b = Right (score b) +combat a [] = Left (score a) +combat [a:as] [b:bs] + | a > b = combat (as ++ [a,b]) bs + | a < b = combat as (bs ++ [b,a]) + | otherwise = abort "draw" + +score :: [Int] -> Int +score d = sum [i*d\\d<-reverse d & i<-[1..]] + +rcombat :: [Int] [Int] [[Int]] [[Int]] -> Either Int Int +rcombat [] b _ _ = Right (score b) +rcombat a [] _ _ = Left (score a) +rcombat [a:as] [b:bs] ahist bhist + | isMember [a:as] ahist && isMember [b:bs] bhist = Left (score [a:as]) + # (ahist, bhist) = ([[a:as]:ahist], [[b:bs]:bhist]) + | a <= length as && b <= length bs + = case rcombat (take a as) (take b bs) [] [] of + Left s = rcombat (as ++ [a,b]) bs ahist bhist + Right s = rcombat as (bs ++ [b,a]) ahist bhist + | a > b = rcombat (as ++ [a,b]) bs ahist bhist + | a < b = rcombat as (bs ++ [b,a]) ahist bhist