commit 17b95068da624f1a56ea149a3b0a82b59b8261ef Author: Martin Berg Alstad Date: Mon Dec 2 18:59:57 2024 +0100 Day1 finished diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a2b918c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +# Build +/target + +# IDE +.idea \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..b9bddce --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,11 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "advent_of_code_2024" +version = "0.1.0" + +[[package]] +name = "day1" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..6f0eb98 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[workspace] +members = ["./day*"] + +[package] +name = "advent_of_code_2024" +version = "0.1.0" +edition = "2021" + +[lib] + +[workspace.dependencies] diff --git a/day1/Cargo.toml b/day1/Cargo.toml new file mode 100644 index 0000000..fb6b7ec --- /dev/null +++ b/day1/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day1" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/day1/src/common.rs b/day1/src/common.rs new file mode 100644 index 0000000..8a0605c --- /dev/null +++ b/day1/src/common.rs @@ -0,0 +1,22 @@ +fn get_raw_text() -> &'static str { + include_str!("./input.txt") +} + +pub(crate) fn parse_input_file() -> (Vec, Vec) { + get_raw_text() + .split("\n") + .map(|line| { + let array: Vec<_> = line.split(" ").filter(|el| !el.is_empty()).collect(); + ( + array + .first() + .and_then(|value| value.parse::().ok()) + .unwrap(), + array + .last() + .and_then(|value| value.parse::().ok()) + .unwrap(), + ) + }) + .collect() +} diff --git a/day1/src/day1.md b/day1/src/day1.md new file mode 100644 index 0000000..f473e9c --- /dev/null +++ b/day1/src/day1.md @@ -0,0 +1,79 @@ + +--- Day 1: Historian Hysteria --- + +The Chief Historian is always present for the big Christmas sleigh launch, but nobody has seen him in months! Last anyone heard, he was visiting locations that are historically significant to the North Pole; a group of Senior Historians has asked you to accompany them as they check the places they think he was most likely to visit. + +As each location is checked, they will mark it on their list with a star. They figure the Chief Historian must be in one of the first fifty places they'll look, so in order to save Christmas, you need to help them get fifty stars on their list before Santa takes off on December 25th. + +Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck! + +You haven't even left yet and the group of Elvish Senior Historians has already hit a problem: their list of locations to check is currently empty. Eventually, someone decides that the best place to check first would be the Chief Historian's office. + +Upon pouring into the office, everyone confirms that the Chief Historian is indeed nowhere to be found. Instead, the Elves discover an assortment of notes and lists of historically significant locations! This seems to be the planning the Chief Historian was doing before he left. Perhaps these notes can be used to determine which locations to search? + +Throughout the Chief's office, the historically significant locations are listed not by name but by a unique number called the location ID. To make sure they don't miss anything, The Historians split into two groups, each searching the office and trying to create their own complete list of location IDs. + +There's just one problem: by holding the two lists up side by side (your puzzle input), it quickly becomes clear that the lists aren't very similar. Maybe you can help The Historians reconcile their lists? + +For example: + +3 4 +4 3 +2 5 +1 3 +3 9 +3 3 + +Maybe the lists are only off by a small amount! To find out, pair up the numbers and measure how far apart they are. Pair up the smallest number in the left list with the smallest number in the right list, then the second-smallest left number with the second-smallest right number, and so on. + +Within each pair, figure out how far apart the two numbers are; you'll need to add up all of those distances. For example, if you pair up a 3 from the left list with a 7 from the right list, the distance apart is 4; if you pair up a 9 with a 3, the distance apart is 6. + +In the example list above, the pairs and distances would be as follows: + + The smallest number in the left list is 1, and the smallest number in the right list is 3. The distance between them is 2. + The second-smallest number in the left list is 2, and the second-smallest number in the right list is another 3. The distance between them is 1. + The third-smallest number in both lists is 3, so the distance between them is 0. + The next numbers to pair up are 3 and 4, a distance of 1. + The fifth-smallest numbers in each list are 3 and 5, a distance of 2. + Finally, the largest number in the left list is 4, while the largest number in the right list is 9; these are a distance 5 apart. + +To find the total distance between the left list and the right list, add up the distances between all the pairs you found. In the example above, this is 2 + 1 + 0 + 1 + 2 + 5, a total distance of 11! + +Your actual left and right lists contain many location IDs. What is the total distance between your lists? + +Your puzzle answer was 1189304. +--- Part Two --- + +Your analysis only confirmed what everyone feared: the two lists of location IDs are indeed very different. + +Or are they? + +The Historians can't agree on which group made the mistakes or how to read most of the Chief's handwriting, but in the commotion you notice an interesting detail: a lot of location IDs appear in both lists! Maybe the other numbers aren't location IDs at all but rather misinterpreted handwriting. + +This time, you'll need to figure out exactly how often each number from the left list appears in the right list. Calculate a total similarity score by adding up each number in the left list after multiplying it by the number of times that number appears in the right list. + +Here are the same example lists again: + +3 4 +4 3 +2 5 +1 3 +3 9 +3 3 + +For these example lists, here is the process of finding the similarity score: + + The first number in the left list is 3. It appears in the right list three times, so the similarity score increases by 3 * 3 = 9. + The second number in the left list is 4. It appears in the right list once, so the similarity score increases by 4 * 1 = 4. + The third number in the left list is 2. It does not appear in the right list, so the similarity score does not increase (2 * 0 = 0). + The fourth number, 1, also does not appear in the right list. + The fifth number, 3, appears in the right list three times; the similarity score increases by 9. + The last number, 3, appears in the right list three times; the similarity score again increases by 9. + +So, for these example lists, the similarity score at the end of this process is 31 (9 + 4 + 0 + 0 + 9 + 9). + +Once again consider your left and right lists. What is their similarity score? + +Your puzzle answer was 24349736. + +Both parts of this puzzle are complete! They provide two gold stars: ** diff --git a/day1/src/input.txt b/day1/src/input.txt new file mode 100644 index 0000000..b32283a --- /dev/null +++ b/day1/src/input.txt @@ -0,0 +1,1000 @@ +58789 28882 +27059 23721 +86784 91527 +72958 13217 +95228 20832 +77437 82573 +33685 76537 +99368 67870 +41094 34696 +70702 18298 +67914 76275 +60325 84734 +25043 31692 +59623 47605 +57390 23721 +68803 36898 +24268 14581 +65399 63078 +97762 19390 +50114 28539 +88373 35451 +49024 61813 +96953 63691 +43964 50579 +84426 60331 +76079 13172 +22861 38715 +91048 87550 +74780 34297 +49960 72835 +91972 50079 +52862 63078 +62546 78927 +92384 70747 +80234 20439 +98465 98833 +34696 26663 +84984 85013 +32047 94866 +20926 91132 +79049 79638 +61387 75742 +40584 67646 +92492 99117 +37042 13172 +24478 21558 +10848 51254 +96010 50300 +21014 29228 +89706 80794 +51308 21014 +87493 91132 +20912 77537 +27054 39926 +57804 54388 +47423 87284 +68652 84795 +86295 42891 +49216 80777 +25463 20439 +20700 33848 +48643 41770 +73701 35843 +81231 45686 +43227 45195 +22496 63694 +98833 70485 +54707 30431 +24668 21558 +69702 90163 +82025 33804 +40061 11701 +16121 91132 +64440 22580 +79108 44508 +51027 21558 +68719 20439 +23563 50579 +83622 95053 +57297 46927 +61983 39502 +12603 34784 +23669 53745 +52072 34297 +78049 75669 +23990 87858 +22137 90163 +90938 18517 +55725 90031 +59993 74012 +54578 85675 +92115 46927 +38969 47605 +81101 10287 +93794 75669 +64839 36867 +47311 12161 +78166 76537 +87125 50079 +66623 25465 +88110 98681 +72660 75592 +41748 37738 +91564 41306 +12219 62821 +60138 52717 +54590 32212 +62975 85753 +61842 35300 +26272 39516 +27007 76251 +48169 63474 +94898 88451 +22706 26762 +56283 76537 +29521 84894 +36845 90163 +88825 90163 +29862 21377 +81174 69137 +13711 34297 +53878 51226 +87590 79980 +99631 76585 +82834 45447 +78342 19644 +28790 72524 +36906 75669 +14336 58221 +76519 77427 +87574 24390 +63709 60766 +39632 36400 +29645 98833 +81798 34297 +81713 86165 +22679 90967 +38591 20490 +14714 93340 +14080 18923 +63166 50079 +13932 17906 +83688 78371 +90711 39189 +90434 96758 +71866 25445 +28566 22989 +20439 20760 +37224 25649 +77827 76115 +64745 37738 +26417 73446 +64271 16805 +81563 36867 +12278 85758 +15257 43127 +97652 21570 +10946 77315 +81470 37738 +47816 63078 +29783 52739 +62569 63078 +71904 21741 +55555 54934 +30111 64622 +97322 59047 +84274 48025 +25514 69909 +19013 20439 +84608 13683 +99317 36003 +77083 76360 +14626 96966 +50572 14581 +21558 60518 +68710 52504 +50579 44922 +98946 15999 +57049 13900 +34217 76875 +88561 63078 +38428 49321 +35301 46430 +68659 44724 +78548 21014 +38423 47605 +25602 76689 +26663 61754 +39416 67964 +35875 55373 +23721 58152 +58006 83208 +52580 62998 +95373 50079 +12865 76537 +72652 20439 +78586 31055 +94022 58221 +25025 98833 +42121 50579 +87831 45945 +48850 64807 +83055 87840 +52315 96758 +93089 68844 +25700 23721 +72511 20439 +39725 34297 +80667 34696 +70151 33255 +92383 96596 +55177 51420 +59932 32699 +42058 98833 +56115 54523 +44800 78852 +74334 85797 +12524 49766 +88761 60104 +83983 65193 +29874 86165 +57987 37738 +99851 83353 +33458 21014 +79218 27405 +59543 44536 +21709 54522 +19922 66820 +41425 76644 +13866 61703 +49725 34696 +51423 17935 +59245 27541 +87806 62519 +79982 11295 +28132 69809 +56313 26663 +37423 50079 +21631 61107 +90163 50079 +64701 13172 +69835 47605 +63939 47605 +69441 37738 +90631 30666 +54776 13172 +25224 45822 +82457 24534 +92005 13172 +18636 14581 +66705 90163 +57104 54522 +48735 97135 +94866 66203 +83090 89376 +40897 54522 +88797 42310 +85498 58221 +30190 63410 +66064 12908 +29791 61813 +66255 83043 +41250 83934 +94161 92546 +54325 55163 +45766 37252 +33824 65193 +45441 80901 +85065 90163 +15504 57097 +91128 13172 +47086 83158 +62989 71264 +46684 36273 +21394 56793 +85025 95859 +30167 20640 +63007 15741 +92712 65193 +90022 96758 +64112 34297 +32922 91132 +82947 54436 +27408 63886 +92065 47605 +60699 20439 +45185 86010 +81565 51093 +43850 76537 +90819 33508 +67506 37738 +40142 34696 +59876 33440 +73904 38458 +21431 59114 +85880 42652 +75274 87983 +63975 75849 +42859 63535 +45645 90967 +34062 61999 +41145 54522 +58221 76199 +41790 54732 +34067 87542 +75883 49363 +64882 21014 +92004 78473 +54170 26663 +53032 65307 +41443 86165 +24191 65193 +68372 61813 +62823 18816 +92170 21014 +79184 93815 +38988 20439 +16245 34696 +61971 17725 +58462 61813 +13669 37284 +95491 50170 +80368 12991 +54437 40865 +49667 91132 +57601 60331 +36103 94892 +61752 46307 +87154 62129 +11458 35248 +95020 75669 +74407 80757 +76364 90967 +74924 79609 +74178 79192 +28787 20439 +30643 47605 +97779 64469 +18060 14581 +33280 66457 +19928 14867 +16465 21558 +96623 49569 +35706 36845 +19824 63236 +79474 37738 +46368 54936 +81026 94866 +74845 92731 +87992 36248 +10511 91369 +17541 91132 +99200 54882 +92088 26663 +30102 96758 +95669 82527 +31323 37738 +49087 72236 +13654 58221 +41957 29009 +69672 60755 +73457 45040 +69851 76537 +51708 72524 +81752 73194 +42100 44337 +65676 34696 +67173 98833 +56955 90163 +47605 90973 +56609 14581 +96803 60138 +20037 74800 +84578 13172 +73603 60215 +26774 63078 +69717 60331 +25769 31133 +65193 33467 +68993 86165 +31290 58673 +35905 12645 +75221 21014 +71743 94866 +15429 51324 +88883 76537 +53273 60331 +53474 94866 +30423 53459 +87208 45513 +21922 76537 +56981 34297 +84407 82475 +92502 72524 +44701 54936 +37137 15727 +46656 91132 +85169 54544 +42787 85847 +32009 15438 +76537 60331 +75936 63078 +64069 94866 +94775 50579 +11066 37007 +96942 12552 +39943 19339 +99820 10453 +82222 60331 +67201 90967 +45047 73382 +52846 42760 +31925 87519 +75095 91207 +67559 86165 +70255 61609 +14881 48739 +66111 50079 +52776 94850 +48217 18408 +27533 10938 +76862 20439 +93730 49950 +19119 98833 +20793 20778 +28844 53836 +75809 26663 +79954 87261 +89143 60138 +15920 94866 +98309 54522 +61390 34297 +91590 61813 +36942 75380 +43612 21014 +62280 76806 +81905 20049 +69500 17817 +30016 54522 +37738 24841 +97736 76537 +95453 60331 +83841 83825 +89661 37221 +54880 38461 +30171 58221 +43651 48482 +32991 50079 +77834 72467 +86698 60331 +54669 75669 +86168 47151 +20582 34297 +63078 61813 +40364 96758 +48964 86751 +91274 65761 +23554 60331 +62116 20439 +47249 14581 +65442 86165 +87647 54528 +67752 90166 +69385 72524 +35212 14581 +55866 34308 +86864 38899 +17913 93044 +70472 90163 +78816 52312 +74722 68941 +69344 34297 +69289 43658 +67116 77640 +17178 38466 +54049 16342 +10096 88031 +10933 22940 +17904 90967 +73750 53331 +24618 32163 +55226 23074 +95001 12994 +77839 14600 +57111 61813 +24177 98351 +86576 13172 +44614 96758 +81490 88939 +24759 20977 +11287 50079 +74785 76689 +27583 37822 +30172 45526 +89894 47983 +42508 54522 +25556 75669 +18793 91132 +32941 47773 +99728 34967 +28210 10047 +27008 19170 +84393 66307 +65829 21014 +49816 56988 +73908 60364 +22824 49783 +27756 33047 +84212 28530 +51194 30445 +50248 66503 +13587 69771 +54561 21830 +14733 36879 +53055 76537 +31145 26316 +16865 56614 +96415 36845 +87252 75073 +58918 14581 +43096 36375 +27809 20695 +76384 34297 +90593 86165 +94850 36867 +84649 63153 +70914 46927 +37245 60331 +15256 15850 +95830 76537 +72468 22864 +68165 25805 +49990 58221 +30245 55581 +80177 89723 +19102 25749 +60883 54936 +94584 21014 +11629 65193 +83942 86493 +98428 20005 +29707 96758 +36468 54522 +86820 76537 +21262 59570 +60331 34966 +29379 94850 +48953 90163 +73148 58221 +97913 21558 +24039 13439 +43317 34297 +34623 21014 +14581 80148 +97267 16045 +29237 67529 +83091 91132 +46331 47605 +69602 98833 +28519 77331 +14318 24668 +26144 90967 +28648 60331 +37188 47605 +95949 50579 +44407 16380 +49859 58221 +46623 76629 +75981 62015 +57484 81117 +85229 73269 +82977 50579 +10758 23721 +10279 94850 +37576 53461 +94624 50982 +95947 60331 +69892 23721 +81444 21014 +13092 26573 +14868 50079 +33930 58282 +57471 62978 +72588 78073 +41668 50079 +10306 50079 +87273 77847 +41744 23721 +35868 91132 +53133 43979 +75783 54936 +10074 35689 +53205 96758 +92488 52569 +38198 90163 +20120 90111 +78344 94850 +99876 13172 +69062 72097 +43840 72694 +48500 13273 +59750 68279 +67216 19658 +16906 50579 +98034 88469 +41621 72071 +93818 23721 +61664 13602 +45369 44961 +20507 28776 +82793 90163 +87708 50079 +36898 98833 +52570 90163 +36679 65193 +26649 50079 +75507 47605 +31553 94257 +17591 31585 +22067 81225 +48812 23721 +49198 56618 +26002 34983 +42947 98833 +12950 59924 +71406 17446 +22795 29922 +87872 63355 +78226 34297 +94380 22491 +56778 96093 +63451 45416 +16336 49990 +20682 12570 +41020 83481 +73814 25139 +82170 78735 +25087 65193 +30733 98833 +70801 86165 +31083 19553 +87063 98719 +42217 72138 +10510 78912 +56707 91456 +13172 71270 +53337 90208 +25513 92697 +39676 63037 +48497 90932 +98346 83722 +17160 26663 +14191 79977 +86557 31093 +12936 13172 +66657 88491 +14496 14581 +10452 14581 +27873 76537 +57301 18372 +84746 96108 +11640 34696 +11240 16854 +13244 14581 +29407 64574 +72524 76537 +70776 93370 +78168 58221 +45118 76537 +95654 56763 +63910 37738 +52213 50079 +71429 35239 +55261 80239 +40819 60331 +77021 87902 +62939 54522 +43136 58221 +54571 77272 +61525 24668 +78329 77956 +16417 34696 +24710 12753 +98654 88262 +11417 43676 +97222 93882 +36533 32918 +63037 23721 +33935 76689 +85633 50622 +53326 22895 +38602 34696 +64320 45677 +84394 20619 +22214 75828 +40603 90163 +12038 72225 +91760 76568 +30962 72524 +69928 34696 +91548 33221 +61270 90163 +52543 21014 +96907 50445 +94862 10606 +36867 21252 +78178 58229 +96175 89174 +18313 34297 +29518 72524 +47176 14117 +16043 11676 +98613 99098 +24524 67759 +50079 16991 +50392 76537 +96483 37738 +23704 98833 +79804 76689 +59352 96758 +28734 94850 +66932 23721 +84652 58221 +81233 58221 +61813 23721 +39414 75107 +61577 90199 +52142 72213 +99269 74680 +65597 91623 +76283 21620 +59366 98833 +96758 96758 +34965 34696 +53408 84822 +15500 41951 +47748 39439 +21807 76689 +43690 33261 +58602 34696 +64122 90967 +54427 26663 +64093 37784 +14460 94866 +89611 29795 +63056 22693 +80641 63078 +20065 76689 +53759 26262 +53429 91132 +33664 96758 +98954 27982 +18508 64233 +73654 60229 +36749 91132 +38704 77512 +58398 18000 +15407 42043 +79946 21014 +65603 21558 +37739 66393 +10565 36845 +12705 70406 +53419 63312 +76095 72348 +74185 84354 +38692 91132 +35982 13657 +20938 78148 +99256 18059 +21586 34696 +77045 12073 +83458 91132 +68485 11739 +19772 21014 +24490 91132 +82581 71791 +62633 18414 +39624 96758 +28147 90163 +53778 90163 +37875 23721 +95920 33634 +25986 80269 +97541 40536 +52877 60331 +94093 25047 +68569 13163 +93531 75209 +72106 23721 +88416 13149 +54125 27336 +98451 79877 +22883 86165 +70670 63113 +19738 50079 +87304 34696 +12339 36867 +10831 56006 +99160 40748 +51288 74418 +93577 58438 +84192 98833 +43597 34696 +92837 63078 +21981 13030 +53101 62867 +61701 98833 +23547 94866 +34215 90478 +90974 38157 +85904 32241 +14327 11808 +91673 47605 +55493 33743 +97416 50579 +64549 13172 +57965 20439 +58101 47605 +18375 71471 +69653 22813 +69096 27820 +87334 63903 +41580 24885 +11874 86165 +44957 34696 +86634 86165 +51588 22065 +92199 58293 +88542 22014 +22393 20439 +68666 11430 +39369 67038 +46682 50079 +76689 98833 +47130 81808 +13334 40847 +81359 99924 +25479 50453 +91035 86165 +86432 36668 +10750 39351 +22408 18607 +30205 94866 +17589 91292 +37538 34297 +22262 72524 +16278 91132 +24024 67674 +91132 47928 +94767 54350 +70645 56810 +88760 64557 +94730 67317 +13825 89274 +23899 61813 +22721 23721 +78866 40625 +40381 32386 +67302 21114 +83660 49990 +82973 48029 +46927 47605 +50994 59060 +75669 96471 +93198 42529 +63040 96758 +88017 16954 +85934 26663 +61221 49936 +34197 50579 +98843 45607 +42984 87664 +33387 35585 +96561 82166 +91974 91132 +29642 21398 +66503 65193 +52839 44772 +45836 95784 +12913 52263 +26079 88145 +77544 88929 +68347 60331 +87632 36867 +50438 64890 +52584 97599 +61632 37738 +98906 47605 +90893 24668 +10194 18767 +46785 21558 +51828 96758 +82148 94866 +77897 37738 +53368 98833 +18940 59455 +32696 54522 +71787 55930 +86247 26113 +56494 34297 +50696 58221 +43551 65193 +95395 26663 +64045 98833 +46544 54224 +86165 59770 +96117 64137 +98537 91132 +33444 22298 +54936 50579 +39368 99983 +82911 84250 +58265 58221 +95446 81144 +79820 20439 +89799 94866 +40876 98833 +42293 26663 +36637 97473 +19260 13172 +98277 62300 +62212 41226 +68970 87041 +43371 21558 +21067 26663 +75308 46978 +44869 53063 +49906 60331 +45217 63078 +50201 11923 +99156 34297 +20043 62082 +43242 27780 +47142 13172 +23278 20135 +78514 91048 +56171 92100 +27955 13172 +86709 83073 +96056 72524 +13890 26986 +82307 60522 +35357 28494 +34580 13172 +62727 41678 +21591 30862 +98258 76537 +74351 91132 +17843 91132 +23738 58221 +18389 58221 +65148 96758 +32995 55446 +51343 58221 +54522 31336 +90967 36845 +95431 26257 +57772 26663 +57712 93356 +52205 57920 +24262 76537 +47614 93956 +65408 60331 +50528 12543 +11005 60331 +10727 90163 +45279 21558 +92496 12711 +34096 42388 +12506 26663 +25503 11871 +39448 36845 +81314 54522 +66364 86165 +85889 14581 +39236 79223 +56055 26663 +90779 34297 +18166 72230 +53177 21014 +43673 81022 +36350 74055 +55270 96758 +65680 41704 +82812 87444 +50741 79868 +36155 91048 +14114 40755 +20019 37738 +84302 37412 +44026 68031 +34297 63388 +22061 89848 +71994 75669 +90335 63839 +39658 98833 +58540 26663 +34246 52806 +83116 82954 \ No newline at end of file diff --git a/day1/src/main.rs b/day1/src/main.rs new file mode 100644 index 0000000..cee7d1c --- /dev/null +++ b/day1/src/main.rs @@ -0,0 +1,13 @@ +use crate::task1::task1; +use crate::task2::task2; + +mod task1; +mod task2; +mod common; + +fn main() { + let task1_answer = task1(); + let task2_answer = task2(); + println!("The answer to task 1 is = {task1_answer}"); + println!("The answer to task 2 is = {task2_answer}"); +} diff --git a/day1/src/task1.rs b/day1/src/task1.rs new file mode 100644 index 0000000..6f44b8c --- /dev/null +++ b/day1/src/task1.rs @@ -0,0 +1,14 @@ +use crate::common::parse_input_file; + +pub(crate) fn task1() -> usize { + let (mut first_number_array, mut second_number_array) = parse_input_file(); + + first_number_array.sort(); + second_number_array.sort(); + + first_number_array + .into_iter() + .zip(second_number_array) + .map(|(first, second)| first.abs_diff(second)) + .sum() +} diff --git a/day1/src/task2.rs b/day1/src/task2.rs new file mode 100644 index 0000000..6591385 --- /dev/null +++ b/day1/src/task2.rs @@ -0,0 +1,28 @@ +use std::collections::HashMap; +use crate::common::parse_input_file; + +pub(crate) fn task2() -> usize { + let (first_number_array, second_number_array) = parse_input_file(); + let mut map = HashMap::::with_capacity(first_number_array.len()); + + first_number_array + .into_iter() + .map(|value| { + if let Some(product) = map.get(&value) { + *product + } else { + let count = count_occurrences(&second_number_array, value); + let product = value * count; + map.insert(value, product); + product + } + }) + .sum() +} + +fn count_occurrences(array: &[usize], value: usize) -> usize { + array + .iter() + .filter(|arr_value| **arr_value == value) + .count() +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..e69de29