chore(*): Move all C# solutions into a C# folder to make it clearer which languages are used

This commit is contained in:
Josh Creek
2024-12-01 11:19:35 +00:00
parent 1a4d7a2d95
commit 9d53354d14
71 changed files with 7 additions and 0 deletions
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
@@ -0,0 +1 @@
bgvyzdsv
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,300 @@
turn off 660,55 through 986,197
turn off 341,304 through 638,850
turn off 199,133 through 461,193
toggle 322,558 through 977,958
toggle 537,781 through 687,941
turn on 226,196 through 599,390
turn on 240,129 through 703,297
turn on 317,329 through 451,798
turn on 957,736 through 977,890
turn on 263,530 through 559,664
turn on 158,270 through 243,802
toggle 223,39 through 454,511
toggle 544,218 through 979,872
turn on 313,306 through 363,621
toggle 173,401 through 496,407
toggle 333,60 through 748,159
turn off 87,577 through 484,608
turn on 809,648 through 826,999
toggle 352,432 through 628,550
turn off 197,408 through 579,569
turn off 1,629 through 802,633
turn off 61,44 through 567,111
toggle 880,25 through 903,973
turn on 347,123 through 864,746
toggle 728,877 through 996,975
turn on 121,895 through 349,906
turn on 888,547 through 931,628
toggle 398,782 through 834,882
turn on 966,850 through 989,953
turn off 891,543 through 914,991
toggle 908,77 through 916,117
turn on 576,900 through 943,934
turn off 580,170 through 963,206
turn on 184,638 through 192,944
toggle 940,147 through 978,730
turn off 854,56 through 965,591
toggle 717,172 through 947,995
toggle 426,987 through 705,998
turn on 987,157 through 992,278
toggle 995,774 through 997,784
turn off 796,96 through 845,182
turn off 451,87 through 711,655
turn off 380,93 through 968,676
turn on 263,468 through 343,534
turn on 917,936 through 928,959
toggle 478,7 through 573,148
turn off 428,339 through 603,624
turn off 400,880 through 914,953
toggle 679,428 through 752,779
turn off 697,981 through 709,986
toggle 482,566 through 505,725
turn off 956,368 through 993,516
toggle 735,823 through 783,883
turn off 48,487 through 892,496
turn off 116,680 through 564,819
turn on 633,865 through 729,930
turn off 314,618 through 571,922
toggle 138,166 through 936,266
turn on 444,732 through 664,960
turn off 109,337 through 972,497
turn off 51,432 through 77,996
turn off 259,297 through 366,744
toggle 801,130 through 917,544
toggle 767,982 through 847,996
turn on 216,507 through 863,885
turn off 61,441 through 465,731
turn on 849,970 through 944,987
toggle 845,76 through 852,951
toggle 732,615 through 851,936
toggle 251,128 through 454,778
turn on 324,429 through 352,539
toggle 52,450 through 932,863
turn off 449,379 through 789,490
turn on 317,319 through 936,449
toggle 887,670 through 957,838
toggle 671,613 through 856,664
turn off 186,648 through 985,991
turn off 471,689 through 731,717
toggle 91,331 through 750,758
toggle 201,73 through 956,524
toggle 82,614 through 520,686
toggle 84,287 through 467,734
turn off 132,367 through 208,838
toggle 558,684 through 663,920
turn on 237,952 through 265,997
turn on 694,713 through 714,754
turn on 632,523 through 862,827
turn on 918,780 through 948,916
turn on 349,586 through 663,976
toggle 231,29 through 257,589
toggle 886,428 through 902,993
turn on 106,353 through 236,374
turn on 734,577 through 759,684
turn off 347,843 through 696,912
turn on 286,699 through 964,883
turn on 605,875 through 960,987
turn off 328,286 through 869,461
turn off 472,569 through 980,848
toggle 673,573 through 702,884
turn off 398,284 through 738,332
turn on 158,50 through 284,411
turn off 390,284 through 585,663
turn on 156,579 through 646,581
turn on 875,493 through 989,980
toggle 486,391 through 924,539
turn on 236,722 through 272,964
toggle 228,282 through 470,581
toggle 584,389 through 750,761
turn off 899,516 through 900,925
turn on 105,229 through 822,846
turn off 253,77 through 371,877
turn on 826,987 through 906,992
turn off 13,152 through 615,931
turn on 835,320 through 942,399
turn on 463,504 through 536,720
toggle 746,942 through 786,998
turn off 867,333 through 965,403
turn on 591,477 through 743,692
turn off 403,437 through 508,908
turn on 26,723 through 368,814
turn on 409,485 through 799,809
turn on 115,630 through 704,705
turn off 228,183 through 317,220
toggle 300,649 through 382,842
turn off 495,365 through 745,562
turn on 698,346 through 744,873
turn on 822,932 through 951,934
toggle 805,30 through 925,421
toggle 441,152 through 653,274
toggle 160,81 through 257,587
turn off 350,781 through 532,917
toggle 40,583 through 348,636
turn on 280,306 through 483,395
toggle 392,936 through 880,955
toggle 496,591 through 851,934
turn off 780,887 through 946,994
turn off 205,735 through 281,863
toggle 100,876 through 937,915
turn on 392,393 through 702,878
turn on 956,374 through 976,636
toggle 478,262 through 894,775
turn off 279,65 through 451,677
turn on 397,541 through 809,847
turn on 444,291 through 451,586
toggle 721,408 through 861,598
turn on 275,365 through 609,382
turn on 736,24 through 839,72
turn off 86,492 through 582,712
turn on 676,676 through 709,703
turn off 105,710 through 374,817
toggle 328,748 through 845,757
toggle 335,79 through 394,326
toggle 193,157 through 633,885
turn on 227,48 through 769,743
toggle 148,333 through 614,568
toggle 22,30 through 436,263
toggle 547,447 through 688,969
toggle 576,621 through 987,740
turn on 711,334 through 799,515
turn on 541,448 through 654,951
toggle 792,199 through 798,990
turn on 89,956 through 609,960
toggle 724,433 through 929,630
toggle 144,895 through 201,916
toggle 226,730 through 632,871
turn off 760,819 through 828,974
toggle 887,180 through 940,310
toggle 222,327 through 805,590
turn off 630,824 through 885,963
turn on 940,740 through 954,946
turn on 193,373 through 779,515
toggle 304,955 through 469,975
turn off 405,480 through 546,960
turn on 662,123 through 690,669
turn off 615,238 through 750,714
turn on 423,220 through 930,353
turn on 329,769 through 358,970
toggle 590,151 through 704,722
turn off 884,539 through 894,671
toggle 449,241 through 984,549
toggle 449,260 through 496,464
turn off 306,448 through 602,924
turn on 286,805 through 555,901
toggle 722,177 through 922,298
toggle 491,554 through 723,753
turn on 80,849 through 174,996
turn off 296,561 through 530,856
toggle 653,10 through 972,284
toggle 529,236 through 672,614
toggle 791,598 through 989,695
turn on 19,45 through 575,757
toggle 111,55 through 880,871
turn off 197,897 through 943,982
turn on 912,336 through 977,605
toggle 101,221 through 537,450
turn on 101,104 through 969,447
toggle 71,527 through 587,717
toggle 336,445 through 593,889
toggle 214,179 through 575,699
turn on 86,313 through 96,674
toggle 566,427 through 906,888
turn off 641,597 through 850,845
turn on 606,524 through 883,704
turn on 835,775 through 867,887
toggle 547,301 through 897,515
toggle 289,930 through 413,979
turn on 361,122 through 457,226
turn on 162,187 through 374,746
turn on 348,461 through 454,675
turn off 966,532 through 985,537
turn on 172,354 through 630,606
turn off 501,880 through 680,993
turn off 8,70 through 566,592
toggle 433,73 through 690,651
toggle 840,798 through 902,971
toggle 822,204 through 893,760
turn off 453,496 through 649,795
turn off 969,549 through 990,942
turn off 789,28 through 930,267
toggle 880,98 through 932,434
toggle 568,674 through 669,753
turn on 686,228 through 903,271
turn on 263,995 through 478,999
toggle 534,675 through 687,955
turn off 342,434 through 592,986
toggle 404,768 through 677,867
toggle 126,723 through 978,987
toggle 749,675 through 978,959
turn off 445,330 through 446,885
turn off 463,205 through 924,815
turn off 417,430 through 915,472
turn on 544,990 through 912,999
turn off 201,255 through 834,789
turn off 261,142 through 537,862
turn off 562,934 through 832,984
turn off 459,978 through 691,980
turn off 73,911 through 971,972
turn on 560,448 through 723,810
turn on 204,630 through 217,854
turn off 91,259 through 611,607
turn on 877,32 through 978,815
turn off 950,438 through 974,746
toggle 426,30 through 609,917
toggle 696,37 through 859,201
toggle 242,417 through 682,572
turn off 388,401 through 979,528
turn off 79,345 through 848,685
turn off 98,91 through 800,434
toggle 650,700 through 972,843
turn off 530,450 through 538,926
turn on 428,559 through 962,909
turn on 78,138 through 92,940
toggle 194,117 through 867,157
toggle 785,355 through 860,617
turn off 379,441 through 935,708
turn off 605,133 through 644,911
toggle 10,963 through 484,975
turn off 359,988 through 525,991
turn off 509,138 through 787,411
toggle 556,467 through 562,773
turn on 119,486 through 246,900
turn on 445,561 through 794,673
turn off 598,681 through 978,921
turn off 974,230 through 995,641
turn off 760,75 through 800,275
toggle 441,215 through 528,680
turn off 701,636 through 928,877
turn on 165,753 through 202,780
toggle 501,412 through 998,516
toggle 161,105 through 657,395
turn on 113,340 through 472,972
toggle 384,994 through 663,999
turn on 969,994 through 983,997
turn on 519,600 through 750,615
turn off 363,899 through 948,935
turn on 271,845 through 454,882
turn off 376,528 through 779,640
toggle 767,98 through 854,853
toggle 107,322 through 378,688
turn off 235,899 through 818,932
turn on 445,611 through 532,705
toggle 629,387 through 814,577
toggle 112,414 through 387,421
toggle 319,184 through 382,203
turn on 627,796 through 973,940
toggle 602,45 through 763,151
turn off 441,375 through 974,545
toggle 871,952 through 989,998
turn on 717,272 through 850,817
toggle 475,711 through 921,882
toggle 66,191 through 757,481
turn off 50,197 through 733,656
toggle 83,575 through 915,728
turn on 777,812 through 837,912
turn on 20,984 through 571,994
turn off 446,432 through 458,648
turn on 715,871 through 722,890
toggle 424,675 through 740,862
toggle 580,592 through 671,900
toggle 296,687 through 906,775
+82
View File
@@ -0,0 +1,82 @@
using System.Reflection;
namespace AOC.Tests.Y2015
{
[TestFixture, Parallelizable(ParallelScope.All)]
public class Day01
{
protected string GetThisClassName() { return this.GetType().Name; }
private string realData;
[SetUp]
public void Setup()
{
realData = File.ReadAllLines(Path.Combine(TestContext.CurrentContext.TestDirectory, "Y2015", "Data", $"{GetThisClassName()}.dat"))[0];
}
[TestCase("(())", 0)] // Example data
[TestCase("()()", 0)] // Example data
[TestCase("(((", 3)] // Example data
[TestCase("(()(()(", 3)] // Example data
[TestCase("))(((((", 3)] // Example data
[TestCase("())", -1)] // Example data
[TestCase("))(", -1)] // Example data
[TestCase(")))", -3)] // Example data
[TestCase(")())())", -3)] // Example data
[TestCase(null, 232)] // The actual answer
public void Part1(string? input, int? expected)
{
var lines = input != null ? input : realData;
int floorsUp = lines.Count(x => x == '(');
int floorsDown = lines.Count(x => x == ')');
var result = floorsUp - floorsDown;
if (expected != null)
{
Assert.That(result, Is.EqualTo(expected.Value));
}
Console.WriteLine($"Part 1: {result}");
}
[TestCase(")", 1)] // Example data
[TestCase("()())", 5)]
[TestCase(null, 1783)] // The actual answer
public void Part2(string? input, int? expected)
{
var lines = input != null ? input : realData;
var currentFloor = 0;
int? result = null;
foreach (var (instruction, index) in lines.WithIndex())
{
if (instruction is '(')
{
currentFloor += 1;
}
else if (instruction is ')')
{
currentFloor -= 1;
}
if (currentFloor is -1)
{
// Zero-indexed, so need to increment
result = index + 1;
break;
}
}
if (expected != null)
{
Assert.That(result, Is.EqualTo(expected.Value));
}
Console.WriteLine($"Part 2: {result}");
}
}
}
+89
View File
@@ -0,0 +1,89 @@
using System;
using System.Reflection;
namespace AOC.Tests.Y2015
{
[TestFixture, Parallelizable(ParallelScope.All)]
public class Day02
{
protected string GetThisClassName() { return this.GetType().Name; }
private string[] realData;
[SetUp]
public void Setup()
{
realData = File.ReadAllLines(Path.Combine(TestContext.CurrentContext.TestDirectory, "Y2015", "Data", $"{GetThisClassName()}.dat"));
}
[TestCase("2x3x4", 58)]
[TestCase("1x1x10", 43)]
[TestCase(null, 1606483)] // The actual answer
public void Part1(string? input, int? expected)
{
string[] lines = input != null ? new[] { input } : realData;
int totalWrappingPaper = 0;
foreach (var present in lines)
{
string[] dimensions = present.Split('x');
int length = Int32.Parse(dimensions[0]);
int width = Int32.Parse(dimensions[1]);
int height = Int32.Parse(dimensions[2]);
int sideAArea = length * width;
int sideBArea = width * height;
int sideCArea = height * length;
int smallestSideArea = new List<int>() { sideAArea, sideBArea, sideCArea }.Min();
int totalPaperForPresent = (2 * sideAArea) + (2 * sideBArea) + (2 * sideCArea) + smallestSideArea;
totalWrappingPaper += totalPaperForPresent;
}
if (expected != null)
{
Assert.That(totalWrappingPaper, Is.EqualTo(expected.Value));
}
Console.WriteLine($"Part 1: {totalWrappingPaper}");
}
[TestCase("2x3x4", 34)]
[TestCase("1x1x10", 14)]
[TestCase(null, 3842356)] // The actual answer
public void Part2(string? input, int? expected)
{
string[] lines = input != null ? new[] { input } : realData;
var totalRibbon = 0;
foreach (var present in lines)
{
string[] dimensions = present.Split('x');
int[] dimensionsInts = Array.ConvertAll(dimensions, s => int.Parse(s));
int length = dimensionsInts[0];
int width = dimensionsInts[1];
int height = dimensionsInts[2];
Array.Sort(dimensionsInts);
var smallestPerimeter = (2 * dimensionsInts[0]) + (2 * dimensionsInts[1]);
var cubicFeetVolume = length * width * height;
var totalRibbonForPresent = smallestPerimeter + cubicFeetVolume;
totalRibbon += totalRibbonForPresent;
}
if (expected != null)
{
Assert.That(totalRibbon, Is.EqualTo(expected.Value));
}
Console.WriteLine($"Part 2: {totalRibbon}");
}
}
}
+197
View File
@@ -0,0 +1,197 @@
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Metadata.Ecma335;
namespace AOC.Tests.Y2015
{
[TestFixture, Parallelizable(ParallelScope.All)]
public class Day03
{
protected string GetThisClassName() { return this.GetType().Name; }
private string realData;
private class House
{
public int VisitedCount { get; set; }
public int X { get; set; }
public int Y { get; set; }
};
[SetUp]
public void Setup()
{
realData = File.ReadAllLines(Path.Combine(TestContext.CurrentContext.TestDirectory, "Y2015", "Data", $"{GetThisClassName()}.dat"))[0];
}
private void DeliverPresentToHouse(int x, int y, ref List<House> houses)
{
if (houses.FindIndex(h => h.X == x && h.Y ==y) > -1)
{
foreach (House house in houses.Where(h => h.X == x && h.Y == y))
{
house.VisitedCount += 1;
}
}
else
{
House house = new House()
{
VisitedCount = 1,
X = x,
Y = y,
};
houses.Add(house);
}
}
[TestCase(">", 2)]
[TestCase("^>v<", 4)]
[TestCase("^v^v^v^v^v", 2)]
[TestCase(null, 2565)] // The actual answer
public void Part1(string? input, int? expected)
{
string lines = input != null ? input : realData;
int currentX = 0;
int currentY = 0;
List<House> houses = new List<House>();
House firstHouse = new House()
{
VisitedCount = 1,
X = currentX,
Y = currentY,
};
houses.Add(firstHouse);
foreach (char movement in lines)
{
switch (movement)
{
case '^':
// North
currentY += 1;
break;
case 'v':
// South
currentY -= 1;
break;
case '<':
// East
currentX += 1;
break;
case '>':
// West
currentX -= 1;
break;
}
DeliverPresentToHouse(currentX, currentY, ref houses);
}
int numberOfHousesVisited = houses.Count;
if (expected != null)
{
Assert.That(numberOfHousesVisited, Is.EqualTo(expected.Value));
}
Console.WriteLine($"Part 1: {numberOfHousesVisited}");
}
[TestCase("^v", 3)]
[TestCase("^>v<", 3)]
[TestCase("^v^v^v^v^v", 11)]
[TestCase(null, 2639)] // The actual answer
public void Part2(string? input, int? expected)
{
string lines = input != null ? input : realData;
int currentX = 0;
int currentY = 0;
int roboSantacurrentX = 0;
int roboSantacurrentY = 0;
List<House> houses = new List<House>();
House firstHouse = new House()
{
VisitedCount = 2, // Visited by both Santa and Robo-Santa
X = currentX,
Y = currentY,
};
houses.Add(firstHouse);
bool isSanta = true;
foreach (char movement in lines)
{
switch (movement)
{
case '^':
// North
if (isSanta)
{
currentY += 1;
}
else
{
roboSantacurrentY += 1;
}
break;
case 'v':
// South
if (isSanta)
{
currentY -= 1;
}
else
{
roboSantacurrentY -= 1;
}
break;
case '<':
// East
if (isSanta)
{
currentX += 1;
}
else
{
roboSantacurrentX += 1;
}
break;
case '>':
// West
if (isSanta)
{
currentX -= 1;
}
else
{
roboSantacurrentX -= 1;
}
break;
}
if (isSanta)
{
DeliverPresentToHouse(currentX, currentY, ref houses);
isSanta = false;
}
else
{
DeliverPresentToHouse(roboSantacurrentX, roboSantacurrentY, ref houses);
isSanta = true;
}
}
int numberOfHousesVisited = houses.Count;
if (expected != null)
{
Assert.That(numberOfHousesVisited, Is.EqualTo(expected.Value));
}
Console.WriteLine($"Part 2: {numberOfHousesVisited}");
}
}
}
+78
View File
@@ -0,0 +1,78 @@
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
namespace AOC.Tests.Y2015
{
[TestFixture, Parallelizable(ParallelScope.All)]
public class Day04
{
protected string GetThisClassName() { return this.GetType().Name; }
private string realData;
[SetUp]
public void Setup()
{
realData = File.ReadAllLines(Path.Combine(TestContext.CurrentContext.TestDirectory, "Y2015", "Data", $"{GetThisClassName()}.dat"))[0];
}
private int CreateMD5Hash(string input, string startingString)
{
int result = 0;
MD5 md5 = MD5.Create();
while (true)
{
byte[] inputBytes = new UTF8Encoding().GetBytes($"{input}{result}");
byte[] hashBytes = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
foreach (var t in hashBytes)
{
sb.Append(t.ToString("X2"));
}
if (sb.ToString().StartsWith(startingString))
{
break;
}
result += 1;
}
return result;
}
[TestCase("abcdef", 609043)]
[TestCase("pqrstuv", 1048970)]
[TestCase(null, 254575)] // The actual answer
public void Part1(string? input, int? expected)
{
string lines = input != null ? input : realData;
var i = CreateMD5Hash(lines, "00000");
if (expected != null)
{
Assert.That(i, Is.EqualTo(expected.Value));
}
Console.WriteLine($"Part 1: {i}");
}
[TestCase(null, 1038736)] // The actual answer
public void Part2(string? input, int? expected)
{
string lines = input != null ? input : realData;
var i = CreateMD5Hash(lines, "000000");
if (expected != null)
{
Assert.That(i, Is.EqualTo(expected.Value));
}
Console.WriteLine($"Part 2: {i}");
}
}
}
+191
View File
@@ -0,0 +1,191 @@
using Creek.HelpfulExtensions;
using System.Reflection;
using System.Text.RegularExpressions;
namespace AOC.Tests.Y2015
{
[TestFixture, Parallelizable(ParallelScope.All)]
public class Day05
{
protected string GetThisClassName() { return this.GetType().Name; }
private string[] realData;
[SetUp]
public void Setup()
{
realData = File.ReadAllLines(Path.Combine(TestContext.CurrentContext.TestDirectory, "Y2015", "Data", $"{GetThisClassName()}.dat"));
}
private static bool HasRepeatedCharacters(string input)
{
bool hasRepeatedCharacters = false;
if (input.Length >= 2)
{
for (int index = 0; index < input.Length - 1; index++)
{
if (input[index] == input[index + 1])
{
hasRepeatedCharacters = true;
}
}
}
return hasRepeatedCharacters;
}
private static bool IsNice(string checkString)
{
// Count non-unique vowels
Regex rx = new("[aeiou]");
int vowelCount = rx.Matches(checkString).Count;
// Check for at least one letter appearing twice in a row
bool hasRepeatedCharacters = HasRepeatedCharacters(checkString);
// Check it does not contain the strings ab, cd, pq, or xy
bool containsForbiddenStrings = checkString.Contains("ab")
|| checkString.Contains("cd")
|| checkString.Contains("pq")
|| checkString.Contains("xy");
return vowelCount >= 3 && hasRepeatedCharacters && !containsForbiddenStrings;
}
private class Pair
{
public int LeftIndex { get; set; }
public int RightIndex { get; set; }
public string? Text { get; set; }
}
private static bool TwiceWithoutOverlap(string checkString)
{
// It contains a pair of any two letters that appears at least twice in the string without overlapping,
// like xyxy (xy) or aabcdefgaa (aa), but not like aaa (aa, but it overlaps).
// Split the string into 'pairs'
List<Pair> pairs = new();
for (int i = 0; i < checkString.Length - 1; i++)
{
Pair pair = new()
{
LeftIndex = i,
RightIndex = i + 1,
Text = $"{checkString[i]}{checkString[i + 1]}",
};
pairs.Add(pair);
}
List<Pair> possibleNonOverlappingPairs = new();
// Check if any of those pairs occurs more than once
foreach (Pair pair in pairs)
{
int count = Regex.Matches(checkString, pair.Text).Count;
if (count > 1)
{
possibleNonOverlappingPairs.Add(pair);
}
}
// Check if any do not overlap, if they don't then return true
foreach (Pair pair in possibleNonOverlappingPairs)
{
if (possibleNonOverlappingPairs.Any(p =>
p.Text == pair.Text
&& (p.LeftIndex != pair.RightIndex || p.RightIndex != pair.LeftIndex)
))
{
return true;
}
}
return false;
}
private static bool CheckRepeatedCharacters(string checkString)
{
// It contains at least one letter which repeats with exactly one letter between them, like xyx, abcdefeghi (efe), or even aaa.
for (int i = 0; i < checkString.Length; i++)
{
if ((i + 2) < checkString.Length)
{
if (checkString[i] == checkString[i + 2] && checkString[i] != checkString[i + 1])
{
return true;
}
}
}
return false;
}
private static bool IsNiceV2(string checkString)
{
// It contains a pair of any two letters that appears at least twice in the string without overlapping, like xyxy (xy) or aabcdefgaa (aa), but not like aaa (aa, but it overlaps).
bool twiceWithoutOverlapping = TwiceWithoutOverlap(checkString);
// It contains at least one letter which repeats with exactly one letter between them, like xyx, abcdefeghi (efe), or even aaa.
bool hasRepeatedCharacters = CheckRepeatedCharacters(checkString);
return twiceWithoutOverlapping && hasRepeatedCharacters;
}
[TestCase("ugknbfddgicrmopn", 1)]
[TestCase("aaa", 1)]
[TestCase("jchzalrnumimnmhp", 0)]
[TestCase("haegwjzuvuyypxyu", 0)]
[TestCase("dvszwmarrgswjxmb", 0)]
[TestCase(null, 258)] // The actual answer
public void Part1(string? input, int? expected)
{
string[] lines = input != null ? new[] { input } : realData;
int niceCount = 0;
foreach (string line in lines)
{
if (IsNice(line))
{
niceCount += 1;
}
}
if (expected != null)
{
Assert.That(niceCount, Is.EqualTo(expected.Value));
}
Console.WriteLine($"Part 1: {niceCount}");
}
[TestCase("qjhvhtzxzqqjkmpb", 1)]
[TestCase("xxyxx", 1)]
[TestCase("uurcxstgmygtbstg", 0)]
[TestCase("ieodomkazucvgmuy", 0)]
[TestCase(null, 53)] // The actual answer
public void Part2(string? input, int? expected)
{
string[] lines = input != null ? new[] { input } : realData;
int niceCount = 0;
foreach (string line in lines)
{
if (IsNiceV2(line))
{
niceCount += 1;
}
}
if (expected != null)
{
Assert.That(niceCount, Is.EqualTo(expected.Value));
}
Console.WriteLine($"Part 2: {niceCount}");
}
}
}
+145
View File
@@ -0,0 +1,145 @@
using Newtonsoft.Json.Linq;
using System;
using System.Reflection;
namespace AOC.Tests.Y2015
{
[TestFixture, Parallelizable(ParallelScope.All)]
public class Day06
{
protected string GetThisClassName() { return this.GetType().Name; }
private string[] realData;
[SetUp]
public void Setup()
{
realData = File.ReadAllLines(Path.Combine(TestContext.CurrentContext.TestDirectory, "Y2015", "Data", $"{GetThisClassName()}.dat"));
}
private enum Instructions
{
Toggle = 7,
TurnOn = 8,
TurnOff = 9,
}
private void OperateLights(string instruction, ref bool[,] lights)
{
Instructions currentInstruction = instruction switch
{
string s when s.StartsWith("toggle") => Instructions.Toggle,
string s when s.StartsWith("turn on") => Instructions.TurnOn,
string s when s.StartsWith("turn off") => Instructions.TurnOff,
_ => throw new InvalidDataException($"Bad instruction -> {instruction}"),
};
// e.g. turn on 0,0 through 999,999
string[] instructionParts = instruction.Substring((int)currentInstruction).Split(' ');
int startX = int.Parse(instructionParts[0].Substring(0, instructionParts[0].IndexOf(",")));
int startY = int.Parse(instructionParts[0].Substring(instructionParts[0].IndexOf(",") + 1));
int endX = int.Parse(instructionParts[2].Substring(0, instructionParts[2].IndexOf(",")));
int endY = int.Parse(instructionParts[2].Substring(instructionParts[2].IndexOf(",") + 1));
for (int i = startX; i <= endX; i++)
{
for (int j = startY; j <= endY; j++)
{
lights[i, j] = currentInstruction switch
{
Instructions.Toggle => !lights[i, j],
Instructions.TurnOn => true,
Instructions.TurnOff => false,
_ => throw new NotImplementedException(),
};
}
}
}
private void OperateScalingLights(string instruction, ref int[,] lights)
{
Instructions currentInstruction = instruction switch
{
string s when s.StartsWith("toggle") => Instructions.Toggle,
string s when s.StartsWith("turn on") => Instructions.TurnOn,
string s when s.StartsWith("turn off") => Instructions.TurnOff,
_ => throw new InvalidDataException($"Bad instruction -> {instruction}"),
};
// e.g. turn on 0,0 through 999,999
string[] instructionParts = instruction.Substring((int)currentInstruction).Split(' ');
int startX = int.Parse(instructionParts[0].Substring(0, instructionParts[0].IndexOf(",")));
int startY = int.Parse(instructionParts[0].Substring(instructionParts[0].IndexOf(",") + 1));
int endX = int.Parse(instructionParts[2].Substring(0, instructionParts[2].IndexOf(",")));
int endY = int.Parse(instructionParts[2].Substring(instructionParts[2].IndexOf(",") + 1));
for (int i = startX; i <= endX; i++)
{
for (int j = startY; j <= endY; j++)
{
lights[i, j] = currentInstruction switch
{
Instructions.Toggle => lights[i, j] += 2,
Instructions.TurnOn => lights[i, j] += 1,
Instructions.TurnOff => lights[i, j] > 0 ? lights[i, j] -= 1 : lights[i, j] = 0,
_ => throw new NotImplementedException(),
};
}
}
}
[TestCase("turn on 0,0 through 999,999", 1000000)]
[TestCase("toggle 0,0 through 999,0", 1000)]
[TestCase("turn on 0,0 through 999,999/nturn off 499,499 through 500,500", 999996)]
[TestCase(null, 400410)] // The actual answer
public void Part1(string? input, int? expected)
{
string[] lines = input != null ? input.Split("/n") : realData;
bool[,] lights = new bool[1000, 1000];
foreach (string line in lines)
{
OperateLights(line, ref lights);
}
IEnumerable<bool> lightsOn = from bool light in lights
where light
select light;
int lightsOnCount = lightsOn.Count();
if (expected != null)
{
Assert.That(lightsOnCount, Is.EqualTo(expected.Value));
}
Console.WriteLine($"Part 1: {lightsOnCount}");
}
[TestCase("turn on 0,0 through 0,0", 1)]
[TestCase("toggle 0,0 through 999,999", 2000000)]
[TestCase(null, 15343601)] // The actual answer
public void Part2(string? input, int? expected)
{
string[] lines = input != null ? input.Split("/n") : realData;
int[,] lights = new int[1000, 1000];
foreach (string line in lines)
{
OperateScalingLights(line, ref lights);
}
int totalBrightness = lights.Cast<int>().Sum();
if (expected != null)
{
Assert.That(totalBrightness, Is.EqualTo(expected.Value));
}
Console.WriteLine($"Part 2: {totalBrightness}");
}
}
}
@@ -0,0 +1,30 @@
--- Day 1: Not Quite Lisp ---
Santa was hoping for a white Christmas, but his weather machine's "snow" function is powered by stars, and he's fresh out! To save Christmas, he needs you to collect fifty stars by December 25th.
Collect stars by helping Santa solve 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!
Here's an easy puzzle to warm you up.
Santa is trying to deliver presents in a large apartment building, but he can't find the right floor - the directions he got are a little confusing. He starts on the ground floor (floor 0) and then follows the instructions one character at a time.
An opening parenthesis, (, means he should go up one floor, and a closing parenthesis, ), means he should go down one floor.
The apartment building is very tall, and the basement is very deep; he will never find the top or bottom floors.
For example:
(()) and ()() both result in floor 0.
((( and (()(()( both result in floor 3.
))((((( also results in floor 3.
()) and ))( both result in floor -1 (the first basement level).
))) and )())()) both result in floor -3.
To what floor do the instructions take Santa?
--- Part Two ---
Now, given the same instructions, find the position of the first character that causes him to enter the basement (floor -1). The first character in the instructions has position 1, the second character has position 2, and so on.
For example:
) causes him to enter the basement at character position 1.
()()) causes him to enter the basement at character position 5.
What is the position of the character that causes Santa to first enter the basement?
@@ -0,0 +1,21 @@
--- Day 2: I Was Told There Would Be No Math ---
The elves are running low on wrapping paper, and so they need to submit an order for more. They have a list of the dimensions (length l, width w, and height h) of each present, and only want to order exactly as much as they need.
Fortunately, every present is a box (a perfect right rectangular prism), which makes calculating the required wrapping paper for each gift a little easier: find the surface area of the box, which is 2*l*w + 2*w*h + 2*h*l. The elves also need a little extra paper for each present: the area of the smallest side.
For example:
A present with dimensions 2x3x4 requires 2*6 + 2*12 + 2*8 = 52 square feet of wrapping paper plus 6 square feet of slack, for a total of 58 square feet.
A present with dimensions 1x1x10 requires 2*1 + 2*10 + 2*10 = 42 square feet of wrapping paper plus 1 square foot of slack, for a total of 43 square feet.
All numbers in the elves' list are in feet. How many total square feet of wrapping paper should they order?
--- Part Two ---
The elves are also running low on ribbon. Ribbon is all the same width, so they only have to worry about the length they need to order, which they would again like to be exact.
The ribbon required to wrap a present is the shortest distance around its sides, or the smallest perimeter of any one face. Each present also requires a bow made out of ribbon as well; the feet of ribbon required for the perfect bow is equal to the cubic feet of volume of the present. Don't ask how they tie the bow, though; they'll never tell.
For example:
A present with dimensions 2x3x4 requires 2+2+3+3 = 10 feet of ribbon to wrap the present plus 2*3*4 = 24 feet of ribbon for the bow, for a total of 34 feet.
A present with dimensions 1x1x10 requires 1+1+1+1 = 4 feet of ribbon to wrap the present plus 1*1*10 = 10 feet of ribbon for the bow, for a total of 14 feet.
How many total feet of ribbon should they order?
@@ -0,0 +1,25 @@
--- Day 3: Perfectly Spherical Houses in a Vacuum ---
Santa is delivering presents to an infinite two-dimensional grid of houses.
He begins by delivering a present to the house at his starting location, and then an elf at the North Pole calls him via radio and tells him where to move next. Moves are always exactly one house to the north (^), south (v), east (>), or west (<). After each move, he delivers another present to the house at his new location.
However, the elf back at the north pole has had a little too much eggnog, and so his directions are a little off, and Santa ends up visiting some houses more than once. How many houses receive at least one present?
For example:
> delivers presents to 2 houses: one at the starting location, and one to the east.
^>v< delivers presents to 4 houses in a square, including twice to the house at his starting/ending location.
^v^v^v^v^v delivers a bunch of presents to some very lucky children at only 2 houses.
--- Part Two ---
The next year, to speed up the process, Santa creates a robot version of himself, Robo-Santa, to deliver presents with him.
Santa and Robo-Santa start at the same location (delivering two presents to the same starting house), then take turns moving based on instructions from the elf, who is eggnoggedly reading from the same script as the previous year.
This year, how many houses receive at least one present?
For example:
^v delivers presents to 3 houses, because Santa goes north, and then Robo-Santa goes south.
^>v< now delivers presents to 3 houses, and Santa and Robo-Santa end up back where they started.
^v^v^v^v^v now delivers presents to 11 houses, with Santa going one direction and Robo-Santa going the other.
@@ -0,0 +1,10 @@
--- Day 4: The Ideal Stocking Stuffer ---
Santa needs help mining some AdventCoins (very similar to bitcoins) to use as gifts for all the economically forward-thinking little girls and boys.
To do this, he needs to find MD5 hashes which, in hexadecimal, start with at least five zeroes. The input to the MD5 hash is some secret key (your puzzle input, given below) followed by a number in decimal. To mine AdventCoins, you must find Santa the lowest positive number (no leading zeroes: 1, 2, 3, ...) that produces such a hash.
For example:
If your secret key is abcdef, the answer is 609043, because the MD5 hash of abcdef609043 starts with five zeroes (000001dbbfa...), and it is the lowest such number to do so.
If your secret key is pqrstuv, the lowest number it combines with to make an MD5 hash starting with five zeroes is 1048970; that is, the MD5 hash of pqrstuv1048970 looks like 000006136ef....
@@ -0,0 +1,31 @@
--- Day 5: Doesn't He Have Intern-Elves For This? ---
Santa needs help figuring out which strings in his text file are naughty or nice.
A nice string is one with all of the following properties:
It contains at least three vowels (aeiou only), like aei, xazegov, or aeiouaeiouaeiou.
It contains at least one letter that appears twice in a row, like xx, abcdde (dd), or aabbccdd (aa, bb, cc, or dd).
It does not contain the strings ab, cd, pq, or xy, even if they are part of one of the other requirements.
For example:
ugknbfddgicrmopn is nice because it has at least three vowels (u...i...o...), a double letter (...dd...), and none of the disallowed substrings.
aaa is nice because it has at least three vowels and a double letter, even though the letters used by different rules overlap.
jchzalrnumimnmhp is naughty because it has no double letter.
haegwjzuvuyypxyu is naughty because it contains the string xy.
dvszwmarrgswjxmb is naughty because it contains only one vowel.
How many strings are nice?
--- Part Two ---
Realizing the error of his ways, Santa has switched to a better model of determining whether a string is naughty or nice. None of the old rules apply, as they are all clearly ridiculous.
Now, a nice string is one with all of the following properties:
It contains a pair of any two letters that appears at least twice in the string without overlapping, like xyxy (xy) or aabcdefgaa (aa), but not like aaa (aa, but it overlaps).
It contains at least one letter which repeats with exactly one letter between them, like xyx, abcdefeghi (efe), or even aaa.
For example:
qjhvhtzxzqqjkmpb is nice because is has a pair that appears twice (qj) and a letter that repeats with exactly one letter between them (zxz).
xxyxx is nice because it has a pair that appears twice and a letter that repeats with one between, even though the letters used by each rule overlap.
uurcxstgmygtbstg is naughty because it has a pair (tg) but no repeat with a single letter between them.
ieodomkazucvgmuy is naughty because it has a repeating letter with one between (odo), but no pair that appears twice.
How many strings are nice under these new rules?
@@ -0,0 +1,33 @@
--- Day 6: Probably a Fire Hazard ---
Because your neighbors keep defeating you in the holiday house decorating contest year after year, you've decided to deploy one million lights in a 1000x1000 grid.
Furthermore, because you've been especially nice this year, Santa has mailed you instructions on how to display the ideal lighting configuration.
Lights in your grid are numbered from 0 to 999 in each direction; the lights at each corner are at 0,0, 0,999, 999,999, and 999,0. The instructions include whether to turn on, turn off, or toggle various inclusive ranges given as coordinate pairs. Each coordinate pair represents opposite corners of a rectangle, inclusive; a coordinate pair like 0,0 through 2,2 therefore refers to 9 lights in a 3x3 square. The lights all start turned off.
To defeat your neighbors this year, all you have to do is set up your lights by doing the instructions Santa sent you in order.
For example:
turn on 0,0 through 999,999 would turn on (or leave on) every light.
toggle 0,0 through 999,0 would toggle the first line of 1000 lights, turning off the ones that were on, and turning on the ones that were off.
turn off 499,499 through 500,500 would turn off (or leave off) the middle four lights.
After following the instructions, how many lights are lit?
--- Part Two ---
You just finish implementing your winning light pattern when you realize you mistranslated Santa's message from Ancient Nordic Elvish.
The light grid you bought actually has individual brightness controls; each light can have a brightness of zero or more. The lights all start at zero.
The phrase turn on actually means that you should increase the brightness of those lights by 1.
The phrase turn off actually means that you should decrease the brightness of those lights by 1, to a minimum of zero.
The phrase toggle actually means that you should increase the brightness of those lights by 2.
What is the total brightness of all lights combined after following Santa's instructions?
For example:
turn on 0,0 through 0,0 would increase the total brightness by 1.
toggle 0,0 through 999,999 would increase the total brightness by 2000000.