From 5e7ae3a05cc0665a9573822d0b662cfc62b171bd Mon Sep 17 00:00:00 2001 From: mazharenko Date: Sun, 1 Dec 2024 12:45:49 +0500 Subject: [PATCH] day 1 --- src/aoc/Year2024/Day01/Day01.cs | 80 ++- tests/AoC.Tests/AoC.Tests.csproj | 5 + tests/AoC.Tests/InputTests.cs | 3 +- tests/AoC.Tests/inputs/input01.txt | 1000 ++++++++++++++++++++++++++++ 4 files changed, 1072 insertions(+), 16 deletions(-) create mode 100644 tests/AoC.Tests/inputs/input01.txt diff --git a/src/aoc/Year2024/Day01/Day01.cs b/src/aoc/Year2024/Day01/Day01.cs index f9a3c05..308ef91 100644 --- a/src/aoc/Year2024/Day01/Day01.cs +++ b/src/aoc/Year2024/Day01/Day01.cs @@ -2,18 +2,68 @@ namespace aoc.Year2024.Day01; internal partial class Day01 { - internal partial class Part1 - { - public string Solve(string input) - { - throw new NotImplementedException(); - } - } - internal partial class Part2 - { - public string Solve(string input) - { - throw new NotImplementedException(); - } - } -} + private static (int[] first, int[] second) ParseBase(string input) + { + var parser = + Numerics.IntegerInt32.ThenIgnore(SpanX.Space) + .Then(Numerics.IntegerInt32) + .Lines() + .Select(lines => ( + lines.Select(x => x.Item1).ToArray(), + lines.Select(x => x.Item2).ToArray()) + ); + + return parser.Parse(input); + } + + internal partial class Part1 + { + private readonly Example example = new( + """ + 3 4 + 4 3 + 2 5 + 1 3 + 3 9 + 3 3 + """, 11); + + public (int[] first, int[] second) Parse(string input) => ParseBase(input); + + public int Solve((int[] first, int[] second) input) + { + var totalDifference = + input.first.Order() + .Zip(input.second.Order(), Distance).Sum(); + + return totalDifference; + + int Distance(int x, int y) => Math.Abs(x - y); + } + } + + internal partial class Part2 + { + private readonly Example example = new( + """ + 3 4 + 4 3 + 2 5 + 1 3 + 3 9 + 3 3 + """, 31); + + + public (int[] first, int[] second) Parse(string input) => ParseBase(input); + + public int Solve((int[] first, int[] second) input) + { + var secondGroups = input.second.ToLookup(x => x); + + var similarityScore = input.first.Select(x => x * secondGroups[x].Count()).Sum(); + + return similarityScore; + } + } +} \ No newline at end of file diff --git a/tests/AoC.Tests/AoC.Tests.csproj b/tests/AoC.Tests/AoC.Tests.csproj index df2f4d1..9be13f9 100644 --- a/tests/AoC.Tests/AoC.Tests.csproj +++ b/tests/AoC.Tests/AoC.Tests.csproj @@ -23,6 +23,11 @@ + + + Always + + diff --git a/tests/AoC.Tests/InputTests.cs b/tests/AoC.Tests/InputTests.cs index b69f2e0..5e30e5e 100644 --- a/tests/AoC.Tests/InputTests.cs +++ b/tests/AoC.Tests/InputTests.cs @@ -8,6 +8,7 @@ internal partial class InputTests { private static IEnumerable GetCases() { - yield break; + yield return new PartInputCaseData(1, 1, "1889772"); + yield return new PartInputCaseData(1, 2, "23228917"); } } \ No newline at end of file diff --git a/tests/AoC.Tests/inputs/input01.txt b/tests/AoC.Tests/inputs/input01.txt new file mode 100644 index 0000000..35cf541 --- /dev/null +++ b/tests/AoC.Tests/inputs/input01.txt @@ -0,0 +1,1000 @@ +66845 37619 +94793 99076 +76946 36179 +27374 48777 +47847 92154 +83270 97857 +19354 94331 +69716 58559 +16902 36957 +51817 88003 +40758 64262 +39074 90635 +34632 79874 +64980 96787 +96137 94559 +55489 98589 +95130 76776 +50275 50416 +46438 49722 +34182 49722 +99196 51678 +28426 49456 +55782 77278 +96554 38327 +92781 25688 +66818 64897 +83432 67261 +92511 85918 +87928 74826 +45552 37494 +60234 58251 +34717 40758 +17406 40666 +29863 13598 +30715 16605 +79985 24806 +19498 33371 +45577 59103 +22430 97187 +60651 52438 +24435 14715 +47805 33629 +21747 21255 +42759 37159 +81798 25688 +69888 75513 +10983 57175 +58312 51968 +86884 41517 +84419 46068 +18170 76776 +18387 30282 +30887 51117 +27509 49568 +79888 65951 +84877 31605 +37741 70988 +56232 51993 +66360 34958 +35077 60422 +11529 96255 +83449 21189 +65777 39047 +92759 70579 +36784 34261 +44166 77293 +27169 46068 +47337 15975 +58412 48777 +44195 71990 +86636 25688 +30002 64342 +92886 31836 +47608 76776 +93286 93216 +95162 72607 +28232 49722 +36024 42759 +79729 44744 +46188 90896 +36793 40758 +43516 69333 +20294 69351 +39704 65846 +12801 75185 +29338 69351 +87398 33648 +43037 40666 +48777 48777 +73934 51968 +63516 67431 +91790 94559 +86093 50924 +80995 95426 +18788 69351 +55112 58623 +50558 51968 +50125 48777 +70970 65082 +95693 34007 +17814 75466 +94946 34672 +28046 18149 +41708 92261 +94045 40666 +43687 64262 +61579 48777 +40428 60422 +22420 32085 +81816 40758 +46315 42253 +13240 48777 +14096 50416 +74231 98724 +65623 29631 +93360 51968 +23203 13106 +62141 96255 +78614 44089 +25431 25688 +40555 37905 +69148 67431 +59262 72776 +69333 40758 +44889 40666 +75646 80493 +96536 65674 +52772 55418 +89647 40758 +15843 14319 +96124 10675 +44307 65041 +26300 19974 +51312 78939 +80629 88003 +33658 72722 +82408 51968 +58384 40362 +96798 51968 +39019 64262 +53075 69333 +90232 81066 +70094 84299 +29829 64262 +22687 30237 +36509 31836 +65164 49722 +97198 73460 +94559 32258 +23976 75804 +68670 45453 +35761 33638 +89859 30972 +87196 48777 +93726 73290 +25588 40666 +82420 68892 +39195 66867 +18964 26674 +76776 45413 +81228 60422 +48337 92298 +35288 50416 +31186 94774 +62040 83156 +82769 40666 +75516 52772 +46916 83286 +77933 62114 +92256 68878 +32048 67431 +31355 92298 +81356 81828 +29014 64443 +40593 76965 +67982 92627 +57605 77293 +59621 23426 +71053 15293 +58788 33577 +96408 15826 +21328 16605 +60042 38036 +26150 16605 +36476 45576 +98875 26150 +50224 26150 +13099 77512 +24240 57493 +14259 36228 +25943 71801 +69018 15570 +64982 29117 +56462 42759 +18046 40666 +78056 26150 +34273 50416 +51578 73053 +28229 51968 +19334 81510 +74063 22991 +64637 99076 +63440 96255 +69596 90956 +44661 60422 +83603 73568 +47360 25240 +38361 56084 +30109 49115 +54979 49722 +57864 76776 +11404 23775 +31046 84459 +42983 63211 +82072 21091 +93495 31277 +14189 73053 +36367 50416 +99522 29631 +26894 44711 +20066 12397 +51211 69351 +75520 71791 +66473 41543 +41955 76776 +26371 45285 +73611 51968 +23686 46068 +85473 60422 +91840 30237 +64262 35142 +16860 48543 +89770 81829 +25598 48534 +80127 48777 +91437 23375 +68177 25777 +38952 90058 +24402 46233 +47962 16605 +35661 77293 +92555 69333 +46076 73053 +21462 50298 +77271 20126 +81010 71068 +22322 51968 +54671 16605 +42034 43993 +61234 16605 +11778 48777 +73064 96914 +92302 66267 +88810 39985 +28987 49722 +19948 63051 +46165 42759 +80483 32007 +10186 87088 +50266 98182 +38661 60422 +43071 29117 +29149 90127 +20786 92298 +41543 63002 +65554 44245 +38133 50446 +83184 43685 +81455 30237 +50181 20015 +21588 41543 +71901 77005 +56580 67190 +92298 30290 +12615 43868 +54771 97825 +40608 83376 +74661 81327 +74667 27063 +91093 69333 +78147 74050 +88520 67431 +83147 58241 +32466 96255 +88003 26594 +48870 44355 +57990 33895 +25924 52335 +28398 25688 +59394 16605 +88485 18070 +24797 94556 +20403 92298 +41703 25688 +98485 39009 +20982 71267 +89943 90424 +90090 84500 +48506 75682 +73724 15460 +16518 51968 +80097 85128 +17726 30691 +55319 76440 +31484 40758 +95079 69351 +51136 69333 +20432 50416 +36397 81701 +60587 87382 +97583 69333 +79660 79014 +90931 61415 +68754 99076 +81332 29631 +36693 18053 +83807 63785 +69967 41543 +42871 98738 +95094 91288 +99212 99076 +44058 84467 +80641 34674 +88334 29631 +88355 71891 +23565 64262 +64119 54463 +17173 36522 +28557 31472 +55707 49080 +60573 53380 +78740 48777 +25688 40758 +61756 50713 +42962 94559 +29631 92298 +94426 67431 +98512 82316 +68213 69351 +89436 29117 +52114 74278 +50779 18690 +37430 18749 +41985 87256 +18966 73331 +46251 69146 +77421 78181 +61790 45476 +74498 10793 +69841 82893 +65897 20462 +41449 29160 +48907 44418 +81437 40758 +59295 67431 +83332 89658 +72986 37494 +96096 50416 +61423 41543 +17994 96255 +61296 15425 +88163 59232 +54306 51570 +95733 61049 +99042 15221 +68770 71427 +88129 69333 +33732 92099 +77286 61983 +90425 60422 +15280 46859 +31735 70146 +52247 13562 +30396 40758 +99667 30237 +51830 79901 +83666 25524 +86580 98750 +85254 48777 +42725 11056 +23687 97931 +22760 94009 +46614 77846 +72811 24806 +54977 49722 +29431 73999 +18885 40666 +36487 64262 +89636 50324 +35161 49495 +81599 67431 +88407 33080 +22435 50416 +79005 10940 +57331 41543 +22158 40666 +96696 30237 +70561 45453 +73421 60631 +61118 43928 +42519 79091 +43521 64262 +63402 15989 +45694 95467 +76105 44778 +62478 68359 +12358 99076 +86440 88199 +87086 83286 +53389 39542 +85805 31836 +67650 68375 +15685 63010 +58192 46795 +47818 92298 +11074 94986 +71914 40666 +93767 14418 +15245 79805 +61703 69333 +98920 26347 +39398 43675 +24806 93670 +95455 83582 +31836 42759 +75823 89112 +76495 95693 +30421 64262 +29524 69542 +87652 26150 +28324 60422 +76379 22670 +21066 20848 +48616 71126 +27230 73053 +72632 73053 +68412 69333 +93254 71387 +58582 40758 +49794 41543 +47461 60562 +76885 14543 +64004 73230 +32657 96255 +53863 10455 +20628 35993 +91608 87804 +75577 69351 +71476 64262 +81995 23722 +50925 69351 +11299 77938 +35420 99076 +10331 29117 +15779 75594 +85678 37494 +91928 42759 +63346 75594 +66976 96255 +83382 88003 +57567 44223 +57699 13304 +87134 99196 +75545 76776 +25631 41543 +87276 10800 +56940 51968 +84938 42251 +95193 54917 +29270 34604 +89353 63191 +40688 42759 +66751 53340 +66452 16819 +38862 17153 +96222 46438 +87343 57690 +46164 57572 +99417 27010 +70060 10746 +71154 34499 +12509 48869 +93651 78940 +71299 73847 +10832 30237 +52974 64262 +40981 11346 +29705 40758 +67938 95118 +60177 49969 +47250 91132 +54643 25688 +85588 76776 +98925 69333 +60422 90370 +24036 39886 +29403 40758 +74173 25688 +55986 40758 +25014 69333 +12619 88003 +94185 66972 +36302 76776 +80587 47593 +69768 39980 +69155 11763 +50416 78224 +46304 16273 +96705 69351 +28345 35177 +77293 10590 +12724 52945 +35375 78625 +75160 45453 +46386 65833 +39994 48140 +91271 59925 +29612 77293 +41036 88472 +81721 69333 +17164 69333 +59045 55408 +47643 48777 +88393 31836 +84088 29471 +59047 99076 +78516 76776 +15157 99196 +43690 94559 +75594 53479 +44896 71111 +94548 39265 +83041 42184 +96815 51415 +32828 70721 +58699 81992 +31287 64262 +26029 37494 +65166 83286 +54211 99076 +83286 91733 +56093 73053 +72216 58645 +41450 27606 +40073 49722 +95257 78671 +45206 57288 +77156 16605 +95757 87915 +37072 43889 +64269 43521 +32134 94350 +17176 28044 +96935 16605 +12632 39455 +58840 64262 +45556 60422 +56436 23521 +63022 10638 +58109 65191 +20092 51968 +94565 87895 +95740 67431 +82012 14463 +95332 76776 +10057 36067 +90826 85685 +26656 37494 +21477 28483 +69198 30835 +86852 31534 +99374 40666 +67436 26735 +69091 37640 +98680 31836 +62432 46068 +81423 37494 +65121 49722 +63197 92795 +61748 89795 +48450 41543 +22248 50416 +53155 29214 +66777 83286 +61747 30639 +77640 31836 +30701 73053 +77911 39784 +68586 93617 +16193 95734 +87617 94302 +30286 81591 +37535 95903 +33511 95693 +13847 94086 +48968 92039 +18649 81319 +90213 51247 +19938 14934 +45563 77293 +65848 83368 +15964 74755 +46068 23448 +42958 13602 +75671 27217 +29655 51968 +22699 25688 +51147 42589 +94306 42759 +25877 30237 +65736 73053 +90797 16605 +37067 95623 +83464 91292 +42197 45801 +91306 99076 +60911 92298 +34318 33706 +38793 96334 +27850 37433 +14650 64262 +91128 64339 +32458 98055 +54600 62677 +98050 75988 +86045 41543 +42279 19215 +42179 41543 +74312 39080 +79748 67431 +40215 77293 +41710 90776 +30328 68060 +26336 88003 +36852 22273 +40112 16836 +85174 16605 +71517 32703 +67431 99004 +43343 64262 +13731 22989 +13807 54987 +67892 39600 +27164 13761 +73866 29117 +49004 11308 +30611 79799 +99434 64262 +39641 96255 +37656 46551 +31848 30237 +98478 83598 +98613 17687 +82739 36729 +90600 41659 +11405 52772 +38095 26098 +78900 49223 +90011 64515 +83200 65037 +71310 31350 +29261 29631 +99897 41543 +10080 60422 +53548 22585 +33783 94884 +13126 76776 +19067 48777 +60524 31565 +78024 69351 +78854 66750 +45049 47031 +99076 25688 +38748 81291 +10930 57953 +59839 26241 +78585 86796 +59376 49722 +60726 60127 +81844 97760 +53820 37832 +58200 51968 +39265 60422 +60470 33170 +91160 96442 +27028 85013 +47223 41543 +76737 16605 +57526 56760 +65236 31836 +53301 85609 +24915 16605 +35708 83286 +95380 27448 +84774 28106 +41172 68961 +91822 90839 +40133 50910 +12658 69351 +68683 61451 +48857 43558 +75612 40666 +68638 84502 +50216 77750 +99960 58184 +84452 13882 +45383 49722 +75492 60195 +13480 22237 +50803 84944 +54012 60422 +84902 96255 +17970 76776 +11010 86694 +99306 49722 +40510 64262 +49829 88882 +59557 60358 +85594 67431 +59545 67431 +20954 40666 +12472 78637 +70813 89650 +99797 81151 +77207 49160 +49923 51618 +65898 30237 +37494 24806 +85492 83863 +39497 37494 +36089 14785 +20089 42979 +96255 96255 +32410 46438 +48216 96255 +76597 54602 +53910 18651 +15354 14547 +98751 90551 +74557 90793 +86473 64457 +57086 86585 +69351 16557 +49032 10281 +95909 64037 +98841 26150 +38404 25688 +53132 48777 +41823 99294 +73232 16605 +14338 74560 +20654 61132 +39466 77293 +17828 99107 +44233 42759 +75537 82505 +97545 60422 +16764 51968 +17017 43797 +70260 61407 +85468 30237 +37966 64262 +49722 63390 +55689 41072 +47330 53047 +94724 60422 +28441 63752 +27435 25688 +49740 11717 +34057 27423 +64654 71907 +83242 29117 +97554 73053 +42324 69351 +43064 47040 +64801 40758 +55649 48777 +71147 41347 +90522 60422 +39447 28206 +26012 92655 +12500 21088 +96913 96255 +73053 21102 +96929 64262 +17234 49722 +71663 69333 +27255 61341 +20631 42759 +53872 89055 +21030 42759 +63642 92298 +37041 13379 +62151 99076 +44779 44361 +33142 70645 +34298 99076 +74521 71083 +97108 42956 +77170 40666 +82868 54524 +60362 38781 +66295 14324 +18853 92189 +86177 76776 +24054 49722 +64974 22099 +88395 26150 +24079 52772 +90673 26493 +28410 60422 +23503 41005 +22127 81692 +43022 69046 +21423 30237 +20584 99347 +19793 64262 +85494 29631 +35760 67431 +37390 11112 +11337 67225 +52314 25688 +99221 92298 +90131 76776 +79699 57892 +22197 89226 +21685 53250 +61808 13618 +95453 30237 +66322 46438 +49172 59373 +54628 40323 +87270 67431 +25855 69351 +18455 75737 +45453 75723 +48577 39690 +47391 56849 +32796 55399 +36667 27839 +24640 92298 +52237 62652 +10472 65480 +96200 40686 +48162 27800 +31265 87629 +28125 99076 +73922 90954 +32255 48777 +47397 73220 +47401 38154 +70327 60422 +38692 19213 +18472 31836 +71218 35466 +37097 33313 +79607 49008 +88691 16605 +30767 19624 +47670 88003 +87223 15008 +62459 74801 +35995 76776 +39905 51968 +62948 16651 +14137 97229 +51968 25688 +19454 77293 +48128 29631 +87280 46179 +97139 29631 +91913 51492 +52926 66255 +88930 41045 +51402 13749 +56326 16605 +56973 31836 +30330 40666 +56823 39962 +43407 69225 +86434 45453 +16605 27225 +24931 27332 +58345 67854 +25412 61629 +90343 95693 +20577 84501 +19871 64775 +33550 73053 +30768 48777 +44985 96255 +93435 69530 +17474 83436 +33032 27464 +67655 77293 +11189 25688 +76696 29631 +81827 37289 +14652 41543 +78881 35379 +49434 40758 +16136 37494 +95899 99076 +52886 49722 +75680 59071 +25008 18424 +10532 45453 +70853 67108 +97765 41543 +77075 98528 +49731 45588 +12421 81360 +13995 20115 +13459 67635 +44822 20506 +45941 40758 +84375 46068 +68154 27538 +50838 85083 +57673 43557 +89564 10626 +86616 45453 +95622 16605 +95036 38065 +69957 76776 +68179 32072 +11978 23791 +89856 12649 +43029 33383 +30680 91350 +65119 51344 +61868 77293 +28887 89141 +30237 92298 +74565 45124 +52029 51153 +53311 81204 +28179 45453 +25996 67537 +21527 88003 +40311 88003 +77116 64262 +32339 43982 +51922 52772 +39858 41543 +42050 33920 +22499 40777 +40666 75657 +49779 48777 +57894 34518 +21691 67431 +29904 25688 +73894 79235 +25080 47224 +24284 41543 +62787 58837 +82576 96255 +90992 21575 +56542 25688 +93368 48777 +97106 96422 +95744 75594 +86470 42303 +15426 92298 +67124 29117 +93975 80395 +20387 57449 +98213 31836 +58271 60721 +26911 48788 +18479 99076 +10735 49722 +27321 42759 +49224 16605 +38905 45630 +81499 15623 +50470 29434 +25745 41944 +85084 95159 +36196 67431 +43825 42915 +11678 14222 +63510 72474 +15304 52772 +13540 25688 +95310 40758 +64421 88003 +14821 76776 +29117 89841 +22026 33570 +66744 36521 +73205 72863 +13305 60422 +47886 15634 +16515 59541 +86124 77004 +30895 46438 +69966 25582 +31783 85509 +97288 51968 \ No newline at end of file