AdventOfCode 2023 Day08 Part1, refactor to separate concerns.

This commit is contained in:
2023-12-08 13:38:51 +01:00
parent 94a5fce777
commit 22c8a76c65

View File

@@ -42,32 +42,16 @@ public class Day08 : IDay
{ {
public string ResolvePart1(string[] inputs) public string ResolvePart1(string[] inputs)
{ {
string startNodeKey = "AAA"; const string startNodeKey = "AAA";
string endNodeKey = "ZZZ"; const string endNodeKey = "ZZZ";
string strLeftRightInstructions = inputs[0]; Map map = new(inputs);
Dictionary<string, MapNode> dictNodes = new();
for (int i = 2; i < inputs.Length; i++)
{
MapNode node = new(inputs[i]);
dictNodes.Add(node.Key, node);
}
MapNode currentNode = dictNodes[startNodeKey]; MapWalker walker = new(map, startNodeKey);
int steps = 0; while (walker.CurrentNode.Key != endNodeKey)
while (currentNode.Key != endNodeKey)
{ {
char leftRightInstruction = strLeftRightInstructions[steps % strLeftRightInstructions.Length]; walker.Step();
if (leftRightInstruction == 'L')
{
currentNode = dictNodes[currentNode.LeftKey];
} }
if (leftRightInstruction == 'R') return walker.Steps.ToString();
{
currentNode = dictNodes[currentNode.RightKey];
}
steps++;
}
return steps.ToString();
} }
public string ResolvePart2(string[] inputs) public string ResolvePart2(string[] inputs)
@@ -75,7 +59,7 @@ public class Day08 : IDay
throw new NotImplementedException(); throw new NotImplementedException();
} }
public class MapNode private class MapNode
{ {
public string Key { get; set; } public string Key { get; set; }
public string LeftKey { get; set; } public string LeftKey { get; set; }
@@ -88,4 +72,79 @@ public class Day08 : IDay
RightKey = strMapNode.Substring(12, 3); RightKey = strMapNode.Substring(12, 3);
} }
} }
private class Map
{
private readonly List<MapNode> _mapNodes;
private readonly Dictionary<string, MapNode> _dictMapNodes;
private readonly string _strLeftRightInstructions;
public Map(string[] inputs)
{
_mapNodes = new List<MapNode>();
_dictMapNodes = new Dictionary<string, MapNode>();
_strLeftRightInstructions = inputs[0];
for (int i = 2; i < inputs.Length; i++)
{
MapNode node = new(inputs[i]);
_mapNodes.Add(node);
_dictMapNodes.Add(node.Key, node);
}
}
public List<MapNode> Nodes
{
get { return _mapNodes; }
}
public MapNode? GetByKey(string key)
{
return _dictMapNodes.GetValueOrDefault(key);
}
public char GetInstruction(int step)
{
char leftRightInstruction = _strLeftRightInstructions[step % _strLeftRightInstructions.Length];
return leftRightInstruction;
}
}
private class MapWalker
{
private readonly Map _map;
private MapNode _currentNode;
private int _steps;
public MapWalker(Map map, string startKey)
{
_map = map;
_currentNode = map.GetByKey(startKey) ?? map.Nodes.First();
_steps = 0;
}
public void Step()
{
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++;
}
public MapNode CurrentNode
{
get { return _currentNode; }
}
public int Steps
{
get { return _steps; }
}
}
} }