{-# LANGUAGE QuasiQuotes #-} module Day13Spec (spec) where import Day13 import Test.Hspec import Text.Heredoc import Data.List (sort, elem, elemIndex) testInput :: String testInput = [str|[1,1,3,1,1] |[1,1,5,1,1] | |[[1],[2,3,4]] |[[1],4] | |[9] |[[8,7,6]] | |[[4,4],4,4] |[[4,4],4,4,4] | |[7,7,7,7] |[7,7,7] | |[] |[3] | |[[[]]] |[[]] | |[1,[2,[3,[4,[5,6,7]]]],8,9] |[1,[2,[3,[4,[5,6,0]]]],8,9] |] falsePositive :: String falsePositive = "[[],[[[10,2,6],[1,4,9,2,5]],[],[5,2],[1,3,5]],[[[7,2,2,0],[0,1],10],10,[9,[4,8],[8,2,10],[1,4,2]],7,[[2]]]]\n" ++ "[[],[[[],[],[9,10,5,6],2,[0]],10],[[],[[4,9,3,7],7,4],[],2,2],[8,[6,8,[7,5,5],[2,4,8,0,7],[3,1,7,3,9]],[[9,1,5,1],[5,1,1],[],[2,1,2,1]],[[0,9,1,10],5,[],10,[5,0,10,2]],[10,[3,4,8,8,4],5]]]\n" spec :: Spec spec = describe "Day12" $ do describe "Part1" $ do it "parser a single pair" $ do let input = "[1,[],3]\n[]\n" show (head $ parsePackets input) `shouldBe` input it "checks order" $ do Entry 1 <= Entry 2 `shouldBe` True Entry 2 <= Entry 1 `shouldBe` False List [] <= List [] `shouldBe` True List [] <= List [Entry 1] `shouldBe` True List [Entry 1] <= List [] `shouldBe` False List [Entry 1] <= Entry 2 `shouldBe` True List [Entry 2] <= Entry 1 `shouldBe` False let pair = head $ parsePackets falsePositive checkPair pair `shouldBe` False it "checks examples" $ do let pairs = parsePackets testInput length pairs `shouldBe` 8 sumIndices pairs `shouldBe` 13 map checkPair pairs `shouldBe` [True, True, False, True, False, True, False, False] describe "Part2" $ do it "checks examples" $ do let packets = sort $ addDividers $ parsePacketList testInput let a = packets !! 9 let b = head dividers a `shouldBe` b elem (head dividers) packets `shouldBe` True findDividers packets `shouldBe` [10, 14]