From 5185e14653783ae40fd81e435c202de09760a13e Mon Sep 17 00:00:00 2001 From: Alex Selimov Date: Sun, 1 Dec 2024 23:03:19 -0500 Subject: [PATCH] Solve day 1 with Haskell --- day1/Part1.hs | 24 ++ day1/Part2.hs | 16 + day1/README.md | 21 + day1/day1.hs | 21 + day1/inputs.txt | 1000 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 1082 insertions(+) create mode 100644 day1/Part1.hs create mode 100644 day1/Part2.hs create mode 100644 day1/README.md create mode 100644 day1/day1.hs create mode 100644 day1/inputs.txt diff --git a/day1/Part1.hs b/day1/Part1.hs new file mode 100644 index 0000000..ce94094 --- /dev/null +++ b/day1/Part1.hs @@ -0,0 +1,24 @@ +module Part1 + ( parseFileString, + quickSort, + ) +where + +split :: [Int] -> ([Int], [Int]) +split mylist = case mylist of + [] -> ([], []) + x : xs -> + let (evens, odds) = split xs + in (x : odds, evens) + +parseFileString :: String -> ([Int], [Int]) +parseFileString file_string = split parsed_word_list + where + parsed_word_list = [read word :: Int | word <- words file_string] + +quickSort :: [Int] -> [Int] +quickSort [] = [] +quickSort (x : xs) = smallerSorted ++ [x] ++ biggerSorted + where + smallerSorted = quickSort [a | a <- xs, a <= x] + biggerSorted = quickSort [a | a <- xs, a > x] diff --git a/day1/Part2.hs b/day1/Part2.hs new file mode 100644 index 0000000..93f08ea --- /dev/null +++ b/day1/Part2.hs @@ -0,0 +1,16 @@ +module Part2 where + +import Data.Map qualified as Map + +getValueCounts :: [Int] -> Map.Map Int Int +getValueCounts [] = Map.empty +getValueCounts (x : xs) = + let xsMap = getValueCounts xs + newCount = Map.findWithDefault 0 x xsMap + 1 + in Map.insert x newCount xsMap + +getSimilarityScore :: [Int] -> Map.Map Int Int -> Int +getSimilarityScore [] map = 0 +getSimilarityScore (x : xs) map = xScore + getSimilarityScore xs map + where + xScore = x * Map.findWithDefault 0 x map diff --git a/day1/README.md b/day1/README.md new file mode 100644 index 0000000..30a2edd --- /dev/null +++ b/day1/README.md @@ -0,0 +1,21 @@ +# Day 1 + +## Instructions + +From this directory open a terminal and execute: + +```sh +runhaskell day1.hs +``` + +The solution for each section will be printed to the terminal output. + +## Notes + +This is my first attempt at writing Haskell code. +I've always wanted to try a formal functional programming language, and I've heard of Haskell through using tools like XMonad and Pandoc. +Since I use Pandoc regularly I thought Haskell would be a useful skill to have in case I ever need to contribute. +I also am interested in the functional programming paradigm and want to adapt my thought processes to write cleaner code. +I thought day 1 would be a good day to try Haskell, and something that would've taken me 10 minutes in any programming language I know really took me a long time. +This is definitely not good, clean, idiomatic Haskell, but it is a start! + diff --git a/day1/day1.hs b/day1/day1.hs new file mode 100644 index 0000000..e80d31a --- /dev/null +++ b/day1/day1.hs @@ -0,0 +1,21 @@ +import Part1 +import Part2 (getSimilarityScore, getValueCounts) +import System.IO + +main = do + inputs <- readFile "inputs.txt" + let (listA, listB) = parseFileString inputs + + -- Solve part 1 + let sortedListA = quickSort listA + let sortedListB = quickSort listB + let distances = [abs (a - b) | (a, b) <- zip sortedListA sortedListB] + let distance = sum distances + putStrLn "The solution to part 1 is: " + print distance + + -- Solve part 2 + let bMap = getValueCounts sortedListB + let score = getSimilarityScore sortedListA bMap + putStrLn "The solution to part 2 is: " + print score diff --git a/day1/inputs.txt b/day1/inputs.txt new file mode 100644 index 0000000..921687d --- /dev/null +++ b/day1/inputs.txt @@ -0,0 +1,1000 @@ +23238 26034 +94370 90190 +15509 72666 +48816 23909 +31300 40420 +14729 97519 +47167 21596 +18644 62370 +10058 89257 +15802 28645 +63557 90190 +66676 90412 +95224 92079 +20517 91403 +33109 13140 +58060 17075 +63986 35300 +92785 33038 +75525 55696 +91034 12712 +54945 85952 +70951 90190 +72666 33543 +19148 51672 +84872 54241 +44568 34382 +83462 63079 +75888 90582 +51807 47834 +71629 83895 +78782 92388 +15141 90190 +31753 44568 +33506 43240 +47083 26486 +38009 74762 +46783 42091 +59913 32597 +27337 56511 +84961 14925 +49804 93940 +79195 90534 +37742 34810 +38653 36830 +60692 21316 +34988 77576 +68550 56068 +59229 83297 +72846 80603 +28642 34506 +96975 38315 +76719 55715 +68212 11978 +49896 58929 +85899 72666 +65378 79253 +81584 98628 +57795 66296 +89138 37249 +24417 72666 +85907 44568 +33187 29605 +32132 47166 +61218 79101 +79906 17323 +75306 69897 +36113 20298 +19848 37536 +79974 53573 +82681 88411 +61356 72666 +67779 12835 +73747 32976 +43226 98628 +27418 42113 +81936 47166 +65390 72188 +86410 48512 +63152 89057 +83433 48642 +15154 49428 +67196 31091 +52145 66296 +97392 54967 +68822 89257 +95968 18999 +10231 48028 +75979 10758 +68791 71081 +73395 23710 +35116 61618 +75457 47834 +59013 74006 +60089 83297 +81215 72666 +85282 30857 +77169 72362 +77343 83136 +58477 47834 +93845 62570 +80320 36393 +25356 36515 +30605 14520 +41757 38101 +52096 63406 +37058 90190 +59797 79101 +65557 21111 +29895 62370 +76060 22181 +20451 88174 +98101 10985 +89542 16550 +35720 78549 +21328 90190 +78382 40764 +70594 72376 +44254 93643 +79324 84865 +61358 62370 +70753 70574 +19289 14760 +50375 77640 +55565 77975 +62697 88319 +97267 29605 +10937 83297 +45672 67842 +39846 62370 +55006 42276 +12817 55094 +25730 62370 +41309 45811 +79332 28406 +62494 71394 +40764 54500 +48573 10017 +32570 92974 +70630 79760 +33351 65490 +72008 71845 +99063 21041 +55424 44568 +54777 63064 +61905 72634 +81660 62370 +43650 92176 +98062 35629 +64890 32075 +84348 79253 +19809 15891 +17397 44325 +85724 79818 +50967 79253 +80513 21562 +90817 90582 +27451 84569 +97017 51807 +22724 65607 +61250 74083 +51613 82431 +92507 66296 +20838 67775 +21646 97601 +43227 42156 +54500 79253 +67589 79631 +68298 21800 +97081 13343 +49525 72376 +77455 89257 +49455 91182 +92689 77523 +10868 44679 +26724 47166 +94594 40764 +84697 79414 +14040 90751 +26930 62370 +62034 79253 +41654 62370 +72668 37285 +69857 97462 +89426 44374 +38354 20538 +68214 68822 +51210 38361 +60260 34047 +97778 35495 +64646 30634 +86706 85448 +71151 32417 +52064 84475 +92076 79101 +52941 65015 +83276 28661 +45040 52329 +24804 40692 +80127 71081 +75976 83297 +50856 54764 +98860 14310 +86849 68822 +55826 22807 +44426 84475 +18776 63854 +88579 41265 +86039 72666 +31003 79253 +38782 72839 +44759 40764 +49379 74859 +96296 22158 +52253 28645 +40895 79101 +11295 47834 +82274 36463 +75980 53984 +80045 90190 +24187 60596 +84264 60692 +39382 52329 +68621 67760 +78207 92176 +29605 89257 +82965 57643 +35473 32611 +76842 83297 +53704 69974 +18987 86774 +87307 55460 +13653 79101 +63448 47166 +82570 11369 +35219 79094 +78126 58440 +90509 42934 +67069 89709 +30317 83976 +59656 67961 +85165 47834 +87543 41626 +63866 28813 +96015 89257 +41635 40764 +58535 47166 +86065 53573 +59283 43240 +52899 68822 +93324 28248 +30465 29605 +90803 40764 +65604 72666 +41070 97917 +21201 32712 +66296 51560 +23241 18816 +49231 22376 +16090 71800 +60878 70919 +18216 86802 +31255 56222 +43385 43240 +27548 79253 +21800 47834 +90258 68822 +66198 41931 +77218 75490 +33929 46602 +63886 36805 +38478 28645 +75472 77934 +22622 47166 +68813 72376 +24242 67007 +56625 30429 +33816 69857 +99853 53573 +25244 99809 +58312 90190 +84081 72666 +18267 84356 +98628 57652 +70385 22850 +55944 49172 +34925 73021 +45979 61368 +69251 66296 +87383 52329 +40150 39790 +53132 65879 +11700 36363 +56444 44568 +96855 62674 +84569 66296 +10076 36393 +82102 77658 +34089 83297 +75465 16448 +96939 27252 +59547 44296 +17871 52665 +48300 85270 +99007 51807 +60501 72338 +54290 75136 +82299 76051 +19912 58398 +29748 28645 +94064 29440 +67058 66296 +96558 33145 +28211 60692 +83522 37461 +24984 31435 +73026 54947 +37704 83297 +45679 12243 +59293 96287 +70715 12977 +75300 92176 +47834 88660 +73179 44568 +15396 69857 +13155 45242 +15322 40764 +87738 44568 +39228 63056 +10094 99306 +19187 51279 +24608 66876 +64188 17378 +92176 34202 +27181 94666 +25568 21800 +23269 79101 +73632 83094 +76443 79253 +50395 40764 +95389 12857 +37621 76935 +98105 83297 +52329 70935 +22302 11041 +21359 68822 +85409 61115 +78562 66999 +80124 12495 +27657 85353 +21050 89257 +99720 24490 +34473 60692 +40097 28645 +71499 89052 +14699 66369 +39662 30699 +10126 90582 +70882 41277 +53822 88374 +26814 44568 +46613 19368 +60821 32241 +50555 48329 +47797 74633 +13233 84569 +82025 20968 +60368 92176 +83906 38286 +10747 61071 +55896 85143 +92268 95602 +69037 84475 +34685 46159 +19218 43240 +28324 72085 +11651 44568 +78958 66147 +17530 46029 +74555 44500 +77014 47834 +24738 47834 +16637 91276 +45665 79253 +92616 30393 +37952 59966 +19685 11968 +94702 29605 +41971 66296 +36214 79101 +56277 52329 +50495 55484 +12931 19137 +30115 27951 +43240 48011 +35685 28124 +83746 99263 +65419 44568 +24233 60692 +47241 55356 +35356 59649 +94202 43240 +79253 72376 +47166 33437 +91716 77323 +47182 44568 +53626 79101 +13563 61608 +48825 25123 +28824 84475 +90758 85547 +89417 90761 +22416 93637 +48389 40943 +38655 27039 +27191 19247 +79955 68822 +82905 40764 +86695 28645 +40972 66296 +45612 28645 +41731 21800 +61510 40764 +91192 98628 +15004 72376 +22943 54500 +68404 41433 +60311 44568 +60689 84252 +60979 43240 +19808 72495 +80126 38496 +13846 29383 +22482 13993 +39349 84569 +85531 37632 +80209 83851 +63852 56007 +54497 35634 +51625 87132 +38979 80241 +28939 88293 +59010 83649 +30553 26199 +94589 79101 +70488 99011 +81746 92176 +64311 44568 +88295 60692 +93665 89557 +53760 33880 +14737 80051 +25521 61141 +86653 98106 +91054 87401 +74929 40764 +20082 72374 +43069 87032 +95088 51436 +82111 80970 +39862 99504 +82669 46360 +83378 62370 +62130 25115 +43544 20178 +23808 89185 +98111 83928 +20879 43240 +49519 84475 +94944 85865 +75415 75964 +47156 36358 +27019 28645 +81852 52329 +83726 90190 +36393 96318 +65529 91634 +22850 28317 +12435 21800 +45103 82620 +54394 16838 +48022 53556 +52784 68553 +76933 84475 +66619 72666 +45083 10443 +85277 88103 +95818 90190 +23357 79253 +10551 63173 +86368 10170 +49946 12592 +73961 76042 +11496 47834 +15914 51997 +21677 66325 +24401 81589 +72493 50642 +72307 91265 +25787 80652 +56985 71081 +55531 44402 +59945 83297 +63295 83297 +20808 90190 +89697 47834 +65568 29693 +98707 28645 +62217 20682 +99249 48525 +13276 33527 +10427 46523 +69239 79253 +69378 67265 +64906 98628 +79879 42860 +12341 24113 +63649 28645 +93094 10616 +81490 28786 +13491 28645 +74076 93412 +54347 22101 +49315 90582 +56725 83297 +86924 56458 +62370 32885 +48988 72376 +42946 42505 +43680 95834 +62438 72376 +46412 79101 +97903 79101 +26320 47834 +69366 71081 +34171 68822 +30248 63980 +19619 40923 +47420 89257 +87678 57669 +61051 96558 +62807 12392 +30534 17024 +49199 67605 +48341 44568 +53638 58468 +12820 84475 +79439 72666 +80714 44568 +14366 84475 +21888 60692 +95972 59581 +63117 85887 +96782 72376 +71622 88794 +62648 62370 +21936 60167 +69675 39650 +88205 66550 +93488 93842 +28475 42399 +75331 25752 +88545 84475 +45032 57784 +97469 41573 +60842 78415 +19399 47166 +36862 22156 +44872 33736 +91430 41673 +28875 44568 +67699 94310 +69033 13525 +68804 47834 +35863 52441 +78678 99708 +68260 84487 +32978 60692 +46940 80397 +72663 74882 +21043 90582 +74375 17862 +96385 76273 +79101 53573 +76218 92278 +87477 43240 +58381 57987 +29149 78325 +16341 72376 +47020 52329 +65310 61228 +37754 83297 +77458 67510 +61915 23653 +36950 55786 +28645 34188 +35813 92176 +47172 66598 +90056 62370 +14736 85451 +89257 84569 +17180 34739 +66755 92176 +28028 29549 +78468 60692 +15102 60692 +17822 41158 +31218 66296 +41429 79101 +21620 72666 +57643 98463 +93661 22308 +97777 51057 +71356 33985 +55197 29232 +70795 55547 +71377 52329 +18251 92760 +49293 83737 +63584 43517 +32445 72666 +70614 68967 +47977 19618 +91379 69845 +17003 85894 +87325 99345 +12731 44568 +42313 70327 +36391 82618 +14548 47834 +80254 21800 +21669 90190 +54961 67621 +95444 79253 +84613 78529 +36436 62364 +62416 61914 +78479 43240 +31827 37146 +94738 81883 +97541 79253 +66855 29605 +70582 83297 +66034 32135 +33893 20394 +77467 68822 +37149 79166 +22676 28333 +92029 47166 +94520 70838 +45370 79253 +86953 50576 +12736 16934 +37014 41185 +95511 85166 +93629 61042 +95831 40730 +56049 80299 +29900 16094 +77032 47834 +88320 64538 +68300 23040 +82526 32180 +91345 62370 +85150 39035 +65519 44754 +10865 28011 +18592 83297 +19078 62370 +65128 83297 +29680 16423 +63907 60692 +52189 79101 +50834 68822 +48390 98628 +16835 43240 +16523 58031 +23750 72666 +93801 62698 +15455 85166 +39313 39154 +12246 29615 +57256 28645 +70719 80450 +93935 11251 +49629 43085 +68611 12734 +69656 44568 +30033 32331 +54145 10729 +25483 29605 +20736 27197 +70628 17513 +46547 79966 +34712 54500 +10856 47834 +40229 18590 +62932 79646 +34539 77892 +52528 92176 +98937 18544 +91209 71352 +18740 63353 +86629 70628 +64790 47041 +56551 79101 +83297 20038 +75130 56853 +33535 61978 +43492 14865 +51344 99384 +35260 24059 +84784 51807 +12409 16319 +16243 18062 +19891 46508 +35614 86985 +15122 68822 +43057 98628 +84820 60692 +12233 76084 +23335 65709 +71081 79101 +67869 62671 +49734 51807 +12696 83851 +15492 54500 +29208 62370 +24284 11417 +41157 47772 +30544 66296 +92226 17271 +61246 52329 +63227 96077 +31426 44568 +57047 79253 +23855 79520 +69439 27186 +85166 31655 +25322 62989 +14479 26112 +77140 28645 +10612 17077 +56543 83297 +53494 80908 +45334 95239 +89902 85166 +73255 57735 +26895 18173 +64218 47924 +78172 49084 +77332 43240 +17955 95261 +31704 37280 +83774 66296 +54943 83851 +65875 79101 +30695 72666 +25827 53304 +66127 92176 +16436 97614 +27869 48013 +54766 34900 +96521 83851 +39183 70628 +18834 35784 +73420 64200 +97485 45854 +50874 98017 +61477 58419 +52459 52329 +82878 79253 +18371 19152 +58631 54500 +92293 22682 +64034 28645 +75077 51807 +81292 45981 +81885 93158 +92300 36393 +97964 67294 +59096 69267 +48792 69248 +22288 94108 +18619 68822 +17920 27815 +89408 72666 +51222 92339 +33457 90190 +79548 68822 +16841 17911 +61683 77353 +58205 39848 +43788 38588 +84475 84475 +74053 10266 +11537 93796 +91987 89494 +19804 40764 +35138 83851 +88532 62479 +21972 20395 +87380 16280 +69296 83851 +78721 85856 +85819 47166 +80119 11244 +96949 13944 +10638 97010 +12213 49984 +90582 66296 +84153 69786 +52703 14947 +82734 44321 +31346 65022 +30574 56778 +48416 38908 +71402 46947 +17158 22018 +40254 82756 +24823 68567 +36467 52987 +38460 90190 +28032 71264 +90853 58391 +93193 72666 +90190 28645 +35742 90190 +64472 95923 +45785 79101 +84235 51774 +20337 21528 +71921 28645 +51844 44536 +93894 84475 +91414 15256 +23884 66296 +57314 28319 +66738 90958 +72376 78771 +59616 21810 +99956 40120 +63926 51298 +53573 25383 +83851 13307 +62052 37219 +18954 54500 +19984 67636 +60119 33810 +12147 20266 +31871 79253 +55040 90217 +23706 83367 +17851 83851 +85520 83297 +38471 40983 +64357 94963 +82719 28371 +55216 56394 +98567 47834 +58865 50989 +84116 10207 +94278 12968 +31912 32099 +14886 44568 +75820 60692 +37985 49205 +66024 31579 +40690 72376 +97296 84475 +26537 64899 +23676 72995 +19014 84475 +93618 67504 +22886 44568 +42999 43240 +97005 40764 +49168 84388 +32187 29605 +55580 35405 +47138 81256 +65134 72988 +70225 28645 +32247 41914 +77382 69857 +85910 89257 +24055 67072 +49487 11022 +70897 18278 +96846 83297 +26249 22533 +31913 72086 +13340 88560 +81511 66296 +57973 36393 +26791 89257 +41155 36393 +11531 65438 +70981 41334 +19021 60692 +19123 84569 +15215 57592 +51944 14684 +42510 72666 +14313 84475 +75686 94789 +37398 64138 +80968 49372 +13423 40764 +51784 21800 +16771 79101 +46751 29605 +32177 39427 +91885 68822 +29436 84475 +37793 40764 +18527 69857 +83464 77792 +44411 54500 +30554 29550 +55958 51807 +97160 23416 +12673 62370 +72021 65743 +86272 27177 +58595 90582 +23628 10375 +79261 72666 +34730 61458 +32101 49340 +91964 19525 +60149 92176 +96421 36393 +73535 81729 +75343 60692 +48638 28844 +33746 66296 +57163 64173 +40694 25667 +17656 12560 +25674 81321 +46631 79101 +31088 79101 +16025 15602 +90344 93010 +83502 89257 +72506 97934 +67982 92119 +53274 20232 +81581 15775 +61634 92176 +34465 76620 +39738 76121 +29929 34694 +63062 53509 +61325 19831 +95180 32789 +76828 81537 +16895 72666 +42407 31971 +38627 66296 +49392 53573 +10342 73936 +81470 19476 +79948 73681 +80798 67908 +48113 26473 +27504 66296 +63214 72376 +30224 23096 +63112 69857 +47829 66296 +32412 32329 +52231 72376 +30379 62620 +80100 29605 +74262 90582 +14634 52329 +41091 97452 +32482 31278 +44499 83851 +12050 85447 +93469 21599 +73286 38522 +88923 81268 +20527 29605 +83959 57974 +92046 83593 +67939 99783 +42239 53315 +56584 16398 +70237 21652 +50928 90582 +77632 88216 +59573 94988 +94450 36393 +43063 91467 +95915 28645 +18977 63414 +59673 96558 +73123 12635