-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from tiberiushunter/day8-2023
Add Day 8 2023 Solution
- Loading branch information
Showing
9 changed files
with
196 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
namespace AdventOfCode.Solutions.Extensions; | ||
public static class EnumerableExtensions | ||
{ | ||
public static IEnumerable<long> LongRange(long start, long length) | ||
{ | ||
var limit = start + length; | ||
|
||
while (start < limit) | ||
{ | ||
yield return start; | ||
start++; | ||
} | ||
} | ||
|
||
public static IEnumerable<T> InfiniteSelect<T>(this IEnumerable<T> source) | ||
{ | ||
while (true) | ||
{ | ||
foreach (T item in source) | ||
{ | ||
yield return item; | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using AdventOfCode.Domain.Interfaces; | ||
using AdventOfCode.Solutions.Extensions; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace AdventOfCode.Solutions._2023; | ||
|
||
public class Day8 : IDay | ||
{ | ||
public string Title => "Haunted Wasteland"; | ||
|
||
public string PartA(string input) | ||
{ | ||
var parsedInput = input.Split("\n\n"); | ||
|
||
var instructions = parsedInput[0].ToCharArray(); | ||
|
||
var network = parsedInput[1] | ||
.Split("\n") | ||
.Select(line => Regex.Match(line, @"([A-Z]+) = \(([A-Z]+), ([A-Z]+)\)", RegexOptions.Compiled)) | ||
.ToDictionary(match => match.Groups[1].Value, match => (match.Groups[2].Value, match.Groups[3].Value)); | ||
|
||
var currentNode = "AAA"; | ||
int counter = 0; | ||
|
||
foreach (var instruction in instructions.InfiniteSelect()) | ||
{ | ||
counter++; | ||
switch (instruction) | ||
{ | ||
case 'L': | ||
currentNode = network[currentNode].Item1; | ||
break; | ||
case 'R': | ||
currentNode = network[currentNode].Item2; | ||
break; | ||
} | ||
|
||
if (currentNode == "ZZZ") | ||
{ | ||
break; | ||
} | ||
} | ||
|
||
return counter.ToString(); | ||
} | ||
|
||
public string PartB(string input) | ||
{ | ||
var parsedInput = input.Split("\n\n"); | ||
|
||
var instructions = parsedInput[0].ToCharArray(); | ||
|
||
var network = parsedInput[1] | ||
.Split("\n") | ||
.Select(line => Regex.Match(line, @"([A-Z]+) = \(([A-Z]+), ([A-Z]+)\)", RegexOptions.Compiled)) | ||
.ToDictionary(match => match.Groups[1].Value, match => (match.Groups[2].Value, match.Groups[3].Value)); | ||
|
||
var currentNodes = network.Where(a => a.Key.EndsWith("A")).Select(a => a.Key).ToList(); | ||
var minimumStepsForEachPath = new long[currentNodes.Count]; | ||
|
||
int counter = 0; | ||
|
||
foreach (var instruction in instructions.InfiniteSelect()) | ||
{ | ||
counter++; | ||
|
||
switch (instruction) | ||
{ | ||
case 'L': | ||
for (int i = 0; i < currentNodes.Count; i++) | ||
{ | ||
currentNodes[i] = network[currentNodes[i]].Item1; | ||
} | ||
break; | ||
case 'R': | ||
for (int i = 0; i < currentNodes.Count; i++) | ||
{ | ||
currentNodes[i] = network[currentNodes[i]].Item2; | ||
} | ||
break; | ||
} | ||
|
||
for (int i = 0; i < currentNodes.Count; i++) | ||
{ | ||
if (currentNodes[i].EndsWith('Z') && minimumStepsForEachPath[i] == 0) | ||
{ | ||
minimumStepsForEachPath[i] = counter; | ||
} | ||
} | ||
|
||
if (minimumStepsForEachPath.All(a => a > 0)) | ||
{ | ||
break; | ||
} | ||
} | ||
|
||
return minimumStepsForEachPath.Aggregate((a, b) => a * b / GreatestCommonDivisor(a, b)).ToString(); | ||
} | ||
|
||
private static long GreatestCommonDivisor(long x, long y) | ||
{ | ||
if (y == 0) { return x; } | ||
|
||
return GreatestCommonDivisor(y, x % y); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
namespace AdventOfCode.Solutions.Tests._2023; | ||
|
||
public class Day8Tests : DayTests | ||
{ | ||
[Test] | ||
public async Task PartA() | ||
{ | ||
// Arrange | ||
string expected = "18727"; | ||
|
||
// Act | ||
var solution = await _solverService.SolveDay(2023, 8); | ||
|
||
// Assert | ||
solution.PartA.Solution.Should().Be(expected); | ||
} | ||
|
||
[Test] | ||
public async Task PartB() | ||
{ | ||
// Arrange | ||
string expected = "18024643846273"; | ||
|
||
// Act | ||
var solution = await _solverService.SolveDay(2023, 8); | ||
|
||
// Assert | ||
solution.PartB.Solution.Should().Be(expected); | ||
} | ||
} |