diff options
author | Julien Dessaux | 2024-12-01 11:40:32 +0100 |
---|---|---|
committer | Julien Dessaux | 2024-12-01 11:40:32 +0100 |
commit | e51c318b876a10be5a82f341e3d32e6050740a0c (patch) | |
tree | 022426af4321ca7bc06dba9ff708d2fce7f9a769 /2024/01-Historian_Hysteria | |
parent | 2023-25 in haskell (diff) | |
download | advent-of-code-e51c318b876a10be5a82f341e3d32e6050740a0c.tar.gz advent-of-code-e51c318b876a10be5a82f341e3d32e6050740a0c.tar.bz2 advent-of-code-e51c318b876a10be5a82f341e3d32e6050740a0c.zip |
2024-01 in haskell
Diffstat (limited to '')
-rw-r--r-- | 2024/01-Historian_Hysteria/example | 6 | ||||
-rw-r--r-- | 2024/01-Historian_Hysteria/first.hs | 46 | ||||
-rw-r--r-- | 2024/01-Historian_Hysteria/input | 1000 | ||||
-rw-r--r-- | 2024/01-Historian_Hysteria/second.hs | 47 |
4 files changed, 1099 insertions, 0 deletions
diff --git a/2024/01-Historian_Hysteria/example b/2024/01-Historian_Hysteria/example new file mode 100644 index 0000000..b8af9ad --- /dev/null +++ b/2024/01-Historian_Hysteria/example @@ -0,0 +1,6 @@ +3 4 +4 3 +2 5 +1 3 +3 9 +3 3 diff --git a/2024/01-Historian_Hysteria/first.hs b/2024/01-Historian_Hysteria/first.hs new file mode 100644 index 0000000..44064c4 --- /dev/null +++ b/2024/01-Historian_Hysteria/first.hs @@ -0,0 +1,46 @@ +-- requires cabal install --lib megaparsec parser-combinators heap vector +module Main (main) where + +import Control.Monad (void, when) +import qualified Data.List as L +import Data.Void (Void) +import Text.Megaparsec +import Text.Megaparsec.Char + +exampleExpectedOutput = 11 + +type Pair = (Int, Int) +type Input = [Pair] + +type Parser = Parsec Void String + +parseNumber :: Parser Int +parseNumber = read <$> some digitChar <* optional hspace + +parsePair :: Parser Pair +parsePair = (,) <$> parseNumber + <*> parseNumber + +parseInput' :: Parser Input +parseInput' = some (parsePair <* eol) <* eof + +parseInput :: String -> IO Input +parseInput filename = do + input <- readFile filename + case runParser parseInput' filename input of + Left bundle -> error $ errorBundlePretty bundle + Right input' -> return input' + +compute :: Input -> Int +compute input = sum compute' + where + (l, r) = unzip input + compute' = fmap abs $ zipWith (-) (L.sort l) (L.sort r) + +main :: IO () +main = do + example <- parseInput "example" + let exampleOutput = compute example + when (exampleOutput /= exampleExpectedOutput) (error $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput) + input <- parseInput "input" + print $ compute input diff --git a/2024/01-Historian_Hysteria/input b/2024/01-Historian_Hysteria/input new file mode 100644 index 0000000..e1e570b --- /dev/null +++ b/2024/01-Historian_Hysteria/input @@ -0,0 +1,1000 @@ +49744 57964 +20738 85861 +20319 65072 +79568 74248 +78194 83454 +48701 94102 +69552 26808 +62781 67392 +85323 47428 +99344 72568 +27523 97243 +48039 36600 +91532 31571 +21306 31571 +52409 10805 +33901 31571 +80772 38756 +13849 54584 +72294 28326 +86065 65553 +93987 72533 +81640 39741 +25701 89912 +98611 57082 +80949 94974 +84717 61876 +31599 57082 +87119 65871 +56659 22897 +60864 45613 +64914 23217 +41931 23325 +13675 56060 +85486 66429 +99589 34428 +80942 50436 +66446 27795 +62260 94102 +17991 95754 +38666 94018 +82715 65319 +96748 95959 +76620 75639 +39724 86975 +83462 61805 +80757 66429 +80161 66912 +27338 97381 +29349 58983 +19921 95959 +33222 95959 +29944 13408 +33806 85400 +70346 93552 +62146 56773 +29455 80011 +13072 39741 +78082 85362 +68244 22897 +92699 85216 +77401 20843 +53297 82515 +47145 90966 +21714 83969 +38897 81770 +26583 96472 +33538 94102 +50607 51437 +29920 18034 +86162 28326 +20887 26374 +70011 95959 +91072 33837 +78041 78550 +36971 96504 +93552 45613 +35166 39027 +55765 21306 +62548 27834 +25287 27430 +16941 92472 +92686 35418 +18196 61876 +98271 56773 +70197 57082 +60087 36019 +94691 57607 +71834 36600 +66838 72703 +99514 30035 +87478 37752 +31855 89828 +63670 55131 +96582 96472 +56506 28326 +22986 50890 +28459 76391 +81838 56060 +73857 57964 +65911 96472 +70631 78246 +35372 83680 +59537 53263 +69802 83656 +60922 95754 +98033 55131 +61192 56233 +10544 96472 +70076 96748 +10345 72533 +44069 59959 +51368 24943 +32209 94323 +93608 15236 +66052 18518 +18194 74248 +61876 60690 +38359 91941 +99648 94102 +26870 95959 +42700 22080 +60343 57082 +86403 63844 +68549 71668 +94395 31571 +54117 76136 +82957 91182 +76970 30786 +21467 96999 +63240 61876 +41407 52311 +79043 64533 +68714 18737 +88316 43212 +74330 56060 +95754 42389 +97007 15593 +23600 61876 +35794 37497 +51155 33837 +38878 57779 +88160 66923 +85960 15356 +19608 90678 +15335 19142 +37858 28326 +52362 61567 +92440 25615 +40806 68569 +71751 97177 +42549 70211 +97573 97882 +70977 72533 +82166 87204 +73168 45613 +33988 40287 +81017 24791 +97826 39494 +49366 57853 +58833 30509 +12049 99552 +44295 28655 +33286 86203 +68443 28326 +72269 59959 +56222 28326 +66315 57082 +76461 90360 +46531 28326 +63925 80949 +34114 91182 +81992 31571 +24536 11763 +22518 70755 +11355 31571 +31145 28326 +55129 17000 +17135 54266 +19316 57094 +63206 26374 +31039 57082 +20090 45613 +25633 34423 +95984 74248 +28231 31899 +79070 33837 +20608 10744 +31697 41568 +13523 99552 +44342 74248 +58138 66429 +53615 11293 +13400 99229 +44548 22051 +24137 37497 +30607 56348 +53530 14398 +44035 83213 +56874 38315 +79779 99487 +43670 58472 +18036 94582 +92084 36917 +95857 89269 +59178 48607 +77507 14389 +24249 94980 +68856 14162 +46913 13677 +77339 59959 +40867 94102 +20361 27399 +10126 40204 +56279 57964 +37924 33837 +35066 28326 +79906 33390 +77938 18925 +30427 74248 +49792 39885 +10401 72533 +27300 12967 +15461 66429 +86381 10670 +25350 72755 +79472 90026 +11038 78545 +31085 82018 +29807 94018 +50918 37018 +56092 21306 +18839 97675 +12995 26374 +89946 62988 +59719 56060 +23083 50209 +14050 77608 +61314 53662 +55468 87206 +32735 39741 +54600 91182 +15821 77607 +88847 22065 +99872 95959 +53375 22897 +35209 56060 +77630 91182 +96800 96472 +26264 66429 +35274 25151 +25538 61863 +23186 96504 +91734 39741 +91485 59959 +89103 36687 +78785 96472 +13949 65200 +44396 31571 +16507 29954 +65200 56352 +67353 98520 +44969 45613 +70144 10313 +52148 90071 +43359 21780 +83268 52625 +92052 42928 +56773 32010 +77013 33885 +83505 38104 +44966 20462 +30790 94102 +73511 57082 +17994 13777 +52046 31571 +71445 38756 +31099 57082 +96873 72533 +63090 47113 +39327 68753 +33913 60253 +38546 53200 +16726 29956 +86820 42850 +61083 38756 +40096 55131 +95004 23484 +17136 61958 +87117 80803 +44050 92626 +65352 22440 +13813 61876 +82994 99641 +16136 45613 +13490 56775 +51061 80949 +61335 91182 +52053 33837 +50455 45613 +50980 41238 +58258 38756 +52546 10801 +32924 50528 +98144 31571 +74993 37070 +27437 36600 +46078 69578 +35719 36796 +91307 66923 +82413 98969 +21224 59959 +95996 90116 +61607 33837 +71741 14132 +48452 79616 +16452 36600 +29032 22897 +68251 80913 +15226 83141 +82072 62781 +78732 39741 +26072 57964 +44560 96564 +57175 78034 +86181 64955 +70004 13437 +21403 13757 +25396 90508 +22388 52004 +70087 96504 +35852 65506 +16149 94963 +12499 33837 +49826 57082 +64984 29694 +84227 56060 +13306 60690 +74909 11772 +74115 55131 +82914 72533 +32578 19223 +30971 86868 +58566 24936 +68362 38756 +23665 66429 +45658 28326 +29325 66923 +11640 96472 +73355 95114 +87940 62631 +47689 53862 +80029 39694 +94261 66429 +84444 34312 +65441 62781 +93632 24791 +27259 60690 +52365 20133 +39145 29503 +54077 45613 +22106 78246 +21462 78868 +61680 96472 +87688 10794 +82919 85055 +70741 55416 +94102 45462 +20816 72533 +23126 80949 +46112 54307 +78700 91182 +37497 10418 +34307 96426 +32939 91637 +55698 73001 +28008 62824 +56695 66157 +45273 79240 +26479 62781 +17458 28326 +13240 96504 +49411 16963 +11293 40392 +59492 11248 +39242 18166 +82125 19380 +10425 43878 +96169 94102 +72148 96504 +98699 55131 +64519 52279 +75839 26129 +99109 35627 +52989 24557 +19839 39741 +33892 57082 +57138 34347 +85986 61876 +63721 30120 +11728 96504 +97643 59959 +75752 35597 +78681 70815 +69201 86837 +22897 33144 +84476 38522 +70192 39741 +11796 57964 +24791 38756 +91343 82170 +71121 29320 +19725 99552 +55131 37497 +10398 64652 +43837 57964 +96472 21420 +15661 66429 +73100 81872 +16347 66923 +78667 20821 +62503 62781 +55388 26374 +30709 66923 +34765 95959 +13082 29914 +92169 76512 +74594 92616 +74628 53874 +36549 25654 +35684 62381 +44535 36600 +41585 64032 +35820 21306 +48491 57082 +36868 56612 +24081 66429 +44738 69207 +14242 61270 +66040 86333 +38756 15652 +12973 93977 +39993 52251 +74626 27427 +50280 39741 +96839 54610 +80128 91182 +78847 59523 +91308 65211 +26341 26687 +53571 95959 +18687 95754 +61386 26984 +44578 56060 +21602 66294 +39402 19968 +98549 21306 +57082 57082 +95948 96472 +55751 28461 +42067 35169 +28553 27971 +47930 26374 +32007 43799 +52341 31491 +71093 55131 +74053 19526 +59959 96472 +34557 39741 +55998 56060 +22907 45181 +23079 95754 +40762 88028 +11191 58553 +37934 56923 +12082 85950 +61831 36600 +15330 24791 +70852 41905 +85973 56773 +34561 75129 +74529 78267 +39741 28059 +20706 21306 +80276 70751 +65709 56060 +36696 94102 +94364 28879 +20233 98589 +27112 80065 +99335 59959 +37076 91182 +92060 71029 +46294 42031 +25253 24610 +45857 89413 +41637 42336 +93798 61876 +17452 94018 +36934 23498 +22889 45613 +71786 91182 +40024 91182 +35594 69578 +46950 22480 +12379 31571 +16945 30582 +61614 68940 +48173 57082 +32624 58684 +94003 80949 +21551 72636 +49500 56060 +91508 20461 +71692 96504 +62379 94102 +70549 11293 +87238 54313 +16792 95243 +93509 41331 +72142 59959 +92474 76566 +53189 85632 +77138 56060 +41950 61876 +85591 40423 +78605 72026 +62515 87456 +49015 56060 +41423 77742 +47452 74248 +57647 66923 +21068 27043 +30120 95754 +95959 18741 +93197 61876 +80423 21650 +57729 71451 +29453 66923 +28326 87009 +46744 59056 +81115 66720 +38857 74550 +49849 70483 +55419 26374 +78335 45613 +60739 12508 +35968 17587 +18079 94018 +53376 80949 +41369 95754 +30754 14438 +81555 28326 +43955 41187 +38988 78246 +38187 67264 +99187 45613 +37551 21995 +13548 57964 +16827 72533 +43704 79494 +19039 36600 +70278 78246 +80016 22311 +62776 45613 +79357 33868 +64692 66923 +98491 42514 +88957 86978 +74622 57082 +47277 56060 +96504 53011 +87114 78246 +49142 68730 +92395 61876 +78246 45633 +11014 94102 +18985 48066 +76726 10822 +57367 77129 +28336 56773 +24092 42238 +25097 27558 +94664 68325 +51015 98113 +40965 69363 +89650 69743 +76254 54738 +69293 31098 +11002 43227 +60647 46055 +46338 97100 +36587 14512 +89640 96472 +27182 38982 +11523 54465 +33740 14458 +71700 78246 +85059 29940 +93168 61876 +96566 96501 +91222 94871 +62135 18552 +75319 24791 +14722 37543 +91216 59756 +58320 96504 +74828 99175 +11918 43162 +84262 30120 +47931 66308 +46840 94018 +24878 71006 +20196 36600 +20053 97794 +67727 36600 +97721 45613 +99910 96504 +51916 61876 +39198 39741 +15353 96472 +27494 83600 +52327 54203 +25445 55131 +29317 39741 +94384 10226 +19572 93557 +36386 44570 +31160 70870 +73783 84105 +54235 28326 +16618 66240 +16583 11293 +19080 91182 +57657 31571 +73272 48955 +69949 96472 +75895 89320 +41001 80919 +14825 57964 +94637 57010 +54556 56655 +33409 68761 +74248 68712 +25684 99655 +48181 63856 +69001 31571 +49706 36600 +54409 39741 +91718 66923 +31725 94777 +72533 27698 +45613 78246 +15604 87841 +32504 35779 +62179 62336 +72501 99090 +30561 39148 +79165 11307 +31571 65867 +13483 20228 +20894 89446 +23467 21201 +92291 55131 +94833 91236 +95658 91182 +51242 98018 +73738 62574 +30352 70801 +27292 17870 +86166 11293 +72308 61796 +73796 91182 +30666 96472 +13744 56060 +94746 69471 +15788 58248 +58081 78246 +23677 35070 +20968 28326 +56889 34484 +63353 66429 +12017 61948 +85307 52672 +59926 51517 +46647 55131 +99274 19268 +34708 21306 +26098 84160 +70078 45613 +86009 71606 +65008 57082 +19264 83154 +20880 46063 +80697 36600 +71856 52774 +62253 85982 +49783 95959 +46304 59716 +21948 61876 +96173 56060 +67970 65512 +29760 78254 +73066 97659 +77146 15368 +37411 79698 +13440 56060 +70769 48563 +24345 24307 +38871 96472 +34643 39899 +85582 97868 +48894 61876 +94213 97721 +18933 96472 +92678 95736 +53120 96472 +64527 96472 +10371 78098 +59311 88758 +44618 56981 +86901 37114 +90476 91182 +49214 45967 +12902 22940 +77674 19791 +58536 54989 +55151 95959 +62585 59296 +79446 91182 +42137 48087 +12461 44870 +19364 33837 +45512 71150 +68482 71531 +85801 78226 +27769 65652 +88199 81929 +99552 96748 +20141 61876 +22666 66429 +86571 85514 +20972 66429 +67365 28326 +96745 70447 +30339 62505 +64329 57964 +96535 26191 +61214 94102 +41068 82628 +54705 78246 +58853 31571 +80242 63529 +50060 57572 +47480 28326 +12425 61026 +44531 87545 +70463 50053 +62417 21306 +41173 74875 +72821 50395 +27947 96504 +15054 26374 +42867 16834 +71107 61876 +21999 91182 +87293 66292 +57292 53687 +89185 22897 +66923 23550 +57480 56060 +92097 56060 +97667 72991 +50851 86690 +92405 36161 +22888 94102 +86556 28570 +62406 11574 +17094 66429 +62672 56060 +27924 30120 +82298 32659 +89119 59959 +74159 69406 +69593 78246 +88983 83908 +93972 83391 +36585 57082 +52166 66923 +62447 95959 +94429 39880 +41624 54107 +14949 64308 +51582 28326 +54462 81726 +92666 64896 +73886 38756 +95390 57082 +75706 87003 +72752 95754 +96156 59816 +35797 31571 +76304 62517 +17302 56078 +29863 45613 +71018 41031 +79393 96504 +39665 84870 +59897 28326 +19583 89861 +39734 55131 +54048 21306 +53737 36600 +25787 92176 +68097 38864 +84659 35639 +66422 78246 +61057 77596 +26983 33837 +78620 95754 +75994 96472 +91182 31571 +39465 99552 +31512 64487 +67267 99879 +92670 61876 +88963 12495 +69591 74404 +34360 95959 +21342 51751 +48368 68547 +84355 71495 +72598 13991 +25399 75100 +77633 38756 +12872 67701 +12911 13630 +67871 17769 +79299 23907 +62490 28214 +71363 26374 +42174 86827 +23543 18277 +33152 73936 +60595 25272 +76463 38756 +53592 83420 +66429 78246 +25297 19454 +48577 57964 +42304 14722 +93639 89903 +42775 71581 +50460 11293 +29524 80109 +46795 40266 +79152 53926 +14530 61876 +68493 26374 +24568 82687 +60047 93799 +16856 57484 +11257 55131 +45834 91182 +67239 99552 +90933 31571 +82510 39741 +41567 57964 +67771 12794 +51836 23333 +24272 34097 +98163 78032 +42341 28326 +99975 50627 +86706 78246 +63008 57082 +33127 94102 +56060 74408 +37624 19600 +17196 46856 +77494 29078 +31050 30252 +30922 68664 +13399 24791 +54645 94102 +62173 56060 +62597 95959 +25348 95754 +60690 34683 +58377 92313 +38765 91182 +59869 59959 +95782 21975 +43833 24559 +57964 47902 +79937 22798 +35657 99552 +38354 39741 +91373 32691 +28546 23562 +38067 66077 +60370 39741 +36600 80189 +92953 61431 +43812 69640 +28600 26374 +56239 94102 +88573 99863 +23117 99552 +18732 74248 +39600 31571 +73867 53714 +77524 95959 +45578 26950 +46821 36659 +62249 97476 +29888 99552 +54493 57468 +25883 29690 +53727 55505 +28962 11293 +89727 11115 +60204 26859 +27607 66429 +54053 36600 +78805 13083 +27832 94102 +69578 72533 +13366 66923 +50257 80949 +61725 12887 +74296 39275 +49595 45613 +68879 79435 +30887 31571 +77646 40579 +55065 95754 +71461 55131 +20455 25666 +15976 96504 +44244 94102 +19368 33837 +66895 59959 +63579 38775 +25319 62695 +65478 38944 +98576 88366 +30591 88848 +92278 26374 +11161 26374 +24787 86464 +32547 93818 +34401 44410 +19631 47153 +97521 48956 +42475 28326 +45770 33837 +96953 52782 +77277 99552 +21192 30120 +96102 49059 +83877 99552 +43843 83930 +94704 36600 +33068 36600 +32295 53884 +14212 29797 +26374 58819 +72056 90078 +33837 93677 +74749 31723 +84678 57082 +37631 96472 +89434 61876 +41106 91182 +67810 18348 +52560 97721 +28995 54202 +17694 42638 +19388 36600 +19607 66429 +63536 12795 +43226 10644 +85270 81984 +11781 38756 +59217 36600 +20981 73723 +41651 66429 +51009 19419 +27331 64134 +52422 83283 +60900 21538 +40218 49769 +46711 48222 +47987 33837 +41207 70442 +47097 45613 +77564 99991 +94018 96504 +74639 32409 +56126 28988 diff --git a/2024/01-Historian_Hysteria/second.hs b/2024/01-Historian_Hysteria/second.hs new file mode 100644 index 0000000..0a13fa2 --- /dev/null +++ b/2024/01-Historian_Hysteria/second.hs @@ -0,0 +1,47 @@ +-- requires cabal install --lib megaparsec parser-combinators heap vector +module Main (main) where + +import Control.Monad (void, when) +import qualified Data.List as L +import Data.Void (Void) +import Text.Megaparsec +import Text.Megaparsec.Char + +exampleExpectedOutput = 31 + +type Pair = (Int, Int) +type Input = [Pair] + +type Parser = Parsec Void String + +parseNumber :: Parser Int +parseNumber = read <$> some digitChar <* optional hspace + +parsePair :: Parser Pair +parsePair = (,) <$> parseNumber + <*> parseNumber + +parseInput' :: Parser Input +parseInput' = some (parsePair <* eol) <* eof + +parseInput :: String -> IO Input +parseInput filename = do + input <- readFile filename + case runParser parseInput' filename input of + Left bundle -> error $ errorBundlePretty bundle + Right input' -> return input' + +compute :: Input -> Int +compute input = L.foldl' similarity 0 l + where + (l, r) = unzip input + similarity :: Int -> Int -> Int + similarity acc i = acc + i * (length $ L.elemIndices i r) + +main :: IO () +main = do + example <- parseInput "example" + let exampleOutput = compute example + when (exampleOutput /= exampleExpectedOutput) (error $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput) + input <- parseInput "input" + print $ compute input |