diff --git a/aoc2022.cabal b/aoc2022.cabal index d323620..141c4cc 100644 --- a/aoc2022.cabal +++ b/aoc2022.cabal @@ -183,4 +183,17 @@ test-suite day05aoctest , HUnit , parsec ghc-options: -threaded -rtsopts -with-rtsopts=-N + default-language: Haskell2010 + +-- Day 6 + +test-suite day06aoctest + type: exitcode-stdio-1.0 + hs-source-dirs: src/day06/test, src/day06 + main-is: AoCTest.hs + other-modules: + Day6Lib + build-depends: base + , HUnit + ghc-options: -threaded -rtsopts -with-rtsopts=-N default-language: Haskell2010 \ No newline at end of file diff --git a/data/input0601.txt b/data/input0601.txt new file mode 100644 index 0000000..5a2b0a7 --- /dev/null +++ b/data/input0601.txt @@ -0,0 +1 @@ +mjqjpqmgbljsphdztnvjfqwrcgsmlb \ No newline at end of file diff --git a/data/input0602.txt b/data/input0602.txt new file mode 100644 index 0000000..9e3311c --- /dev/null +++ b/data/input0602.txt @@ -0,0 +1 @@ +bvwbjplbgvbhsrlpgdmjqwftvncz \ No newline at end of file diff --git a/data/input0603.txt b/data/input0603.txt new file mode 100644 index 0000000..cca46f5 --- /dev/null +++ b/data/input0603.txt @@ -0,0 +1 @@ +nppdvjthqldpwncqszvftbrmjlhg \ No newline at end of file diff --git a/data/input0604.txt b/data/input0604.txt new file mode 100644 index 0000000..848af1c --- /dev/null +++ b/data/input0604.txt @@ -0,0 +1 @@ +nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg \ No newline at end of file diff --git a/data/input0605.txt b/data/input0605.txt new file mode 100644 index 0000000..a4fc5ba --- /dev/null +++ b/data/input0605.txt @@ -0,0 +1 @@ +zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw \ No newline at end of file diff --git a/data/input061.txt b/data/input061.txt new file mode 100644 index 0000000..250beaa --- /dev/null +++ b/data/input061.txt @@ -0,0 +1 @@ +lrdrgrcggjrgrddfqfqrqppptrtssnqqqlzqqslspppsddfnnwgnnwhhjttnsnswnnbqbrrcqrqdqcdddlnddwwlmmlmtmwmggjgjmmmrppqhhswhwhmhchtchhsttvbtvvpspjjqrrgqrqmqvmvzmvzzrnnqlnlwlttjstjtjvjddrzzrnrmrbmbwwzppnqpqmmjnnvqnvvdzdpdvvmhvhhmshmsssvnnqppclcqlcqctqtfffvrfvrffptfftppncncwnntfntftdftfhfzzfwzzcddngdndccrttjmmcddrvrwwnntggghccjbjvbvjvlvjlvlgljlnjjtvjjfddmwwhnnqnrnccwllpmprmmspsvsrsslstsllpmllfzlflqltlssdbbdsdqsdqqgfgfjjtzjzmmsnsmnnzssgfgzgjgwwqfqtfthhhczzpwprwwqttqcqctcpcrrvwrvrggfvfqvvbqqhnnczcjjjfbfhbhjhssnwwfswszwwgbgzgczcbbvmvrrdgrddfwdffvsvtstztftfvfssgffnrnnzzqddltdtbdttnnfrnnfrfddpmpqqplqqbdbzbnnnrttjfttprtrllqhhwjhjghjjcrcqqznztzllldjjhqqspsfpfprrsfspsrrzbbjmjssnjnsjnjllbttgrtrnnqhhvjhvvrrplrprtrtprtrdtdfttwdttqtttwvtvtmtntnnfvfsfnndsnnnfqfmmcdmdppsmpmnnwhhlwwrvwrwvwlwnwhwjwvwbvwvnnqtnnttcgtccbggzgwzwczcpzcpzpzgzngnzzhfhdhzzzsggtssddgtdtndtdllpbpjpgjjsllvqvmmvjmvjvbbfzzbdzzghhzjzvvnffwsshqhqhjhmmtgmgpmgmpmzzzhtzzbzsbzsbzszvsvgsvspvsstwtgwgqgjggsfsmffhlhmhdmhmgmrmffjcjggqgsghgttjffprpmmscsggswssgzzsqzqjqwqjwqwswjjqpjpllwrwffvfbfpbffjsswgsshjsjqssqrrvcrrnvvdnnwndnffpdffjlffrjrlrqrmmlzlttpccgchggdjjhwwvqvvrprdrttlvvtlvtlvvvhqhwhmmpmrrjsrrzbzfzdzhdzzwgwwqllmmthmmvpprqprrzmrrfrmfrrmfmjmwwhtwhttnbbhbnnhbnnmhmgmjgmgfmmhpplwplwlswwjjbpbhpbbzmmgjmmggbzbssppjtttfmfwwcwmmbmrmbrblbmmszzvgvzgvgvdggjgsjgsjgghfgfngffsnfssdpdgpgrgnrggrlggdwgdgddfwddjdrrppnnqbnntmmrmlmrrgvvqjjwzzgnzzdtzzbccnbcbttbzzcqqrcqqpspbphbphhnpnccggczcgcpgcpctppqwpwccsgsttgtbttvftfcfbfvfppfpnpffmjmljmmrtrsrttnhhcvctclttrffhzhrzzdpzzwvwddcggpbptbpbzpzdpzddbjjdvvwmmfvfcfmfmmvrmvrmmcdmdqqhjqhqmmstmmdnnvssmbsmmnhmnmlmttlthtvhvvgbvgvbvqbvbvnbvvpwwhwsszpszppjljsllzhzcchmhdhphvvtpvvfssvmvcmmrjmmgfmfwmfmsspbbpnnsbsgbbzzllzzsqqswqqsbqqlvqlqzzbrzzqsqdqjqzqlqccqbqlblpprrthrttjzzfvzzpvpbppqjpjjtllmrmzzwnnrggpfpllwhhnmnpmmdppwnnwpnnzlzggvpvgpprdrzddlmddjndjjrljlsjshhqbbhhmhvhqhzqqlbqlqnqqmhmwmhwwgbwbpbhpbblnnshnshslllrcrncctnthtllmglmlmnmfnfllrbrmdqsncjfbjnghmvpctfzttqwsfptwhfhmdhzgbmrpqvsbbwdwdnqgclrgvdbqvtnzvlzmdjzgbhbtqjhrgqqjgwcvpqhhwsmmflgsjsvbnjcjcqwqqfcbvfrllbpphfglptjfczmnfnrmqdgrhbvrddwvhnbffsnzntvldrwmgqmqdbmmpdjhpdqfsbzsfbtdzlfjwtwfpfrhzcwwrcfpdjzjzqvcnhvdvlbdbjfrnmchdjprbfpzdtmjhdjtrwfrsghngbswhfzdfvhdmnqwnpspdjgsfcjjhrlnpncfdhzfzglgvrjlzdcncbfjhpnrvwnqbzhtdfcnpnpddsnjcbgdvzlsmpnlbftgjfsjsslljppjhtjfddmwbtrvmhvshnvhhfdsqhpmgzfrcqtpdmfcwglnbjzmtmzshzsrnbzlwlzdjzzsqfmjzbnprjdzswwcgjjwslhnfhbflvcpdfzwhzzjmfvpfhzbrcdvtlmbvrqvvtlpbdnshtsvlcnlwqzcnfhzndfjbhgwpvspdvhfptnfvwznscmgthspwpjfwlbbcrzgthrhnvscvfvwrwvtzpjqmwdbjjslvzlmqstzwqtsdsqqjpqthgnshpjncwgvppngvvfrjpztftgjbbmgdddrcgjggwpfrvfqbnzvwtscftbwfssmrgrbchjmvqcdgbdmmsswvplnvnjrnqzhttbzhlthzjslgtgrnqpghtbjnbftpdttvqhrljhnfqllrhfmzcnnwrljmchsvdbgvwmrpfzjsbngpffpgntrgflntbjnjwqbdhvhssvptdvvqvtrgqhhzrcfnnmbtzqrcghggcqhndggqzzgswflhqqrgmnggbrvpwqgtlptgmzdvgztgfqdzwnbrvvqhpnmcmptqljmqqmsslfcmgpnmnjmrsngrtbsffqbwlhsrwbzdcqvpbptpjphcjnwsmrjdbbrwftsnrgfhpjvsjcwpmpvfjtjvfnnwdjdttsjrqrgsznzqwjmsvscdtpptmdhqvwngtldtsnstjmwwrwwzdzvtndrrhgsgshzlpdrmlsgvplvbfrffvgvhmncbjdlqspbpdpcdsffrjzptltsznwwqbvnrwfdtrlbbvsmzjrhhvscqfwsqtmwnpjfgnjcttwphtqqhnvgjmzvczcrlmjjgwgnprcmmprltfrgrjlhvggdwvpnrqrtfwhpsfnjfvmvgzpmhrqmggldmsvztwcwzgblfbvbfvnshrdppzcvjwbjmsncwnbnjwbmwqjjwtjmwzpdvpwrfdtrqhnltgntvglgspvnbcsvnpppwmgphhsdqtpmrzmbdqlghsgjnnbhflzwghzzhdfsvjjcjfcssmvrmfqbgzcfwmhpvjqqrrhpsffczwgbjgwqlvzrvnhvzrqnfllqtrcjhmpdjfpqnlzhdnsctbfhsbpgwqdjdjhfbqzvzpsgmqwfjsffdjcqfmjgzqzvfsbhhvnwlfjfmdnphggdbnmpznlrzbnlbvhvplhjbzdmspnnlbctgzphghpngppdpdfbcdcpfntqlrwclsvnpljdbwcbhwzzdhbhsmslvvtjsmqmtpnhmmqnhfggnlpfthdhpwmrhzgfpwsffnrdqszcttrjsqzjqgspltfzzjnzwdfzvnmncwnphmjvcwlgrzcvpzcbndvhtjnfsgjtjdqfmgcgtgppsrcqwdjcqfddlhhnlfjrnsnssqblmnjjvmfbghtwwgcpgzfddvzsrsghqfrpvdtqmjfrzpvclmsnpmrqngdwvznlhdpbrzqtswfhnblnnbwjcwwbwfrgcdgrpphnslgnwbtfcgcmvvllwgvrcrdfcfwvwmdhhzmmnzmjgmgrgpdhngjgmhlqwtgzlngrbbfwfjrpwbqjnvdggrcfdgphfctvbmjnwfbpwrvbdbjrbhtnhfvhwvrptptsdrnzmnlwgbrwcswrccwdftgvjnvhffghdvhltjwhppfwfnmmtwclzzftzwvmhpmgvdsqzrfwqsmcgswnzjcnrvdgjlqdjrczphsvldlfzwdwmpncpvgqsvgpfpsfbgbmdhnfqbhdqwwwfdgqtmjlfbztsrwjrtqnrfpfqgplznpftrnjzhzcrnngqpwbrpbhlbfsgrpwfflrpbqdrqdplgcn \ No newline at end of file diff --git a/src/day05/Day5Lib.hs b/src/day05/Day5Lib.hs index f99fa27..27b9add 100644 --- a/src/day05/Day5Lib.hs +++ b/src/day05/Day5Lib.hs @@ -13,7 +13,6 @@ module Day5Lib ) where --- import Text.ParserCombinators.Parsec import Debug.Trace import Text.Parsec @@ -64,7 +63,6 @@ command = do char ' ' string "to " to <- many $ oneOf ['0' .. '9'] - -- char '\n' return Command {cnt = read countS, from = read from - 1, to = read to - 1} taskFile = do diff --git a/src/day05/test/Basic.hs b/src/day05/test/Basic.hs index 7e79677..9f1ce92 100644 --- a/src/day05/test/Basic.hs +++ b/src/day05/test/Basic.hs @@ -2,12 +2,6 @@ import Day5Lib import Test.HUnit import Text.Parsec --- trange = TestCase (assertEqual "1-2" (Range (1,2)) $ parseRange "1-2") --- trangel = TestCase (assertEqual "131231-215562" (Range (131231,215562)) $ parseRange "1-2") - --- trow = TestCase (assertEqual "1-2,3-4" [Range (1,2), Range(3,4)] $ parseRow "1-2,3-4") --- trowl = TestCase (assertEqual "1-2,3-4" [Range (1,2), Range(34524,62345)] $ parseRow "1-2,34524-62345") - tParseCrate = TestCase $ assertEqual "parse crate" (Right (Crate 'A')) (parse crate "" "[A]") tParseCrateOrNothing1 = diff --git a/src/day06/Day6Lib.hs b/src/day06/Day6Lib.hs new file mode 100644 index 0000000..4fbe38a --- /dev/null +++ b/src/day06/Day6Lib.hs @@ -0,0 +1,29 @@ +module Day6Lib + ( -- + processInput1, + processInput2, + ) +where + +import Data.List (sort) +-- import Debug.Trace + +createTiles :: Int -> [a] -> [[a]] +createTiles size list + | length list >= size = take size list : createTiles size (tail list) + | otherwise = [] + +allUnique :: Ord a => [a] -> Bool +allUnique = allUnique' . sort + +allUnique' [] = True +allUnique' [e] = True +allUnique' (e1 : e2 : l) = e1 /= e2 && allUnique' (e2 : l) + +-- #################### + +processInput1 :: String -> Int +processInput1 = (+ 4) . length . (takeWhile not) . map allUnique . createTiles 4 + +processInput2 :: String -> Int +processInput2 = (+ 14) . length . (takeWhile not) . map allUnique . createTiles 14 diff --git a/src/day06/test/AoCTest.hs b/src/day06/test/AoCTest.hs new file mode 100644 index 0000000..553f49e --- /dev/null +++ b/src/day06/test/AoCTest.hs @@ -0,0 +1,49 @@ +import Day6Lib (processInput1, processInput2) +import System.IO +import Test.HUnit + +testCases1 = + [ ("data/input0601.txt", 7), + ("data/input0602.txt", 5), + ("data/input0603.txt", 6), + ("data/input0604.txt", 10), + ("data/input0605.txt", 11), + ("data/input061.txt", 1920) + ] + +testCase1 (file, result) = do + withFile + file + ReadMode + ( \handle -> do + contents <- hGetContents handle + assertEqual "input test" result $ processInput1 contents + ) + +testCases2 = + [ ("data/input0601.txt", 19), + ("data/input0602.txt", 23), + ("data/input0603.txt", 23), + ("data/input0604.txt", 29), + ("data/input0605.txt", 26), + ("data/input061.txt", 2334) + ] + +testCase2 (file, result) = do + withFile + file + ReadMode + ( \handle -> do + contents <- hGetContents handle + assertEqual ("input test: " ++ file) result $ processInput2 contents + ) + +tests = + TestList $ + [TestCase (testCase1 c) | c <- testCases1] + ++ [TestCase (testCase2 c) | c <- testCases2] + +main :: IO () +main = do + runTestTT tests + return () diff --git a/src/day06/test/Basic.hs b/src/day06/test/Basic.hs new file mode 100644 index 0000000..e0e0e05 --- /dev/null +++ b/src/day06/test/Basic.hs @@ -0,0 +1,30 @@ +import Day6Lib +import Test.HUnit +import Text.Parsec + + +-- tCommand2 = +-- TestCase $ +-- assertEqual +-- "command" +-- (Right Command {cnt = 1, from = 0, to = 1}) +-- (parse command " " "move 1 from 1 to 2") + + +tests = + TestList $ + [ tParseCrate, + tParseCrateOrNothing1, + tParseCrateOrNothing2, + tCrateRow, + tNumberRow, + tCommand1, + tCommand2, + tStackBuild1, + tStackBuild2 + ] + +main :: IO () +main = do + runTestTT tests + return ()