diff --git a/hie.yaml b/hie.yaml index 6facbf5..be5f215 100644 --- a/hie.yaml +++ b/hie.yaml @@ -469,3 +469,6 @@ cradle: component: "sln_2023_24" - path: "./solutions/src/2023/25.hs" component: "sln_2023_25" + + - path: "./solutions/src/2024/01.hs" + component: "sln_2024_01" diff --git a/inputs/2024/01.txt b/inputs/2024/01.txt new file mode 100644 index 0000000..e4fcaaa --- /dev/null +++ b/inputs/2024/01.txt @@ -0,0 +1,1000 @@ +80784 47731 +81682 36089 +22289 41038 +79525 17481 +62156 70590 +87975 21561 +54635 59542 +43393 99451 +45310 59542 +18324 92078 +36887 79481 +35723 48782 +78420 35875 +93307 52649 +77342 80601 +69125 47895 +37292 20025 +45553 59542 +27412 30010 +67708 70822 +92078 91109 +48367 87581 +26852 30538 +42123 17859 +20067 87581 +20239 32262 +50660 73585 +46240 45533 +29502 46131 +77080 36089 +64180 14043 +74942 72085 +73979 22860 +47999 41397 +36014 35101 +39827 32262 +81418 86581 +47467 14538 +65923 46584 +95054 17500 +59680 37730 +94609 19539 +33451 39467 +69173 12422 +31769 34255 +85180 29056 +82104 52296 +90955 38171 +83927 70590 +59455 17306 +28681 86581 +54107 13789 +79824 21386 +53890 46519 +94883 87581 +12797 46584 +66809 24306 +78327 24086 +19780 43234 +55623 38171 +10937 42288 +24262 74556 +92076 77080 +69179 15029 +13764 52214 +92187 63803 +91328 94609 +24356 36089 +82974 84225 +55924 70822 +65439 55383 +78097 82069 +58045 12422 +72087 37730 +70822 32262 +78511 48368 +39251 54157 +74623 59542 +32104 74623 +69499 17481 +97919 33260 +37778 74623 +56977 14684 +19100 98708 +29453 34463 +18839 87581 +75434 75839 +58303 84225 +84924 24108 +99813 37730 +99218 93749 +54262 38470 +61773 94274 +67034 56935 +71788 74556 +83455 22604 +76089 84225 +11003 85999 +80814 62471 +41928 56000 +52541 71034 +37488 53519 +78475 33260 +57117 32449 +54543 54732 +19050 52775 +80967 76950 +71045 29497 +61189 43136 +75055 38171 +91573 46131 +50100 70590 +46554 24973 +25154 99579 +19853 77080 +11807 20266 +40158 24315 +92543 38171 +93300 87581 +79628 68978 +19164 51702 +53719 96515 +38074 46503 +25693 53188 +79321 17481 +25108 21108 +38398 72487 +22073 59542 +98191 39605 +80619 66029 +99138 74556 +49897 33260 +84225 10112 +14331 66948 +94603 36089 +33642 75862 +14314 89669 +12998 48362 +10989 21312 +68074 46131 +10531 47636 +32312 70590 +66844 38171 +73563 77034 +33099 65439 +93734 71747 +56538 35101 +55501 82069 +19974 94609 +21308 23592 +37062 36089 +99908 59542 +92651 94321 +81371 84225 +96268 35101 +58685 25444 +37191 86819 +71956 87899 +28341 70822 +11995 64983 +92426 70590 +30515 57117 +72193 37685 +82150 77080 +19292 73860 +52076 53020 +26135 78522 +49482 79269 +56103 81747 +62006 66781 +41905 24906 +23782 10112 +39865 41627 +80074 91781 +84213 70822 +19586 43155 +87133 70822 +67811 65439 +60019 23478 +29057 70590 +77349 16491 +32372 13789 +93055 65439 +82209 30330 +75731 60775 +91276 79848 +90330 37730 +41180 59542 +81204 95937 +36092 11620 +13033 38171 +34488 51306 +71770 81668 +37630 57117 +87608 69809 +90593 59542 +52792 39467 +33508 46226 +78150 29434 +18072 84225 +11205 63803 +28801 39467 +89617 47552 +91269 77080 +50829 15405 +96387 68582 +44407 46131 +17491 74556 +77464 57117 +67053 71830 +67048 20722 +15369 76996 +71812 74803 +96980 35466 +39993 82069 +65498 59148 +88138 79430 +16497 54357 +75624 46131 +84028 39464 +82572 39122 +14297 36089 +33805 85723 +44778 74556 +14498 87581 +71384 17116 +47173 82069 +60652 82069 +67241 46316 +73276 80192 +46786 68313 +88795 97650 +95120 23939 +53010 82069 +40256 82069 +26913 71782 +44746 88309 +49866 86581 +50951 97892 +80832 30492 +22643 70822 +84334 86581 +96028 70590 +39816 56848 +78427 63379 +27147 92078 +35658 12422 +76747 47552 +32896 59542 +98898 12422 +45166 37081 +38602 27009 +17507 82069 +75567 74623 +71921 29970 +11800 90710 +23973 68582 +80315 68252 +86659 13782 +23014 71633 +33041 57117 +93351 65439 +69088 46584 +71994 91050 +61532 98835 +98790 76392 +62890 75035 +37723 36089 +56136 20455 +91663 84225 +76413 87581 +89223 11659 +33477 47973 +34541 16506 +82603 59542 +37730 75791 +25272 98835 +86703 82248 +56755 54948 +74556 92078 +11337 36089 +52534 27174 +60327 87581 +84554 89264 +11381 59542 +99601 81999 +20139 33278 +25530 59558 +97414 74906 +11269 70822 +66500 13789 +23195 68582 +22872 53300 +94937 51202 +12273 55687 +63364 71074 +14160 15554 +57473 39467 +19091 87899 +43286 16810 +25975 33571 +57319 39467 +61075 66766 +95980 98826 +47190 55086 +82297 87581 +24071 53175 +85766 82069 +48467 36089 +16301 77080 +89176 48368 +76512 29182 +61987 37730 +10252 83226 +42503 12203 +75498 71010 +56685 98679 +22338 48368 +40666 86581 +88276 32262 +69493 94854 +75435 22046 +91561 48368 +99202 42878 +90978 38171 +89712 77080 +48877 36148 +31528 92078 +15135 36258 +47468 10112 +23557 10112 +46381 98492 +95007 57390 +31053 95937 +26099 27278 +34939 75494 +44252 59542 +54945 73875 +26328 38171 +73116 46131 +16168 13789 +28518 28323 +25310 28056 +63436 27589 +77562 37730 +80614 82409 +76421 63235 +24521 53590 +17937 86492 +80687 56569 +56076 10767 +64810 74623 +73278 26540 +58083 46584 +90208 32262 +17793 65461 +92155 47552 +47982 91242 +82069 77080 +39226 87581 +34622 42348 +99735 46584 +22264 37730 +46131 70590 +90426 18162 +75148 63803 +98835 70822 +80373 30652 +62705 46131 +95974 32262 +54882 36453 +50578 71111 +30132 39798 +22527 35101 +41027 91852 +69013 46238 +79152 69190 +89330 61021 +85358 13789 +15086 54357 +50002 39467 +42579 42483 +11717 87581 +43825 24664 +50771 87581 +89105 45839 +84561 92078 +42692 38899 +20001 94609 +40205 38051 +13295 20519 +96020 10112 +47293 87899 +94929 10112 +24422 82448 +62066 59975 +52303 86581 +79329 72343 +69417 16516 +13062 47552 +41201 84658 +92635 62180 +19834 91848 +34343 18222 +18208 30134 +82611 63803 +12207 74511 +93804 53502 +51066 33260 +28374 66275 +34107 10112 +13223 80233 +97959 94470 +51054 57117 +82951 30540 +61626 32898 +52816 16045 +67095 46131 +96825 70590 +34307 77500 +98853 35083 +76437 65439 +71489 77080 +46520 16597 +15288 54357 +34660 63199 +65053 65439 +99414 57117 +85660 83055 +10245 32262 +12775 67705 +57298 65439 +64105 36878 +36104 12422 +89241 56000 +41045 87764 +16506 88026 +53044 44649 +17425 84939 +56000 69714 +63575 92078 +50418 19328 +39915 10112 +97542 66182 +97468 16506 +74779 94609 +90120 17677 +91404 87581 +83440 31253 +80583 16585 +16542 82069 +34305 48368 +95455 30383 +56639 87581 +33983 12422 +36565 47552 +63737 32262 +60378 37730 +80067 95189 +45068 61725 +20720 74180 +24918 94609 +18264 86581 +43293 57117 +67279 38551 +75286 95937 +19582 71378 +62806 12497 +48345 96497 +45026 68499 +97371 36028 +80754 74761 +97543 54357 +51110 12422 +23427 24635 +44698 68582 +84624 28102 +74813 12960 +94691 82069 +68498 93943 +30492 52810 +65243 34777 +12244 82069 +14525 65439 +75430 98694 +73494 87567 +97587 67336 +59739 59542 +38047 30492 +35519 43518 +64167 92078 +52460 72101 +49707 81573 +86581 24885 +93340 69244 +91249 13789 +35632 52637 +78007 56318 +86640 46131 +84853 30492 +69589 55403 +87840 94609 +15931 65439 +87788 94609 +36704 39467 +56849 92199 +23845 70822 +58771 59306 +15094 72864 +78669 38116 +67290 30492 +30809 12422 +60616 82069 +99946 86597 +57245 10112 +14346 59022 +95083 79436 +57974 65334 +22448 86581 +89557 42455 +72735 70224 +80323 28694 +19558 59542 +66100 67968 +29557 54357 +75549 48368 +47979 65120 +81370 26550 +99542 80446 +92151 48368 +60693 60658 +70590 36856 +93811 95439 +50263 78334 +51728 74556 +48629 23585 +63500 35242 +98688 74623 +50638 39467 +39320 57117 +85601 48368 +86749 63803 +84452 24596 +40346 33260 +56693 59543 +96450 10429 +46319 50421 +88183 63450 +47057 56000 +99066 65734 +95736 95937 +49970 77187 +76310 86581 +72960 21713 +61234 33798 +20364 70590 +89304 82069 +90141 11734 +11708 88748 +67064 10112 +51923 71970 +47055 36089 +45990 28498 +89727 87581 +92801 71911 +39807 46584 +72390 35136 +16446 62868 +77863 56000 +42686 13789 +28678 75289 +84858 67442 +64404 84225 +77138 70822 +48368 33260 +89589 10112 +92469 43286 +66906 19599 +96794 74623 +98997 15262 +32791 74556 +86217 36089 +23368 31640 +88661 19276 +72933 85540 +22703 30492 +71905 65439 +58050 40461 +29588 95734 +39243 55933 +83527 17481 +15249 58026 +77033 47662 +47552 86581 +57262 69752 +20061 18772 +81279 43751 +58632 70157 +13683 94609 +21964 74556 +95224 59604 +83465 88283 +81225 36089 +16585 17481 +15766 15653 +50261 57292 +82233 54937 +60140 94609 +52750 31767 +20808 56000 +57243 92469 +43252 32262 +60198 86581 +18620 55678 +44051 50219 +15210 39162 +60005 36089 +66217 74556 +13853 70822 +63803 70822 +35040 65079 +65842 38038 +39316 20720 +87663 65984 +20060 68582 +25890 48796 +16116 82639 +31571 70822 +43980 84152 +53704 11951 +79055 42179 +57231 22127 +88376 85408 +59541 95959 +92556 53432 +88194 16317 +34727 30904 +16387 57117 +96730 10644 +65083 36089 +15228 16506 +81423 59542 +87899 17481 +90911 85798 +17159 83599 +50392 77080 +22864 43286 +37528 94609 +21429 59542 +28265 46877 +46764 37608 +91323 65439 +83391 35269 +74503 39369 +93683 63803 +31047 38528 +53705 33260 +74884 86581 +54241 99733 +45641 73338 +64783 94609 +36885 76835 +94878 70590 +95560 21274 +61985 67119 +65075 74623 +32813 86204 +36749 72304 +78654 50542 +86690 74556 +70019 32262 +20641 60329 +77752 57117 +60383 20123 +90352 74623 +24141 71761 +65390 10350 +47054 38708 +39003 57915 +38161 92078 +38171 32262 +84400 10112 +15341 70822 +39428 16506 +13942 13789 +96936 70483 +93060 50175 +38192 67104 +46944 64882 +10846 12422 +35101 35801 +91104 20720 +13040 13789 +32161 31456 +97253 86581 +70362 70590 +53579 26550 +32295 56680 +77624 74556 +19453 38171 +11458 82069 +39312 94596 +68109 81118 +63009 59542 +96761 33260 +42532 96201 +42166 87800 +87613 44967 +69532 34024 +48387 83207 +54987 28919 +20342 84225 +23664 56000 +46174 10112 +72309 12474 +72157 73250 +28932 14632 +36882 84225 +23261 38171 +99849 41962 +95036 44159 +67696 12422 +58158 12422 +99688 84696 +95937 56267 +67472 28880 +32262 78595 +39793 31505 +53311 87581 +28411 51506 +53156 88986 +97772 95152 +27795 70590 +25013 30223 +14528 72611 +76358 11601 +46584 32226 +45091 53888 +78740 82069 +28185 56000 +86150 33260 +12422 13789 +49946 36089 +39558 91418 +12969 78072 +30758 23674 +30625 72335 +29062 46065 +51088 61952 +88725 61869 +94002 74556 +35800 29768 +43599 95937 +65922 82638 +52159 91936 +96552 48368 +29575 38995 +51418 82069 +39467 36420 +76259 15058 +52506 24873 +34793 68305 +36089 94116 +25337 69496 +46924 86581 +62992 82069 +62236 95937 +50926 25384 +72595 74556 +53666 67031 +44374 15724 +44629 51188 +80493 95937 +76680 59202 +96833 95937 +96740 86581 +90400 65439 +41922 10112 +10333 23762 +13789 94609 +79941 85091 +34855 55865 +87532 37140 +85678 46584 +34735 74556 +27059 28517 +62170 92078 +59542 84972 +44107 46131 +62531 86581 +36662 56000 +36292 59542 +76115 57037 +39296 41740 +34919 10743 +74948 86439 +50539 59294 +85056 31471 +28116 65439 +39085 38176 +84409 95937 +57138 46131 +72483 57117 +32395 72441 +95005 74883 +87581 51660 +28602 28664 +34996 46131 +96430 56339 +77226 70822 +93760 10112 +40364 94609 +40141 39467 +68490 40042 +48192 93216 +68068 70590 +73633 15355 +83619 47552 +81572 73873 +25924 70822 +29554 63803 +19515 26687 +61203 65439 +66108 77080 +34039 33260 +54877 35542 +38583 96489 +90667 42450 +84938 10487 +46965 59542 +54823 74069 +71321 73685 +19154 57117 +73217 30492 +76105 13647 +39670 12422 +30004 64951 +21400 16931 +58732 46131 +46185 57117 +35140 97447 +21937 32262 +29766 37730 +97110 17481 +37622 70822 +10481 46131 +89082 37730 +37941 74556 +14004 13789 +75256 31754 +91586 98835 +70127 84728 +27519 76800 +30742 29667 +40956 72181 +38337 85286 +93338 32262 +17167 48368 +90586 87581 +77695 72650 +78449 10112 +14007 92469 +44816 56000 +92128 12422 +70434 89508 +46495 17481 +86412 57117 +12915 29947 +31239 98835 +25873 62842 +18560 54357 +56101 27741 +85775 70590 +36475 51980 +80038 10112 +98826 30492 +41315 82816 +11389 82624 +11947 75881 +26407 10112 +30869 41026 +87026 44676 +41060 77177 +27239 32212 +50309 47552 +28120 35101 +52772 87899 +50851 13789 +52738 67642 +73374 61276 +29063 83110 +75283 77080 +91345 33260 +14809 19229 +82519 27777 +92688 65439 +10526 48368 +59702 92469 +35688 48965 +87590 24828 +40206 75331 +36194 74556 +43336 12746 +26034 29117 +23466 47552 +33234 19772 +17481 11383 +68582 54357 +36038 20809 +33704 98835 +87642 46131 +37947 38171 +17577 54357 +13994 48368 +70913 45734 +11985 23504 +82269 84225 +83126 70590 +47981 50609 +71785 10112 +38305 87857 +59774 78056 +62970 82069 +21547 84225 +63443 87640 +74676 92894 +54357 35752 +88456 38171 +80643 40334 +78727 73557 +86551 13789 +31360 70822 +38144 12422 +39925 26550 +18733 67964 +14557 23598 +68231 70590 +28923 37251 +36731 36089 +16989 57160 +10112 90976 +81269 32323 +46176 11467 +34165 74556 +30496 32262 +33260 70590 +91319 45070 +80484 38171 +61567 48368 +36103 70590 +45693 70590 +41025 43757 +36802 65596 +21523 98958 +80599 32262 +49397 92078 +74366 65439 +30292 94948 +20673 10112 +13158 46131 +89970 26550 +70825 12276 +96736 32750 +26698 17481 +94201 26049 +66228 18185 +50623 91018 +63509 70822 +96572 46131 +34380 46715 +21120 17481 +24159 56000 +59659 74556 +31572 53951 +48375 82069 +12985 36089 +82040 10557 +63252 57117 +31520 86581 +38262 13789 +41980 40901 +76273 47552 +11812 46131 +28759 77080 +12882 92653 +64935 33260 +26550 83813 +19156 58394 +93914 35101 +43129 96976 +13090 57117 +98463 76524 +36993 46131 +89249 35207 +12936 18458 +26497 56000 +75288 35237 +79742 82354 +65715 12422 +21544 29737 diff --git a/solutions/solutions.cabal b/solutions/solutions.cabal index d5e7ce3..36a0673 100644 --- a/solutions/solutions.cabal +++ b/solutions/solutions.cabal @@ -1113,3 +1113,10 @@ executable sln_2023_25 import: day main-is: 2023/25.hs build-depends: random, fgl + + +executable sln_2024_01 + import: day + main-is: 2024/01.hs + build-depends: containers + diff --git a/solutions/src/2024/01.hs b/solutions/src/2024/01.hs new file mode 100644 index 0000000..7e6676b --- /dev/null +++ b/solutions/src/2024/01.hs @@ -0,0 +1,48 @@ +{-# Language ImportQualifiedPost, QuasiQuotes #-} +{-| +Module : Main +Description : Day 1 solution +Copyright : (c) Eric Mertens, 2024 +License : ISC +Maintainer : emertens@gmail.com + + + +Find sum of pairwise distances between two sorted lists +and find sum of elements in first list multiplied by +frequences of that element in the second list. + +>>> :{ +:main + +"3 4 +4 3 +2 5 +1 3 +3 9 +3 3 +" +:} +11 +31 + +-} +module Main (main) where + +import Advent (counts, format) +import Data.List (sort) +import Data.Map qualified as Map + +-- | >>> :main +-- 1530215 +-- 26800609 +main :: IO () +main = + do (left, right) <- unzip <$> [format|2024 1 (%u %u%n)*|] + print (sum (zipWith distance (sort left) (sort right))) + + let tab = counts right + print (sum [x * Map.findWithDefault 0 x tab | x <- left]) + +-- | Absolute distance between two numbers. +distance :: Int -> Int -> Int +distance x y = abs (x - y)