AdventOfCode 2023 Day08 Part2, solution using LCM (Specific for this problem input)
This commit is contained in:
@@ -82,10 +82,7 @@ public class Day08 : IDay
|
|||||||
Map map = new(inputs);
|
Map map = new(inputs);
|
||||||
|
|
||||||
MapWalker walker = new(map, startNodeKey);
|
MapWalker walker = new(map, startNodeKey);
|
||||||
while (walker.CurrentNode.Key != endNodeKey)
|
walker.StepWhile(w => w.CurrentNode.Key != endNodeKey);
|
||||||
{
|
|
||||||
walker.Step();
|
|
||||||
}
|
|
||||||
return walker.Steps.ToString();
|
return walker.Steps.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,16 +90,19 @@ public class Day08 : IDay
|
|||||||
{
|
{
|
||||||
Map map = new(inputs);
|
Map map = new(inputs);
|
||||||
List<MapNode> startNodes = map.Nodes.Where(n => n.Key.EndsWith("A")).ToList();
|
List<MapNode> startNodes = map.Nodes.Where(n => n.Key.EndsWith("A")).ToList();
|
||||||
|
|
||||||
List<MapWalker> walkers = startNodes.Select(n => new MapWalker(map, n.Key)).ToList();
|
List<MapWalker> walkers = startNodes.Select(n => new MapWalker(map, n.Key)).ToList();
|
||||||
while (walkers.All(w => w.CurrentNode.Key.EndsWith("Z")) == false)
|
foreach (MapWalker walker in walkers)
|
||||||
{
|
{
|
||||||
foreach (MapWalker walker in walkers)
|
walker.StepWhile(w => w.CurrentNode.Key.EndsWith("Z") == false);
|
||||||
{
|
|
||||||
walker.Step();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return walkers.First().Steps.ToString();
|
|
||||||
|
long steps = 1;
|
||||||
|
foreach (MapWalker walker in walkers)
|
||||||
|
{
|
||||||
|
steps = LeastCommonMultiple(steps, walker.Steps);
|
||||||
|
}
|
||||||
|
|
||||||
|
return steps.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private class MapNode
|
private class MapNode
|
||||||
@@ -169,18 +169,21 @@ public class Day08 : IDay
|
|||||||
_steps = 0;
|
_steps = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Step()
|
public void StepWhile(Func<MapWalker, bool> condition)
|
||||||
{
|
{
|
||||||
char leftRightInstruction = _map.GetInstruction(_steps);
|
while (condition(this))
|
||||||
if (leftRightInstruction == 'L')
|
|
||||||
{
|
{
|
||||||
_currentNode = _map.GetByKey(_currentNode.LeftKey) ?? _map.Nodes.First();
|
char leftRightInstruction = _map.GetInstruction(_steps);
|
||||||
|
if (leftRightInstruction == 'L')
|
||||||
|
{
|
||||||
|
_currentNode = _map.GetByKey(_currentNode.LeftKey) ?? _map.Nodes.First();
|
||||||
|
}
|
||||||
|
if (leftRightInstruction == 'R')
|
||||||
|
{
|
||||||
|
_currentNode = _map.GetByKey(_currentNode.RightKey) ?? _map.Nodes.First();
|
||||||
|
}
|
||||||
|
_steps++;
|
||||||
}
|
}
|
||||||
if (leftRightInstruction == 'R')
|
|
||||||
{
|
|
||||||
_currentNode = _map.GetByKey(_currentNode.RightKey) ?? _map.Nodes.First();
|
|
||||||
}
|
|
||||||
_steps++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public MapNode CurrentNode
|
public MapNode CurrentNode
|
||||||
@@ -193,4 +196,22 @@ public class Day08 : IDay
|
|||||||
get { return _steps; }
|
get { return _steps; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations
|
||||||
|
private static long GreatestCommonDivisor(long a, long b)
|
||||||
|
{
|
||||||
|
while (b != 0)
|
||||||
|
{
|
||||||
|
long temp = b;
|
||||||
|
b = a % b;
|
||||||
|
a = temp;
|
||||||
|
}
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://en.wikipedia.org/wiki/Least_common_multiple#Calculation
|
||||||
|
private static long LeastCommonMultiple(long a, long b)
|
||||||
|
{
|
||||||
|
return (a / GreatestCommonDivisor(a, b)) * b;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user