From b0966e5b391ef0f0f2ef1491c98358e58f2cfe4b Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sun, 1 Dec 2024 22:06:10 -0500 Subject: [PATCH] Advent of Code 2024, Day 1 --- 2024/Cargo.lock | 7 + 2024/Cargo.toml | 6 + 2024/data/day1.txt | 1000 ++++++++++++++++++++++++++++++++++++++++++++ 2024/src/day1.rs | 157 +++++++ 2024/src/main.rs | 13 + rust-toolchain | 3 +- 6 files changed, 1184 insertions(+), 2 deletions(-) create mode 100644 2024/Cargo.lock create mode 100644 2024/Cargo.toml create mode 100644 2024/data/day1.txt create mode 100644 2024/src/day1.rs create mode 100644 2024/src/main.rs diff --git a/2024/Cargo.lock b/2024/Cargo.lock new file mode 100644 index 0000000..45e8feb --- /dev/null +++ b/2024/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aoc-2024" +version = "0.1.0" diff --git a/2024/Cargo.toml b/2024/Cargo.toml new file mode 100644 index 0000000..d7b7677 --- /dev/null +++ b/2024/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "aoc-2024" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/2024/data/day1.txt b/2024/data/day1.txt new file mode 100644 index 0000000..e24cefb --- /dev/null +++ b/2024/data/day1.txt @@ -0,0 +1,1000 @@ +84283 63343 +35360 98209 +17841 84541 +22035 44413 +43645 22440 +45290 95042 +47251 69434 +39300 96178 +19278 89783 +85842 65775 +35833 86545 +83505 46651 +84748 99338 +89475 33222 +42161 58577 +44380 60478 +71528 39698 +70514 65226 +44413 43676 +33787 32152 +90737 63686 +37294 68183 +75263 59626 +60662 72213 +84705 69756 +21217 99901 +72906 34754 +93885 69920 +95413 90232 +15097 99343 +51861 30403 +86779 81128 +89053 71770 +91473 84748 +42848 83319 +75389 16660 +79784 77290 +93069 79793 +47264 51397 +27695 54288 +92063 39301 +39085 15346 +51201 25788 +17981 36723 +32685 56814 +98625 47590 +69815 20602 +93402 99803 +72880 15499 +97907 62268 +76481 85794 +44842 69492 +80295 16660 +97958 94756 +89545 16660 +73090 77911 +72433 16911 +85972 27192 +10582 69368 +22334 16852 +43360 63474 +79393 86545 +42302 14040 +61177 58936 +49655 42875 +72005 33787 +58727 43007 +37514 33044 +99443 33020 +52137 58953 +49038 71779 +94756 37855 +62268 44413 +27702 33097 +98521 65775 +61583 12043 +22777 65011 +18614 58936 +38328 73771 +15249 75263 +33863 40144 +46651 46651 +61084 82666 +39251 68710 +22781 42030 +53976 41887 +35530 66749 +25493 10759 +20353 17588 +84109 65785 +83893 32822 +30896 10671 +97083 33787 +73752 61585 +67790 86677 +79260 34669 +65897 82748 +44296 15919 +21934 96178 +77571 39195 +56142 75263 +57117 97137 +23301 98209 +73163 86545 +91882 82221 +95331 31680 +59289 77212 +15754 71947 +46398 88874 +10667 15893 +69601 15499 +11225 22506 +98793 43676 +97146 65775 +86898 27456 +50178 43676 +19563 96178 +37236 83319 +48110 15099 +87813 83628 +45147 28949 +18308 15320 +83455 62844 +31167 94756 +57734 78110 +28433 71843 +17114 50003 +30689 83319 +32142 84973 +75022 97137 +37954 58936 +87682 71077 +75831 51397 +15774 31580 +97448 70574 +11672 91230 +65732 47962 +42222 62092 +80590 69230 +58007 43676 +51361 81128 +77212 95485 +97079 80445 +14347 51648 +60311 82950 +91345 94033 +98757 43676 +17921 10573 +66116 44413 +51094 90107 +67373 80341 +79628 16066 +69987 96086 +27839 76671 +18904 76136 +87664 15499 +34757 65775 +59509 77212 +36746 29953 +49422 90054 +15204 94756 +76707 10470 +57263 81128 +24221 15499 +66460 34853 +26979 24071 +50113 51397 +27576 18881 +10024 58690 +94266 44338 +53098 15499 +49708 26678 +45448 14024 +71075 86545 +70967 16660 +79261 17794 +10368 47000 +52272 44413 +99474 63493 +62001 62268 +13025 58936 +44750 31131 +53413 78110 +29540 86516 +67424 93231 +19550 58936 +47390 36675 +26625 78110 +53218 44473 +96391 67378 +99990 86545 +15308 36723 +65730 71355 +65789 52609 +87805 42937 +29236 78110 +31771 94756 +18987 59217 +64300 10605 +78147 86545 +48116 86545 +92646 31914 +65185 75151 +19530 78110 +52051 95575 +85748 46024 +79421 44413 +80422 93619 +29790 89448 +55576 47590 +49121 38714 +72333 55404 +65315 65775 +90550 75308 +31131 70078 +24074 84512 +25162 11398 +75109 71691 +43749 47992 +75832 80252 +65214 63054 +39525 97845 +63454 51633 +51736 56796 +35556 54493 +21880 78110 +65295 85023 +92624 63420 +40519 60621 +17223 26035 +58997 82029 +96291 43676 +18878 17802 +83923 86545 +39873 52247 +11656 18538 +70458 98474 +84033 62268 +39535 40947 +32254 83944 +61992 45922 +29791 65458 +54348 50197 +19270 33788 +85744 13744 +96599 86043 +30579 51397 +45856 97022 +50586 39670 +23436 21616 +60158 69962 +60361 46651 +78825 65775 +10426 94756 +96035 97582 +15499 52076 +20954 57704 +22388 51397 +81163 58840 +91634 62284 +32022 60129 +91628 83319 +62766 39021 +69120 52720 +90633 86545 +10409 56735 +12777 48223 +34714 75263 +79836 42421 +10520 75263 +62340 75043 +39720 26790 +46519 39888 +61520 65775 +33304 22157 +36117 47033 +70466 26469 +64757 58936 +21216 65775 +18545 47590 +93089 12789 +93798 86967 +82130 93561 +25802 82666 +84789 53152 +40909 51397 +71607 63580 +58121 46248 +96176 83150 +57256 56877 +45369 81128 +14089 80671 +62274 58936 +93696 30843 +87893 11107 +70203 33787 +64881 41883 +25012 88519 +61533 26411 +42375 15214 +71157 31131 +83004 53943 +91731 20072 +80380 32606 +84931 93116 +84992 82666 +18938 85274 +53302 66304 +54031 25703 +53733 82087 +70053 16035 +94922 46651 +51039 15775 +95907 25691 +48755 86545 +71863 52366 +26925 57806 +78898 70700 +42515 82666 +73641 13915 +76761 68975 +81003 64780 +78110 93231 +43324 32347 +21609 82998 +82606 47590 +81882 81455 +33483 15499 +94856 96178 +83072 91213 +95776 15752 +49704 67234 +38311 59510 +18526 73415 +34486 49323 +86291 95736 +48231 48723 +58843 94756 +39909 96229 +48028 97137 +56310 29503 +99185 24905 +17336 82666 +92522 85585 +13402 73819 +32705 81018 +48232 69309 +57780 67505 +81213 58936 +98451 82666 +99334 65775 +20346 83118 +23437 66474 +10188 47428 +36257 54877 +56471 83319 +16660 96178 +67004 82666 +42043 25942 +73986 23575 +22940 75135 +14788 90530 +17351 83319 +93467 47590 +45821 68349 +50930 64681 +97634 35360 +43212 31098 +74266 69116 +54839 31433 +21639 25809 +46195 44054 +51737 84748 +42702 86545 +33691 77212 +27068 16406 +82599 15400 +39389 67976 +20089 57521 +43676 38333 +40624 36299 +69489 65775 +57102 86545 +85599 98399 +28855 77212 +61910 71149 +72866 85822 +89253 93951 +92040 90428 +79472 18069 +86424 58936 +37352 44413 +85210 94311 +89903 78875 +80026 60474 +88961 99820 +38159 81128 +35620 38529 +93003 52573 +48646 58936 +44325 13961 +82076 24493 +74069 81128 +18067 28571 +99684 10225 +21031 82452 +90638 86545 +57199 31313 +82205 13207 +71149 73214 +58191 81342 +47303 65775 +36022 37220 +72656 49204 +55308 53561 +19095 65493 +46635 71555 +15506 64319 +40226 49581 +25646 79607 +44326 44382 +74259 78110 +83125 44570 +85377 51736 +49474 81128 +77096 66424 +32334 82666 +88697 91300 +13865 11138 +48113 75493 +15017 81128 +61694 93132 +27654 65809 +79058 86545 +32035 84748 +20212 22750 +51434 35768 +89681 75874 +38356 91593 +40535 30523 +35913 51397 +20493 78110 +65172 84035 +20026 74404 +74410 30968 +63206 31719 +39064 41786 +57312 85504 +42601 93207 +56746 31131 +13866 83100 +56073 86545 +89828 68239 +43617 80074 +68234 40950 +17714 96178 +91242 93460 +61907 51622 +45542 54328 +56287 98209 +33780 54835 +81704 17337 +95069 45562 +54245 83519 +10776 92714 +15551 83089 +47590 46303 +71220 47329 +20160 28184 +93991 88224 +59450 97215 +71333 84428 +42520 48130 +54366 15499 +31338 44103 +94941 86108 +13396 81924 +74187 93231 +10131 47816 +34774 44413 +36723 91465 +45677 34269 +13499 35048 +15009 58120 +84895 90143 +74165 83319 +18259 46651 +98209 87849 +80104 96178 +69709 96178 +68814 62370 +97541 29548 +73729 39436 +72978 62854 +58000 84748 +63119 81674 +40369 44413 +90150 95171 +52608 92615 +71077 38291 +31624 93044 +38760 77212 +91176 58936 +63143 40887 +91823 16660 +34818 44413 +59892 11840 +17484 36429 +30905 54199 +32038 17281 +36181 79577 +61442 58936 +52676 92055 +84219 94756 +66449 58936 +22863 73911 +23017 89669 +79892 94756 +82803 51397 +83319 65016 +30234 42381 +73499 45602 +46440 44587 +95631 44461 +81128 76437 +74448 46651 +14447 52123 +32611 24331 +57781 22177 +64820 65775 +68420 84266 +47754 67988 +34335 71149 +29717 64219 +89191 64088 +82795 57333 +16549 22484 +82556 77514 +99163 23617 +18471 93880 +56195 44413 +89300 85985 +46990 17063 +81806 15499 +38774 57907 +75955 45074 +56634 53662 +14693 31131 +53016 52394 +73961 91020 +62726 10077 +73112 92012 +95820 81782 +89033 43179 +18786 68422 +37161 54313 +65250 96178 +37464 43676 +86725 79195 +85011 26319 +67737 69409 +98362 54308 +34884 44086 +32918 45910 +44619 52487 +80495 81128 +55837 51397 +60689 40038 +44176 54195 +39854 12679 +13045 94621 +40466 56205 +97137 77870 +19527 69865 +88017 39180 +31622 21522 +79812 11165 +55320 85476 +99675 95829 +10226 39512 +58609 56835 +92126 77212 +85290 36723 +79783 51199 +93581 47590 +90551 41001 +40558 11266 +39089 24707 +32385 66290 +16747 34912 +20505 78110 +70730 52705 +48459 15541 +66724 82666 +26340 71149 +11972 70352 +85523 91638 +76588 87532 +15882 85803 +84892 71074 +17238 46651 +72970 40619 +63431 71149 +90427 85393 +57232 69012 +64933 35643 +56557 86545 +89980 84682 +39968 59407 +22733 20816 +64872 58936 +58936 26119 +75725 37733 +28054 15499 +72081 31664 +25950 98957 +35455 33324 +61813 15499 +41704 25493 +79271 86494 +23953 51653 +46535 41106 +63102 96797 +30834 94721 +64012 91509 +68305 51458 +94295 72833 +40622 63182 +91004 79623 +64037 88188 +11981 71149 +94012 17064 +78178 71465 +61342 12008 +46974 58318 +57688 78663 +64308 23440 +44044 46651 +36812 12527 +46898 96178 +37454 45987 +96884 43676 +52528 43676 +56661 72213 +91311 58936 +94931 38918 +87647 17588 +57203 46651 +21346 42051 +61629 21742 +93631 48556 +81359 34333 +66314 90524 +13411 62864 +15431 82666 +62759 80050 +84813 65775 +71791 51397 +83838 16358 +90129 99706 +98160 31885 +33794 77212 +71088 93454 +85754 75521 +85337 36180 +54712 43676 +43618 58936 +25861 51397 +59485 84700 +20950 98209 +59143 36723 +19018 96178 +99973 62268 +35822 38826 +31306 99043 +92941 31131 +92804 77212 +62182 15499 +93200 39106 +79238 66201 +56028 97137 +26131 41721 +55412 86233 +40092 64847 +89193 97749 +84584 44413 +58395 69675 +70934 42497 +27303 24086 +41447 95939 +15725 64142 +69760 58936 +81266 53614 +95326 57234 +89236 97137 +45408 99439 +65803 58102 +23823 89352 +82448 83319 +12317 15499 +63704 31229 +31591 46651 +60656 40668 +84397 86629 +15943 71628 +69553 40846 +67670 20397 +29623 41118 +45656 11366 +57563 57937 +35401 33787 +29524 92772 +10799 77212 +20503 81128 +31145 97137 +24662 17588 +88787 42052 +77284 27806 +81111 13760 +96401 77212 +45881 84748 +14096 91121 +31716 44830 +96178 97137 +11460 79257 +22663 64393 +17066 53398 +48248 17099 +47906 13089 +60675 18687 +14382 86545 +62222 70385 +99668 63170 +16029 71024 +20810 82413 +11971 63222 +70446 98209 +95100 19344 +81776 14882 +56409 90152 +78955 66877 +15972 35794 +82762 57101 +40585 43676 +82715 32513 +13035 12929 +41208 43676 +75405 51397 +52968 37848 +89777 77408 +47910 41390 +17844 92716 +78842 71149 +96933 78110 +84401 86308 +22335 83319 +38772 39043 +10235 91837 +64002 91515 +97099 72526 +19165 32320 +94851 53321 +11044 81516 +30088 31131 +92285 19663 +90624 43676 +54999 90850 +56563 61111 +93006 42621 +77028 44413 +63496 93231 +59288 16304 +19260 82666 +65456 78110 +76020 47590 +71510 78110 +38373 15499 +95329 77350 +81785 15499 +73967 65024 +55970 81128 +75253 51397 +67687 97112 +26178 22355 +31644 43676 +85675 51397 +98065 65775 +47206 22445 +68588 67760 +81859 34895 +81781 96369 +12100 17237 +76959 12327 +62721 92743 +47728 82465 +80148 57620 +36073 81128 +77828 43217 +60402 51397 +20533 67838 +67535 65775 +86024 93819 +45988 74424 +79733 90048 +36916 67049 +91726 78110 +89614 77956 +91991 61361 +48407 44413 +41683 34105 +61197 72068 +23055 75738 +76099 31131 +34782 29960 +79495 96116 +56038 77059 +75701 79362 +90211 35325 +91105 59877 +26571 55619 +70643 23825 +96461 86872 +97211 33461 +83364 41646 +37258 39006 +76258 43478 +77897 78110 +90589 15499 +53899 94756 +59814 91161 +99348 15499 +45978 88916 +21550 44413 +28503 44413 +70361 93856 +38176 77212 +27681 42058 +34616 32695 +63530 77048 +93477 33487 +38551 85570 +30281 56360 +85761 86914 +54185 81927 +34284 21499 +90875 28296 +50159 22467 +73017 44413 +70427 75581 +13241 73319 +43718 84796 +58491 55625 +61676 44413 +59141 99602 +68941 47529 +99683 74769 +11312 96178 +12988 13264 +58625 54979 +38011 19401 +31122 43676 +72187 60980 +83040 16660 +35538 72268 +26926 14779 +20473 71077 +65791 97137 +90431 75263 +44240 65302 +68127 31131 +88606 59555 +82666 84748 +63367 65186 +66772 36723 +34590 91525 +10183 17103 +54013 72306 +88038 86803 +85275 51830 +29557 85444 +91185 61242 +91026 98854 +47846 50418 +58439 13040 +17800 79381 +50614 23120 +20747 99140 +50825 19555 +92963 46049 +86545 64971 +32892 59315 +41415 49777 +52966 15575 +94229 46570 +24717 15057 +90893 73249 +91788 31131 +20609 58936 +83204 92333 +25338 93231 +51397 58936 +93636 88577 +37898 88541 +93231 74087 +74840 87061 +50972 56483 +24441 62016 +45678 16300 +74739 43676 +40860 67689 +30257 10504 +62026 74316 +55422 82666 +78640 31557 +34047 41209 +72688 25384 +71214 58936 +29061 25473 +81733 25169 +30705 54797 +45282 97408 +70052 68168 +85605 83319 +67848 15499 +29579 11173 +21750 37393 +44657 35203 +14712 43246 +66229 51397 +69289 43240 +74346 78110 +63305 48302 +35229 43676 +99323 96098 +17588 81128 +17774 30312 +89787 71712 +41204 73470 +62131 77212 +43849 47590 +39529 83224 +40983 34788 +24893 43702 +45386 94505 +56318 41455 +68530 55855 +25873 42309 +94040 79923 +36153 86545 +88010 54142 +79130 78110 +71226 10679 +29354 70963 +87872 74238 +27909 65520 +99432 27102 +10868 48012 +26181 51794 +11684 81810 +93130 78110 +63372 55575 +78345 69499 +75999 96178 +87508 35360 +79511 85378 +51818 64013 +79942 38589 +84855 88979 +87913 96178 +47101 12601 +50727 16660 +20430 78110 +74052 10988 +63972 58139 +32279 32116 +83960 82666 +57325 44413 +91674 17561 +78735 86390 +72213 15499 +65775 74982 +78046 31131 +92992 19498 +61673 58879 +90012 91669 +73742 23296 +64602 10252 +34277 19600 +38771 62538 +83906 75263 +54798 39169 +48992 86743 +39017 27160 +17400 97272 +48587 46651 +37271 78110 +59764 98477 +63559 55088 +21472 92869 +70180 11171 diff --git a/2024/src/day1.rs b/2024/src/day1.rs new file mode 100644 index 0000000..643c067 --- /dev/null +++ b/2024/src/day1.rs @@ -0,0 +1,157 @@ +use std::collections::HashMap; + +const INPUT: &'static str = include_str!("../data/day1.txt"); + +pub fn day1a() -> String { + let (left_list, right_list) = parse_lists(INPUT); + + let left_list = sort_list_asc(left_list); + let right_list = sort_list_asc(right_list); + + let zipped = zip_lists(left_list, right_list); + + format!("{}", total_distance(zipped)) +} + +pub fn day1b() -> String { + let (left_list, right_list) = parse_lists(INPUT); + format!("{}", total_similarity(left_list, right_list)) +} + +fn total_distance(pairs: Vec<(i32, i32)>) -> i32 { + pairs.into_iter().fold(0, |acc, pair| acc + distance(pair)) +} + +fn total_similarity(left_list: Vec, right_list: Vec) -> i32 { + let left_frequency = frequency_count(left_list); + let right_frequency = frequency_count(right_list); + + let mut similarity = 0; + + for (key, count) in left_frequency.into_iter() { + let right_occurences = right_frequency.get(&key).cloned().unwrap_or(0); + + println!("[{}] {} * {}", key, count, right_occurences); + + similarity += key * count * right_occurences; + } + + similarity +} + +fn distance(pair: (i32, i32)) -> i32 { + (pair.0 - pair.1).abs() +} + +fn parse_lists(input: &str) -> (Vec, Vec) { + let mut left_list = vec![]; + let mut right_list = vec![]; + + input.lines().for_each(|text| { + let values = text.split(' ').filter(|t| !t.is_empty()).map(|t| t.parse::().unwrap()).collect::>(); + left_list.push(values[0]); + right_list.push(values[1]); + }); + + (left_list, right_list) +} + +fn sort_list_asc(mut input: Vec) -> Vec { + input.sort(); + input +} + +fn zip_lists(left: Vec, right: Vec) -> Vec<(i32, i32)> { + left.into_iter().zip(right).collect::>() +} + +type FrequencyCount = HashMap; + +fn frequency_count(lst: Vec) -> FrequencyCount { + let mut cnt = HashMap::new(); + + for number in lst { + cnt.entry(number).and_modify(|e| {*e += 1; }).or_insert(1); + } + + cnt +} + +#[cfg(test)] +mod test { + use super::*; + + const TEST_INPUT: &'static str = "3 4 +4 3 +2 5 +1 3 +3 9 +3 3"; + + #[test] + fn it_figures_out_smallest_numbers() { + let (left_list, right_list) = parse_lists(TEST_INPUT); + + assert_eq!(left_list.len(), 6); + assert_eq!(right_list.len(), 6); + + assert_eq!(left_list, vec![3, 4, 2, 1, 3, 3]); + assert_eq!(right_list, vec![4, 3, 5, 3, 9, 3]); + + let left_list = sort_list_asc(left_list); + let right_list = sort_list_asc(right_list); + + assert_eq!(left_list[0], 1); + assert_eq!(right_list[0], 3); + } + + #[test] + fn it_zips_two_lists() { + let (left_list, right_list) = parse_lists(TEST_INPUT); + + let left_list = sort_list_asc(left_list); + let right_list = sort_list_asc(right_list); + + let zipped = zip_lists(left_list, right_list); + + assert_eq!(zipped[0], (1, 3)); + assert_eq!(zipped[1], (2, 3)); + assert_eq!(zipped[2], (3, 3)); + assert_eq!(zipped[3], (3, 4)); + assert_eq!(zipped[4], (3, 5)); + assert_eq!(zipped[5], (4, 9)); + } + + #[test] + fn it_finds_difference_between_pairs() { + let (left_list, right_list) = parse_lists(TEST_INPUT); + + let left_list = sort_list_asc(left_list); + let right_list = sort_list_asc(right_list); + + let zipped = zip_lists(left_list, right_list); + + assert_eq!(distance(zipped[0]), 2); + assert_eq!(distance(zipped[1]), 1); + + assert_eq!(total_distance(zipped), 11); + } + + #[test] + fn it_counts_frequencies() { + let (left_list, _right_list) = parse_lists(TEST_INPUT); + + let frequencies = frequency_count(left_list); + + assert_eq!(frequencies.get(&1), Some(&1)); + assert_eq!(frequencies.get(&2), Some(&1)); + assert_eq!(frequencies.get(&3), Some(&3)); + assert_eq!(frequencies.get(&4), Some(&1)); + } + + #[test] + fn it_calculates_total_similarity() { + let (left_list, right_list) = parse_lists(TEST_INPUT); + assert_eq!(total_similarity(left_list, right_list), 31); + } +} diff --git a/2024/src/main.rs b/2024/src/main.rs new file mode 100644 index 0000000..4f33f0b --- /dev/null +++ b/2024/src/main.rs @@ -0,0 +1,13 @@ +mod day1; + +fn main() { + let day = std::env::args().skip(1).next(); + + let result = match day.as_ref().map(|v| v.as_ref()) { + Some("1a") => day1::day1a(), + Some("1b") => day1::day1b(), + _ => panic!("unrecognized day"), + }; + + println!("[{}] {}", day.unwrap(), result); +} diff --git a/rust-toolchain b/rust-toolchain index 3e044fb..1de01fa 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1,3 +1,2 @@ [toolchain] -channel = "1.73.0" -targets = [ "wasm32-unknown-unknown" ] +channel = "1.81.0"