From d4c3c9aca8965298a3366c78eb0155bdffb1bba9 Mon Sep 17 00:00:00 2001 From: Alexander Sigler Date: Thu, 16 Dec 2021 10:43:18 -0800 Subject: [PATCH] Added Day 5,6,7 and corresponding test cases. --- AOC2021.Test/AOC2021.Test.csproj | 20 +- AOC2021.Test/DayTest.cs | 90 ++++ AOC2021.Test/Input/Day5_input.txt | 500 ++++++++++++++++++ AOC2021.Test/Input/Day5_test.txt | 10 + AOC2021.Test/Input/Day6_input.txt | 1 + AOC2021.Test/Input/Day6_test.txt | 1 + AOC2021.Test/Input/Day7_input.txt | 1 + AOC2021.Test/Input/Day7_test.txt | 1 + AOC2021/Controllers/AdventOfCodeController.cs | 3 + AOC2021/Days/Day5.cs | 73 +++ AOC2021/Days/Day6.cs | 82 +++ AOC2021/Days/Day7.cs | 61 +++ AOC2021/Models/AOCDay.cs | 2 +- AOC2021/Models/AOCResponse.cs | 9 +- AOC2021/Models/Day5/Line.cs | 66 +++ AOC2021/Models/Day5/Vent.cs | 20 + AOC2021/Models/Day6/Fish.cs | 22 + AOC2021/Models/Day6/FishSchool.cs | 49 ++ 18 files changed, 1005 insertions(+), 6 deletions(-) create mode 100644 AOC2021.Test/Input/Day5_input.txt create mode 100644 AOC2021.Test/Input/Day5_test.txt create mode 100644 AOC2021.Test/Input/Day6_input.txt create mode 100644 AOC2021.Test/Input/Day6_test.txt create mode 100644 AOC2021.Test/Input/Day7_input.txt create mode 100644 AOC2021.Test/Input/Day7_test.txt create mode 100644 AOC2021/Days/Day5.cs create mode 100644 AOC2021/Days/Day6.cs create mode 100644 AOC2021/Days/Day7.cs create mode 100644 AOC2021/Models/Day5/Line.cs create mode 100644 AOC2021/Models/Day5/Vent.cs create mode 100644 AOC2021/Models/Day6/Fish.cs create mode 100644 AOC2021/Models/Day6/FishSchool.cs diff --git a/AOC2021.Test/AOC2021.Test.csproj b/AOC2021.Test/AOC2021.Test.csproj index 0e8762c..c8499da 100644 --- a/AOC2021.Test/AOC2021.Test.csproj +++ b/AOC2021.Test/AOC2021.Test.csproj @@ -1,4 +1,4 @@ - + net5.0 @@ -42,6 +42,24 @@ Always + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + diff --git a/AOC2021.Test/DayTest.cs b/AOC2021.Test/DayTest.cs index a4ab03c..cfbf472 100644 --- a/AOC2021.Test/DayTest.cs +++ b/AOC2021.Test/DayTest.cs @@ -3,6 +3,10 @@ using AOC2021.Tests.Models; using Microsoft.VisualStudio.TestTools.UnitTesting; using AOC2021.Models; using AOC2021.Test.Models; +using System.Diagnostics; +using System; +using System.Collections.Generic; +using System.Linq; namespace AOC2021.Tests { @@ -46,5 +50,91 @@ namespace AOC2021.Tests var result = _tester.Test(request); Assert.IsTrue(request.Answer.Equals(result.Answer)); } + + [TestMethod] + public void Day5() + { + var request = new TestRequest() { Day = "day5", Answer = new Answer() { Day_A_Test = "5", Day_A_Input = "7644", Day_B_Test = "12", Day_B_Input = "18627" } }; + var result = _tester.Test(request); + Assert.IsTrue(request.Answer.Equals(result.Answer)); + } + + [TestMethod] + public void Day6() + { + var request = new TestRequest() { Day = "day6", Answer = new Answer() { Day_A_Test = "5934", Day_A_Input = "390011", Day_B_Test = "26984457539", Day_B_Input = "1746710169834" } }; + var result = _tester.Test(request); + Assert.IsTrue(request.Answer.Equals(result.Answer)); + } + [TestMethod] + public void Day7() + { + var request = new TestRequest() { Day = "day7", Answer = new Answer() { Day_A_Test = "37", Day_A_Input = "345035", Day_B_Test = "168", Day_B_Input = "97038163" } }; + var result = _tester.Test(request); + Assert.IsTrue(request.Answer.Equals(result.Answer)); + } + + //[TestMethod] + public void MapVsEnumberable() + { + var addSw = new Stopwatch(); + addSw.Start(); + var eList = new System.Collections.Generic.List(); + for (int x = 0; x < 1000; x++) + for (int y = 0; y < 1000; y++) + { + eList.Add(new DataPoint() { x = x, y = y, name = $"{x} - {y}" }); + } + addSw.Stop(); + Console.WriteLine($"Took {addSw.ElapsedMilliseconds}ms to create list"); + addSw.Restart(); + var dList = new Dictionary(); + for (int x = 0; x < 1000; x++) + for (int y = 0; y < 1000; y++) + { + dList.Add($"{x},{y}", new DataPoint() { x = x, y = y, name = $"{x} - {y}" }); + } + addSw.Stop(); + Console.WriteLine($"Took {addSw.ElapsedMilliseconds}ms to create dictionary"); + + var random = new Random(); + var getSW = new Stopwatch(); + getSW.Start(); + for (int i = 0; i < 1000; i++) + { + var pointX = random.Next(0, 1000); + var pointY = random.Next(0, 1000); + var point = GetDP(eList, pointX, pointY); + Assert.AreEqual(pointY, point.y); + } + getSW.Stop(); + Console.WriteLine($"Took {getSW.ElapsedTicks}ticks to get list "); + getSW.Reset(); + getSW.Start(); + for (int i = 0; i < 1000; i++) + { + var pointX = random.Next(0, 1000); + var pointY = random.Next(0, 1000); + var point = dList[$"{pointX},{pointY}"]; + Assert.AreEqual(pointY, point.y); + } + getSW.Stop(); + Console.WriteLine($"Took {getSW.ElapsedTicks}ticks to get dictionary "); + } + + private DataPoint GetDP(List eList, int pointX, int pointY) + { + foreach (var bs in eList) + if (bs.x == pointX && bs.y == pointY) + return bs; + return null; + } + + } + class DataPoint + { + public int x { get; set; } + public int y { get; set; } + public string name { get; set; } } } diff --git a/AOC2021.Test/Input/Day5_input.txt b/AOC2021.Test/Input/Day5_input.txt new file mode 100644 index 0000000..41daa62 --- /dev/null +++ b/AOC2021.Test/Input/Day5_input.txt @@ -0,0 +1,500 @@ +599,531 -> 599,32 +435,904 -> 435,489 +768,714 -> 768,187 +845,552 -> 596,801 +167,680 -> 167,445 +45,887 -> 45,346 +780,295 -> 179,896 +310,539 -> 602,831 +535,556 -> 349,556 +797,180 -> 797,62 +771,406 -> 120,406 +383,296 -> 383,918 +689,815 -> 73,199 +658,642 -> 658,333 +931,104 -> 708,104 +406,278 -> 406,29 +315,532 -> 773,74 +439,953 -> 289,953 +555,162 -> 695,302 +444,522 -> 444,828 +460,844 -> 460,972 +838,18 -> 143,713 +335,785 -> 335,485 +757,886 -> 757,327 +266,205 -> 273,205 +934,42 -> 19,957 +671,622 -> 263,214 +739,781 -> 739,332 +848,507 -> 848,394 +577,58 -> 461,174 +49,905 -> 921,33 +627,455 -> 205,455 +106,523 -> 974,523 +707,335 -> 707,313 +65,214 -> 712,214 +610,267 -> 610,403 +47,699 -> 565,181 +288,833 -> 709,833 +452,59 -> 452,632 +629,209 -> 125,209 +535,232 -> 535,342 +542,942 -> 542,753 +618,905 -> 552,905 +598,314 -> 976,314 +350,824 -> 17,824 +753,570 -> 753,617 +544,302 -> 259,302 +628,271 -> 628,379 +856,265 -> 856,792 +77,317 -> 77,122 +905,420 -> 905,687 +812,512 -> 812,411 +844,486 -> 771,559 +798,778 -> 798,215 +571,160 -> 278,453 +242,352 -> 227,352 +958,118 -> 167,909 +201,915 -> 201,564 +163,583 -> 163,279 +23,111 -> 23,883 +248,281 -> 331,281 +381,768 -> 900,768 +78,988 -> 78,326 +914,659 -> 247,659 +532,531 -> 520,531 +65,309 -> 734,978 +170,923 -> 399,694 +740,496 -> 196,496 +832,452 -> 816,452 +675,463 -> 878,463 +659,852 -> 560,852 +143,655 -> 227,655 +334,795 -> 334,978 +217,913 -> 368,913 +675,33 -> 503,33 +42,981 -> 811,981 +458,162 -> 722,162 +92,613 -> 92,542 +393,584 -> 393,252 +276,256 -> 725,705 +752,442 -> 752,789 +63,281 -> 744,281 +596,845 -> 35,284 +594,534 -> 964,164 +337,380 -> 337,511 +158,142 -> 75,225 +606,47 -> 606,111 +987,30 -> 62,955 +192,196 -> 428,196 +449,672 -> 449,77 +804,151 -> 804,255 +783,581 -> 287,581 +860,891 -> 69,100 +966,187 -> 761,392 +400,742 -> 278,742 +661,656 -> 592,587 +787,415 -> 787,771 +866,228 -> 417,228 +915,385 -> 505,385 +715,620 -> 715,633 +615,31 -> 615,940 +691,885 -> 527,885 +426,705 -> 351,705 +258,215 -> 258,949 +480,449 -> 480,710 +788,710 -> 788,67 +850,90 -> 597,90 +398,379 -> 18,759 +248,107 -> 665,524 +901,933 -> 208,240 +433,424 -> 110,424 +214,447 -> 389,272 +468,330 -> 468,928 +950,759 -> 332,759 +447,541 -> 420,541 +659,138 -> 604,83 +821,264 -> 95,264 +914,132 -> 46,132 +821,604 -> 821,57 +805,734 -> 85,14 +806,274 -> 164,916 +205,780 -> 205,133 +798,472 -> 361,472 +817,57 -> 127,747 +172,119 -> 922,869 +118,167 -> 55,167 +56,548 -> 344,836 +117,108 -> 940,931 +530,46 -> 530,785 +528,507 -> 729,708 +11,986 -> 987,10 +979,932 -> 76,29 +863,250 -> 210,903 +879,215 -> 891,215 +592,219 -> 592,528 +211,760 -> 211,347 +21,842 -> 633,230 +110,356 -> 110,254 +925,606 -> 444,125 +757,566 -> 757,498 +702,622 -> 637,622 +51,379 -> 365,379 +273,906 -> 273,494 +170,795 -> 929,36 +159,56 -> 435,56 +724,953 -> 724,735 +536,748 -> 901,748 +937,148 -> 937,510 +963,507 -> 863,507 +840,290 -> 840,221 +864,154 -> 55,963 +977,487 -> 685,487 +863,617 -> 210,617 +862,308 -> 291,879 +286,477 -> 286,276 +550,805 -> 550,489 +964,508 -> 821,651 +475,290 -> 789,290 +25,882 -> 25,349 +570,374 -> 604,374 +354,442 -> 514,282 +457,700 -> 360,700 +548,889 -> 548,502 +11,393 -> 11,829 +60,714 -> 781,714 +943,953 -> 972,924 +757,386 -> 465,386 +230,463 -> 27,463 +815,385 -> 326,385 +32,630 -> 378,976 +298,853 -> 298,644 +532,146 -> 23,146 +958,685 -> 737,464 +853,847 -> 79,73 +815,590 -> 815,961 +49,87 -> 751,789 +55,513 -> 55,378 +163,907 -> 574,907 +355,168 -> 355,836 +453,742 -> 674,742 +273,458 -> 685,458 +981,961 -> 958,984 +120,59 -> 401,59 +735,964 -> 395,964 +277,377 -> 277,646 +633,694 -> 633,707 +224,376 -> 976,376 +201,790 -> 293,790 +950,952 -> 12,14 +389,48 -> 356,48 +337,424 -> 166,424 +591,915 -> 591,456 +205,162 -> 942,162 +404,421 -> 404,748 +319,983 -> 608,694 +94,677 -> 94,853 +873,388 -> 873,617 +858,82 -> 858,890 +64,503 -> 64,787 +372,224 -> 50,546 +531,241 -> 960,670 +47,33 -> 975,961 +853,52 -> 271,634 +668,437 -> 668,719 +162,290 -> 843,290 +421,299 -> 944,822 +103,983 -> 103,324 +290,71 -> 290,686 +209,38 -> 546,38 +740,878 -> 378,878 +741,795 -> 741,916 +27,431 -> 445,431 +795,289 -> 795,759 +345,772 -> 775,772 +977,480 -> 512,15 +49,863 -> 49,659 +223,590 -> 779,590 +503,771 -> 917,771 +499,289 -> 935,725 +246,459 -> 246,395 +860,257 -> 656,257 +425,87 -> 425,603 +355,378 -> 355,23 +462,286 -> 462,358 +181,571 -> 181,732 +17,649 -> 476,649 +394,321 -> 394,293 +812,660 -> 515,957 +21,150 -> 799,928 +437,593 -> 437,372 +125,495 -> 373,743 +482,404 -> 482,420 +283,580 -> 283,234 +667,966 -> 827,806 +959,961 -> 959,931 +461,845 -> 206,845 +299,888 -> 299,836 +680,828 -> 680,855 +958,977 -> 26,45 +847,419 -> 290,976 +892,920 -> 892,180 +487,945 -> 487,445 +329,570 -> 583,570 +110,940 -> 989,61 +475,351 -> 882,351 +953,229 -> 429,229 +119,125 -> 749,125 +834,103 -> 212,725 +978,412 -> 978,343 +916,310 -> 758,310 +825,761 -> 720,761 +353,954 -> 353,795 +422,464 -> 422,356 +662,964 -> 836,790 +242,873 -> 242,570 +742,972 -> 797,972 +698,364 -> 360,26 +258,633 -> 19,872 +406,649 -> 406,685 +386,710 -> 925,710 +347,657 -> 524,480 +812,905 -> 554,647 +420,505 -> 420,231 +908,693 -> 908,724 +130,772 -> 130,898 +560,23 -> 560,987 +941,831 -> 941,544 +817,940 -> 132,255 +515,280 -> 515,811 +544,102 -> 568,102 +115,612 -> 67,660 +743,762 -> 743,152 +246,14 -> 691,459 +766,492 -> 673,492 +467,179 -> 351,63 +655,779 -> 655,524 +314,171 -> 314,108 +414,64 -> 502,64 +564,239 -> 894,239 +984,974 -> 56,46 +201,963 -> 201,223 +238,194 -> 238,832 +30,652 -> 477,652 +818,735 -> 582,971 +225,566 -> 673,566 +172,865 -> 74,865 +264,101 -> 264,812 +487,916 -> 979,916 +879,30 -> 10,899 +797,657 -> 797,136 +750,642 -> 593,799 +550,244 -> 418,376 +158,816 -> 668,816 +505,648 -> 303,648 +411,688 -> 263,688 +544,35 -> 771,35 +545,846 -> 286,846 +284,760 -> 284,929 +835,401 -> 708,401 +533,591 -> 545,591 +866,757 -> 475,757 +202,62 -> 907,767 +456,655 -> 456,123 +367,714 -> 225,714 +359,679 -> 926,679 +623,853 -> 623,865 +170,120 -> 213,120 +481,741 -> 481,435 +928,73 -> 41,960 +551,282 -> 551,265 +988,986 -> 12,10 +351,172 -> 791,172 +49,65 -> 952,968 +725,617 -> 691,617 +509,159 -> 697,159 +83,985 -> 83,968 +206,617 -> 334,489 +880,682 -> 966,768 +60,896 -> 60,617 +501,686 -> 49,234 +801,708 -> 738,771 +548,883 -> 548,33 +753,162 -> 29,162 +102,478 -> 102,295 +115,656 -> 637,134 +924,970 -> 924,963 +191,340 -> 191,515 +764,481 -> 523,481 +97,619 -> 97,890 +228,183 -> 228,624 +171,867 -> 68,867 +797,685 -> 167,685 +510,955 -> 464,955 +930,955 -> 233,258 +934,572 -> 934,900 +217,822 -> 797,242 +868,939 -> 369,440 +861,811 -> 861,36 +346,617 -> 346,153 +754,526 -> 754,426 +482,724 -> 482,21 +328,984 -> 976,984 +933,895 -> 325,287 +965,973 -> 232,240 +502,707 -> 767,972 +353,680 -> 815,218 +311,210 -> 311,157 +156,944 -> 928,172 +615,395 -> 101,909 +107,500 -> 528,921 +375,42 -> 375,796 +13,292 -> 818,292 +613,144 -> 613,541 +340,677 -> 340,406 +631,655 -> 744,655 +22,242 -> 723,943 +705,596 -> 980,321 +316,955 -> 316,515 +760,279 -> 44,279 +391,328 -> 391,724 +917,476 -> 917,668 +66,907 -> 913,60 +597,260 -> 362,25 +568,584 -> 568,297 +375,506 -> 375,300 +988,31 -> 72,947 +425,342 -> 154,342 +196,395 -> 899,395 +904,17 -> 94,17 +546,159 -> 751,159 +284,557 -> 175,448 +69,201 -> 697,201 +130,421 -> 224,421 +646,462 -> 637,453 +187,638 -> 621,638 +832,212 -> 416,212 +614,582 -> 348,582 +677,404 -> 677,709 +178,122 -> 915,859 +81,849 -> 223,849 +717,18 -> 646,18 +723,666 -> 974,666 +703,234 -> 130,234 +317,107 -> 106,107 +207,397 -> 207,375 +688,465 -> 982,171 +749,201 -> 610,201 +280,313 -> 827,860 +773,873 -> 917,873 +337,908 -> 337,155 +541,427 -> 385,583 +611,314 -> 131,794 +966,909 -> 104,47 +785,556 -> 346,556 +914,645 -> 914,718 +683,941 -> 657,915 +919,665 -> 310,56 +743,978 -> 779,978 +953,925 -> 953,854 +899,347 -> 705,347 +46,597 -> 46,255 +332,364 -> 922,954 +38,987 -> 832,193 +77,585 -> 77,262 +155,61 -> 734,640 +953,136 -> 655,136 +939,730 -> 158,730 +903,458 -> 393,458 +50,227 -> 50,249 +536,814 -> 536,242 +906,694 -> 259,47 +317,237 -> 853,773 +828,55 -> 509,55 +40,664 -> 341,965 +414,820 -> 53,459 +244,344 -> 272,344 +191,606 -> 308,606 +329,409 -> 329,960 +166,863 -> 938,91 +655,396 -> 291,760 +634,666 -> 625,666 +360,622 -> 360,550 +568,473 -> 840,201 +534,162 -> 534,823 +583,563 -> 583,521 +124,447 -> 124,79 +207,559 -> 207,649 +688,238 -> 26,900 +173,33 -> 117,33 +665,800 -> 665,86 +121,515 -> 121,132 +32,472 -> 32,960 +513,28 -> 513,299 +881,612 -> 881,415 +72,71 -> 977,976 +169,821 -> 111,821 +603,756 -> 254,756 +182,129 -> 182,824 +746,670 -> 942,670 +143,15 -> 72,86 +108,134 -> 963,989 +860,388 -> 834,362 +252,811 -> 473,811 +575,306 -> 575,368 +686,471 -> 686,38 +673,59 -> 673,861 +461,949 -> 491,949 +915,373 -> 330,958 +933,699 -> 588,699 +254,798 -> 254,498 +329,865 -> 329,926 +569,243 -> 659,243 +762,808 -> 921,967 +722,460 -> 68,460 +136,470 -> 355,470 +133,919 -> 56,842 +87,868 -> 853,102 +622,102 -> 446,102 +798,494 -> 135,494 +281,858 -> 281,172 +141,172 -> 765,796 +794,194 -> 102,886 +539,983 -> 539,895 +841,755 -> 841,365 +695,429 -> 166,958 +965,933 -> 899,933 +603,699 -> 603,708 +598,635 -> 844,635 +288,190 -> 288,946 +559,383 -> 423,383 +795,332 -> 409,718 +600,645 -> 478,645 +831,24 -> 905,24 +13,817 -> 606,224 +828,878 -> 96,146 +32,197 -> 32,891 +84,832 -> 84,756 +404,281 -> 404,781 +394,441 -> 489,536 +845,876 -> 589,876 +833,114 -> 833,834 +979,130 -> 979,238 +907,189 -> 396,700 +448,740 -> 714,474 +145,837 -> 100,837 +982,983 -> 38,39 +962,506 -> 962,764 +773,922 -> 975,922 +892,666 -> 904,654 +754,201 -> 459,496 +108,829 -> 108,894 +122,381 -> 122,484 +683,301 -> 630,354 +47,103 -> 897,953 +549,880 -> 942,487 +944,15 -> 44,915 +713,456 -> 713,402 +83,865 -> 239,865 +814,585 -> 814,105 +980,439 -> 685,439 \ No newline at end of file diff --git a/AOC2021.Test/Input/Day5_test.txt b/AOC2021.Test/Input/Day5_test.txt new file mode 100644 index 0000000..82029a1 --- /dev/null +++ b/AOC2021.Test/Input/Day5_test.txt @@ -0,0 +1,10 @@ +0,9 -> 5,9 +8,0 -> 0,8 +9,4 -> 3,4 +2,2 -> 2,1 +7,0 -> 7,4 +6,4 -> 2,0 +0,9 -> 2,9 +3,4 -> 1,4 +0,0 -> 8,8 +5,5 -> 8,2 \ No newline at end of file diff --git a/AOC2021.Test/Input/Day6_input.txt b/AOC2021.Test/Input/Day6_input.txt new file mode 100644 index 0000000..22e0632 --- /dev/null +++ b/AOC2021.Test/Input/Day6_input.txt @@ -0,0 +1 @@ +1,3,4,1,1,1,1,1,1,1,1,2,2,1,4,2,4,1,1,1,1,1,5,4,1,1,2,1,1,1,1,4,1,1,1,4,4,1,1,1,1,1,1,1,2,4,1,3,1,1,2,1,2,1,1,4,1,1,1,4,3,1,3,1,5,1,1,3,4,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,5,2,5,5,3,2,1,5,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,5,1,1,1,1,5,1,1,1,1,1,4,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,3,1,2,4,1,5,5,1,1,5,3,4,4,4,1,1,1,2,1,1,1,1,1,1,2,1,1,1,1,1,1,5,3,1,4,1,1,2,2,1,2,2,5,1,1,1,2,1,1,1,1,3,4,5,1,2,1,1,1,1,1,5,2,1,1,1,1,1,1,5,1,1,1,1,1,1,1,5,1,4,1,5,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,5,4,5,1,1,1,1,1,1,1,5,1,1,3,1,1,1,3,1,4,2,1,5,1,3,5,5,2,1,3,1,1,1,1,1,3,1,3,1,1,2,4,3,1,4,2,2,1,1,1,1,1,1,1,5,2,1,1,1,2 \ No newline at end of file diff --git a/AOC2021.Test/Input/Day6_test.txt b/AOC2021.Test/Input/Day6_test.txt new file mode 100644 index 0000000..72bfd5d --- /dev/null +++ b/AOC2021.Test/Input/Day6_test.txt @@ -0,0 +1 @@ +3,4,3,1,2 \ No newline at end of file diff --git a/AOC2021.Test/Input/Day7_input.txt b/AOC2021.Test/Input/Day7_input.txt new file mode 100644 index 0000000..f2c9fae --- /dev/null +++ b/AOC2021.Test/Input/Day7_input.txt @@ -0,0 +1 @@ +1101,1,29,67,1102,0,1,65,1008,65,35,66,1005,66,28,1,67,65,20,4,0,1001,65,1,65,1106,0,8,99,35,67,101,99,105,32,110,39,101,115,116,32,112,97,115,32,117,110,101,32,105,110,116,99,111,100,101,32,112,114,111,103,114,97,109,10,90,350,371,573,395,1345,2,660,190,88,16,88,168,1148,336,190,546,531,734,686,87,502,375,722,69,639,936,592,1084,264,299,287,603,1109,485,1081,1481,981,356,437,879,259,6,142,194,1428,1264,543,590,167,43,63,155,114,1061,594,1823,710,1607,305,457,135,277,302,162,75,95,1334,320,892,55,1080,5,390,1275,301,827,597,385,208,79,83,344,954,426,811,79,130,227,14,448,98,136,1000,408,858,263,144,860,552,1025,63,319,178,474,1709,234,1452,664,966,295,321,62,132,427,179,1705,110,34,373,367,1110,80,1842,659,268,10,791,378,1390,395,42,364,404,481,127,243,332,254,55,513,335,14,167,787,242,176,65,923,750,1281,583,124,139,146,453,228,418,860,975,75,84,132,945,1257,988,1179,92,48,1631,1267,689,243,950,1389,417,23,865,54,365,1253,476,952,1337,113,1226,383,353,33,90,249,4,512,206,501,465,821,257,1668,330,203,16,817,224,339,1389,1331,1230,538,221,1124,48,365,582,206,191,228,0,653,198,32,661,386,669,213,810,842,192,1451,51,21,609,203,557,8,124,339,597,273,299,187,1753,329,335,767,1404,306,138,192,580,1069,298,7,166,358,968,838,985,267,372,631,1597,113,803,523,567,405,50,30,1254,965,125,438,879,723,4,81,454,239,1495,394,677,424,519,224,1307,744,44,134,480,949,535,603,257,388,350,479,293,471,95,94,312,8,179,7,154,383,112,26,1694,15,245,435,364,148,594,778,316,1471,670,3,1021,64,142,97,500,58,124,311,392,489,277,863,859,1549,64,1759,116,258,245,595,108,800,89,29,1171,318,36,1529,691,238,622,191,130,1016,408,35,0,1078,186,95,83,287,188,275,1385,198,4,697,553,583,98,1506,1351,166,330,925,230,3,147,748,640,733,355,330,33,1084,753,53,690,245,436,1028,343,533,361,779,328,409,744,414,669,568,235,76,244,843,165,197,1693,6,18,110,48,279,832,702,32,1599,685,245,212,24,124,300,177,20,6,1035,1721,767,138,1116,3,296,1042,1335,347,215,377,1028,192,220,475,1323,9,663,738,88,367,187,56,263,19,80,4,466,1,696,128,571,1215,981,58,368,693,333,40,149,46,252,532,12,526,1171,302,112,1017,1262,807,332,16,715,569,1184,158,570,277,31,414,572,848,1633,784,357,529,286,1510,109,455,902,43,203,443,175,298,484,607,961,1114,295,9,781,487,183,846,336,577,6,723,1369,1484,301,1366,240,641,1937,14,354,458,22,1567,1169,683,544,708,538,12,683,872,7,209,65,18,936,40,511,297,860,732,748,449,549,861,110,106,21,58,201,665,26,604,140,1188,275,132,21,1079,20,648,120,1079,480,751,465,22,744,374,894,1628,3,367,945,19,373,185,28,711,251,0,1488,756,761,1424,757,69,15,649,280,116,102,122,71,79,18,934,596,463,779,216,1183,1354,18,1147,247,113,1379,686,524,45,1007,108,408,965,64,718,44,104,1510,143,161,46,34,871,329,992,559,503,1497,229,358,469,421,124,54,941,407,385,460,967,470,25,1552,109,992,6,331,345,225,23,48,712,483,1109,970,379,513,825,91,65,25,515,41,332,84,671,1318,505,772,21,1463,517,143,238,31,529,532,833,1671,110,766,44,257,36,458,1358,603,7,366,968,579,303,469,399,387,296,681,117,1089,931,1602,37,79,71,132,147,744,264,206,132,214,99,104,177,547,102,1550,771,1517,785,106,245,8,1602,298,15,533,451,339,351,448,241,199,128,1059,12,1,126,9,943,81,342,931,1007,499,1034,81,1483,98,782,1096,1050,952,185,1043,461,896,58,309,56,6,409,855,576,243,825,991,547,93,1721,604,580,355,11,854,0,222,496,671,935,148,1202,7,346,795,1409,1499,834,34,450,44,126,1203,18,779,1084,535,386,320,575,14,670,81,1036,1336,223,1054,1631,339,18,130,1002,131,36,998,462,559,1322,472,491,215,1402,1611,113,127,484,78,277,19,1104,71,220,40,586,555,489,611,267,507,632,47,786,59,1352,350,58,281,770,267,147,293,827,273,103,56,50,6,1224,1065,752,99,956,441,316,687,127,494,136,1336,1065,250,547,513,563,90,45,637,715,203,388,477,555,460,360,39,14,641,585,22,801,157,202,1152,505,227,572,599,659,361,1600,986,108,945,75,341,51,264,449,199,41,141,1258,870,1648,257,895,267,62,8,1208,166,142,618,375,1403 \ No newline at end of file diff --git a/AOC2021.Test/Input/Day7_test.txt b/AOC2021.Test/Input/Day7_test.txt new file mode 100644 index 0000000..8b029d8 --- /dev/null +++ b/AOC2021.Test/Input/Day7_test.txt @@ -0,0 +1 @@ +16,1,2,0,4,2,7,1,2,14 \ No newline at end of file diff --git a/AOC2021/Controllers/AdventOfCodeController.cs b/AOC2021/Controllers/AdventOfCodeController.cs index e08ab44..5a7e44e 100644 --- a/AOC2021/Controllers/AdventOfCodeController.cs +++ b/AOC2021/Controllers/AdventOfCodeController.cs @@ -23,6 +23,9 @@ namespace AOC2021.Controllers [Route("day2")] [Route("day3")] [Route("day4")] + [Route("day5")] + [Route("day6")] + [Route("day7")] public AOCResponse Day(AOCVersion version, [FromBody] string input, bool IgnoreLogMessages = false) { AOCRequest request = new AOCRequest() { Input = input, Version = version, IgnoreLogMessages = IgnoreLogMessages }; diff --git a/AOC2021/Days/Day5.cs b/AOC2021/Days/Day5.cs new file mode 100644 index 0000000..5851bd4 --- /dev/null +++ b/AOC2021/Days/Day5.cs @@ -0,0 +1,73 @@ +using AOC2021.Models; +using AOC2021.Models.Day5; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace AOC2021.Days +{ + public class Day5 : AOCDay + { + private Dictionary floor; + public Day5() + { + floor = new Dictionary(); + } + protected override AOCResponse ExecutePartA() + { + var lines = GetSplitInput().Select(x => new Line(x)); + foreach (var line in lines) + { + var pointsOnLine = line.GetPointsInHorizVertLine(); + foreach (var point in pointsOnLine) + { + GetVent(point.X, point.Y).OverlappingLines.Add(line); + } + } + + var overlap = floor.Values.Where(x => x.OverlappingLines.Count >= 2); + foreach (var o in overlap) + { + Log($"Over laps at {o.X},{o.Y} count of {o.OverlappingLines.Count}"); + } + this._response.Answer = overlap.Count().ToString(); + + return this._response; + } + + protected override AOCResponse ExecutePartB() + { + var lines = GetSplitInput().Select(x => new Line(x)); + foreach (var line in lines) + { + var pointsOnLine = line.GetPointsInAllDirections(); + foreach (var point in pointsOnLine) + { + GetVent(point.X, point.Y).OverlappingLines.Add(line); + } + } + + var overlap = floor.Values.Where(x => x.OverlappingLines.Count >= 2); + foreach (var o in overlap) + { + Log($"Over laps at {o.X},{o.Y} count of {o.OverlappingLines.Count}"); + } + this._response.Answer = overlap.Count().ToString(); + return this._response; + } + + private Vent GetVent(int x, int y) + { + Vent v; + if (floor.TryGetValue($"{x},{y}", out v)) + { + return v; //It found the vent + } + v = new Vent() { X = x, Y = y }; + floor.Add($"{x},{y}", v); + return v; + } + + } +} diff --git a/AOC2021/Days/Day6.cs b/AOC2021/Days/Day6.cs new file mode 100644 index 0000000..0b2e42e --- /dev/null +++ b/AOC2021/Days/Day6.cs @@ -0,0 +1,82 @@ +using AOC2021.Helper; +using AOC2021.Models; +using AOC2021.Models.Day6; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace AOC2021.Days +{ + public class Day6 : AOCDay + { + private int MODELING_DAYS = 80; + private List _fishes; + public Day6() + { + this._fishes = new List(); + } + + /// + /// First implementaiton uses a list and a fish class to keep track. Each fish is independent of one another + /// and we just sum up the number of fish objects we have regardless of the day that they are on. + /// + /// + protected override AOCResponse ExecutePartA() + { + var initialState = this._request.Input.Trim().Split(","); + foreach (var day in initialState) + _fishes.Add(new Fish(day.ToInt())); + + for (int i = 0; i < MODELING_DAYS; i++) + { + var newFishes = new List(); + foreach (var fish in _fishes) + { + var child = fish.DayPassed(); + if (child != null) + newFishes.Add(child); + } + foreach (var newFish in newFishes) + _fishes.Add(newFish); + Log($"Day {i + 1} Count {_fishes.Count}: " + + $"{_fishes.Where(x => x.Count == 0).Count()}, " + + $"{_fishes.Where(x => x.Count == 1).Count()}, " + + $"{_fishes.Where(x => x.Count == 2).Count()}, " + + $"{_fishes.Where(x => x.Count == 3).Count()}, " + + $"{_fishes.Where(x => x.Count == 4).Count()}, " + + $"{_fishes.Where(x => x.Count == 5).Count()}, " + + $"{_fishes.Where(x => x.Count == 6).Count()}, " + + $"{_fishes.Where(x => x.Count == 7).Count()}, " + + $"{_fishes.Where(x => x.Count == 8).Count()}, "); + } + + this._response.Answer = _fishes.Count.ToString(); + + return this._response; + } + + /// + /// More efficient way, have long[] array of 9 values (0-8). Each day that passes we move the + /// value from the later day into the previous day. When fish hits day 0 we do two things + /// 1) Add the number of 0 fishes back to day 6 (mothers returning back into circulation) + /// 2) Add the number of 0 fishes to day 8 indicating that they are babies. + /// This is a vastly more efficient way as we only keep track of the number of fishes per day, not + /// the other way around of days per fishies. + /// + /// + protected override AOCResponse ExecutePartB() + { + MODELING_DAYS = 256; + + var fishSchool = new FishSchool(this._request.Input.Trim()); //Create a new school of fish + + for (int i = 0; i < MODELING_DAYS; i++) + { + fishSchool.PassDay(); + Log($"Day {i+1} Count {fishSchool.NumberOfFishies()}: " + fishSchool.ToString()); + } + this._response.Answer = fishSchool.NumberOfFishies().ToString(); + return this._response; + } + } +} diff --git a/AOC2021/Days/Day7.cs b/AOC2021/Days/Day7.cs new file mode 100644 index 0000000..1ac9a54 --- /dev/null +++ b/AOC2021/Days/Day7.cs @@ -0,0 +1,61 @@ +using AOC2021.Helper; +using AOC2021.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace AOC2021.Days +{ + public class Day7 : AOCDay + { + private int[] _crabs; + protected override AOCResponse ExecutePartA() + { + CreateCrabs(); + int minFuel = int.MaxValue; + for (int position = _crabs.Min(); position < _crabs.Max(); position++) + { + int fuel = 0; + for (int i = 0; i < _crabs.Length; i++) + { + fuel += Math.Abs(position - _crabs[i]); + } + minFuel = Math.Min(minFuel, fuel); + Log($"For pos {position} it took {fuel}"); + } + this._response.Answer = minFuel.ToString(); + Log("Min Fuel is " + minFuel); + return this._response; + } + + protected override AOCResponse ExecutePartB() + { + CreateCrabs(); + int minFuel = int.MaxValue; + for (int position = _crabs.Min(); position < _crabs.Max(); position++) + { + int fuel = 0; + for (int i = 0; i < _crabs.Length; i++) + { + var spacesMoved= Math.Abs(position - _crabs[i]); + var fuelConsumed = ((spacesMoved * spacesMoved) + spacesMoved) / 2; //(n^2+n)/2 + fuel += fuelConsumed; + } + minFuel = Math.Min(minFuel, fuel); + Log($"For pos {position} it took {fuel}"); + } + this._response.Answer = minFuel.ToString(); + Log("Min Fuel is " + minFuel); + return this._response; + } + + private void CreateCrabs() + { + var crabPositions = this._request.Input.Trim().Split(","); + _crabs = new int[crabPositions.Length]; + for (int i = 0; i < crabPositions.Length; i++) + _crabs[i] = crabPositions[i].ToInt(); + } + } +} diff --git a/AOC2021/Models/AOCDay.cs b/AOC2021/Models/AOCDay.cs index 1fab357..6e00c2e 100644 --- a/AOC2021/Models/AOCDay.cs +++ b/AOC2021/Models/AOCDay.cs @@ -38,7 +38,7 @@ namespace AOC2021.Models break; } timer.Stop(); - this._response.RunTime = timer.ElapsedMilliseconds.ToString(); + this._response.RunTime = timer.ElapsedTicks.ToString(); this._response.Status = true; } catch (Exception e) diff --git a/AOC2021/Models/AOCResponse.cs b/AOC2021/Models/AOCResponse.cs index c18924a..a894cc2 100644 --- a/AOC2021/Models/AOCResponse.cs +++ b/AOC2021/Models/AOCResponse.cs @@ -19,14 +19,15 @@ namespace AOC2021.Models public IEnumerable Debug { get; set; } [DataMember] public string StackTrace { get; set; } - private string timeInMs; + private string timeInTicks; [DataMember] - public string RunTime { get { return FormatRunTime(); } set { timeInMs = value; } } + public string RunTime { get { return FormatRunTime(); } set { timeInTicks = value; } } public string FormatRunTime() { - var ts = TimeSpan.FromMilliseconds(Convert.ToDouble(timeInMs)); - return $"Run time is {ts.Hours}h {ts.Minutes}min {ts.Seconds}sec {ts.Milliseconds}ms"; + var ts = TimeSpan.FromTicks((long)Convert.ToDouble(timeInTicks)); + var microseconds = (ts.Ticks - (ts.Milliseconds * TimeSpan.TicksPerMillisecond)) / (TimeSpan.TicksPerMillisecond / 1000); + return $"Run time is {ts.Minutes}min {ts.Seconds}sec {ts.Milliseconds}ms {microseconds}µs"; } } diff --git a/AOC2021/Models/Day5/Line.cs b/AOC2021/Models/Day5/Line.cs new file mode 100644 index 0000000..8d6c15d --- /dev/null +++ b/AOC2021/Models/Day5/Line.cs @@ -0,0 +1,66 @@ +using AOC2021.Helper; +using System; +using System.Collections.Generic; +using System.Drawing; + +namespace AOC2021.Models.Day5 +{ + public class Line + { + public string LineEntry { get; set; } + public Point Starting { get; set; } + public Point Ending { get; set; } + + public Line(string line) + { + this.LineEntry = line; + var split = line.Split("->"); + + var xValue = split[0].Trim().Split(","); + var yValue = split[1].Trim().Split(","); + + Starting = new Point(xValue[0].ToInt(), xValue[1].ToInt()); + Ending = new Point(yValue[0].ToInt(), yValue[1].ToInt()); + } + + public List GetPointsInHorizVertLine() + { + var list = new List(); + if (Starting.X == Ending.X) + { + var start = Math.Min(Starting.Y, Ending.Y); + var end = Math.Max(Starting.Y, Ending.Y); + for (int i = start; i <= end; i++) + list.Add(new Point(Starting.X, i)); + } + else if (Starting.Y == Ending.Y) + { + var start = Math.Min(Starting.X, Ending.X); + var end = Math.Max(Starting.X, Ending.X); + for (int i = start; i <= end; i++) + list.Add(new Point(i, Starting.Y)); + } + + return list; + } + + public List GetPointsInAllDirections() + { + var list = GetPointsInHorizVertLine(); + if (list.Count != 0) + return list; + + var m = (Ending.Y - Starting.Y) / (Ending.X - Starting.X); + var b = Ending.Y - (m * Ending.X); + + int minX = Math.Min(Starting.X, Ending.X); + int maxX = Math.Max(Starting.X, Ending.X); + for (int x = minX; x <= maxX; x++) + { + var y = (m * x) + b; + list.Add(new Point(x, y)); + } + return list; + } + } +} diff --git a/AOC2021/Models/Day5/Vent.cs b/AOC2021/Models/Day5/Vent.cs new file mode 100644 index 0000000..8220ab9 --- /dev/null +++ b/AOC2021/Models/Day5/Vent.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace AOC2021.Models.Day5 +{ + public class Vent + { + public int X { get; set; } + public int Y { get; set; } + + public List OverlappingLines { get; set; } + + public Vent() + { + this.OverlappingLines = new List(); + } + } +} diff --git a/AOC2021/Models/Day6/Fish.cs b/AOC2021/Models/Day6/Fish.cs new file mode 100644 index 0000000..8c40aee --- /dev/null +++ b/AOC2021/Models/Day6/Fish.cs @@ -0,0 +1,22 @@ +namespace AOC2021.Models.Day6 +{ + public class Fish + { + public int Count { get; set; } + public Fish (int count) + { + this.Count = count; + } + + public Fish DayPassed() + { + if (Count == 0) + { + Count = 6; + return new Fish(8); + } + Count--; + return null; + } + } +} diff --git a/AOC2021/Models/Day6/FishSchool.cs b/AOC2021/Models/Day6/FishSchool.cs new file mode 100644 index 0000000..c6757dc --- /dev/null +++ b/AOC2021/Models/Day6/FishSchool.cs @@ -0,0 +1,49 @@ +using AOC2021.Helper; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace AOC2021.Models.Day6 +{ + public class FishSchool + { + public long[] _fishes; + public FishSchool(string initialState) + { + _fishes = new long[9]; + foreach (var day in initialState.Split(",")) + { + _fishes[day.ToInt()] += 1; + } + } + + public void PassDay() + { + var motherFishies = _fishes[0]; + for (int i = 1; i < _fishes.Length; i++) + { + _fishes[i - 1] = _fishes[i]; + } + + _fishes[6] += motherFishies; //Mothers go back to 6 days + _fishes[8] = motherFishies; //These are the children + + } + + public override string ToString() + { + return string.Join(", ", _fishes); + } + + public long NumberOfFishies() + { + long val = 0; + foreach (var fish in _fishes) + { + val += fish; + } + return val; + } + } +}