2023-12-08 21:46:28 -06:00
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
|
|
namespace HauntedWasteland;
|
|
|
|
|
|
|
|
|
|
public class Program
|
|
|
|
|
{
|
|
|
|
|
public static async Task<int> Main(string[] args)
|
|
|
|
|
{
|
|
|
|
|
if (args.Length is 0)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Please provide a path to the input file.");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (File.Exists(args[0]) is false)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("The provided file does not exist.");
|
|
|
|
|
return -2;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-09 00:43:17 -06:00
|
|
|
var isPart2 = args.Length > 1 && args[1] == "part2";
|
2023-12-08 21:46:28 -06:00
|
|
|
var input = await File.ReadAllLinesAsync(args[0]);
|
|
|
|
|
|
|
|
|
|
var stopwatch = new Stopwatch();
|
|
|
|
|
stopwatch.Start();
|
|
|
|
|
|
2023-12-09 00:43:17 -06:00
|
|
|
var map = Map.Parse(input);
|
|
|
|
|
var result = isPart2
|
|
|
|
|
? map.CountStepsToAllZNodes()
|
|
|
|
|
: map.CountStepsToZ();
|
2023-12-08 21:46:28 -06:00
|
|
|
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
|
2023-12-09 00:43:17 -06:00
|
|
|
Console.WriteLine($"The number of steps is {result}. ({stopwatch.ElapsedMilliseconds}ms)");
|
2023-12-08 21:46:28 -06:00
|
|
|
|
2023-12-09 00:43:17 -06:00
|
|
|
return (int)result;
|
2023-12-08 21:46:28 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-09 00:47:02 -06:00
|
|
|
/// <summary>
|
|
|
|
|
/// A map of the Haunted Wasteland.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="turns">The turns to take at each step.</param>
|
|
|
|
|
/// <param name="nodes">The nodes in the map.</param>
|
|
|
|
|
/// <returns>An instance of <see cref="Map"/>.</returns>
|
2023-12-08 21:46:28 -06:00
|
|
|
public class Map(
|
|
|
|
|
List<char> turns,
|
|
|
|
|
List<Node> nodes
|
|
|
|
|
)
|
|
|
|
|
{
|
2023-12-09 00:47:02 -06:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the turns to take at each step.
|
|
|
|
|
/// </summary>
|
2023-12-08 21:46:28 -06:00
|
|
|
public List<char> Turns { get; init; } = turns;
|
2023-12-09 00:47:02 -06:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the nodes in the map.
|
|
|
|
|
/// </summary>
|
2023-12-08 21:46:28 -06:00
|
|
|
public List<Node> Nodes { get; init; } = nodes;
|
|
|
|
|
|
2023-12-09 00:47:02 -06:00
|
|
|
/// <summary>
|
|
|
|
|
/// Parses a map from a string array.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="mapInput">The map input.</param>
|
|
|
|
|
/// <returns>An instance of <see cref="Map"/>.</returns>
|
2023-12-08 21:46:28 -06:00
|
|
|
public static Map Parse(string[] mapInput)
|
|
|
|
|
{
|
|
|
|
|
var turns = mapInput[0].ToList();
|
|
|
|
|
var nodes = mapInput[2..]
|
|
|
|
|
.Select(Node.Parse)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
return new Map(turns, nodes);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-09 00:47:02 -06:00
|
|
|
/// <summary>
|
|
|
|
|
/// Counts the number of steps to the Z node.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The number of steps to the Z node.</returns>
|
2023-12-08 21:46:28 -06:00
|
|
|
public int CountStepsToZ()
|
|
|
|
|
{
|
|
|
|
|
var current = Nodes.First(n => n.Current == "AAA");
|
|
|
|
|
var steps = 0;
|
|
|
|
|
|
|
|
|
|
while (current.Current != "ZZZ")
|
|
|
|
|
{
|
|
|
|
|
var next = Turns[steps % Turns.Count] switch
|
|
|
|
|
{
|
|
|
|
|
'R' => current.Right,
|
|
|
|
|
'L' => current.Left,
|
|
|
|
|
_ => throw new Exception("Invalid turn")
|
|
|
|
|
};
|
|
|
|
|
current = Nodes.First(n => n.Current == next);
|
|
|
|
|
steps++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return steps;
|
|
|
|
|
}
|
2023-12-09 00:43:17 -06:00
|
|
|
|
2023-12-09 00:47:02 -06:00
|
|
|
/// <summary>
|
|
|
|
|
/// Finds the prime factors of a number.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="number">The number to find the prime factors of.</param>
|
|
|
|
|
/// <returns>The prime factors of the number.</returns>
|
2023-12-09 00:43:17 -06:00
|
|
|
public List<long> FindPrimeFactors(long number)
|
|
|
|
|
{
|
|
|
|
|
var factors = new List<long>();
|
|
|
|
|
|
|
|
|
|
// Start with the smallest prime number, 2.
|
|
|
|
|
var divisor = 2;
|
|
|
|
|
|
|
|
|
|
// Continue until the number is reduced to 2 or less.
|
|
|
|
|
while (number >= 2)
|
|
|
|
|
{
|
|
|
|
|
// If the number is divisible by the current divisor,
|
|
|
|
|
if (number % divisor == 0)
|
|
|
|
|
{
|
|
|
|
|
// Add the divisor to the list of factors.
|
|
|
|
|
factors.Add(divisor);
|
|
|
|
|
// Divide the number by the divisor to reduce it.
|
|
|
|
|
number /= divisor;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// If the number is not divisible by the current divisor, increment the divisor.
|
|
|
|
|
divisor++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return factors;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-09 00:47:02 -06:00
|
|
|
/// <summary>
|
|
|
|
|
/// Finds the least common multiple of a list of numbers.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="numbers">The numbers to find the least common multiple of.</param>
|
|
|
|
|
/// <returns>The least common multiple of the numbers.</returns>
|
2023-12-09 00:43:17 -06:00
|
|
|
public long FindLeastCommonMultiple(List<long> numbers)
|
|
|
|
|
{
|
|
|
|
|
var primeFactors = numbers.Select(FindPrimeFactors).ToList();
|
|
|
|
|
|
|
|
|
|
var uniquePrimeFactors = primeFactors
|
|
|
|
|
.SelectMany(pf => pf)
|
|
|
|
|
.Distinct()
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var maxPrimeFactors = uniquePrimeFactors
|
|
|
|
|
.Select(upf => primeFactors.Max(pf => pf.Count(f => f == upf)))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var result = uniquePrimeFactors
|
|
|
|
|
.Zip(maxPrimeFactors)
|
|
|
|
|
.Aggregate(
|
|
|
|
|
(long)1,
|
|
|
|
|
(acc, b) =>
|
|
|
|
|
// b.First is the prime factor
|
|
|
|
|
// b.Second is the number of times it occurs
|
|
|
|
|
acc * (long)Math.Pow(b.First, b.Second)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-09 00:47:02 -06:00
|
|
|
/// <summary>
|
|
|
|
|
/// Counts the number of steps to all Z nodes.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The number of steps to all Z nodes.</returns>
|
2023-12-09 00:43:17 -06:00
|
|
|
public long CountStepsToAllZNodes()
|
|
|
|
|
{
|
|
|
|
|
var startNodes = Nodes.Where(n => n.Current.EndsWith('A')).ToList();
|
|
|
|
|
var nodeSteps = new List<long>();
|
|
|
|
|
|
|
|
|
|
foreach (var startNode in startNodes)
|
|
|
|
|
{
|
|
|
|
|
var current = startNode;
|
|
|
|
|
var steps = 0;
|
|
|
|
|
|
|
|
|
|
while (current.Current.EndsWith('Z') is false)
|
|
|
|
|
{
|
|
|
|
|
var next = Turns[steps % Turns.Count] switch
|
|
|
|
|
{
|
|
|
|
|
'R' => current.Right,
|
|
|
|
|
'L' => current.Left,
|
|
|
|
|
_ => throw new Exception("Invalid turn")
|
|
|
|
|
};
|
|
|
|
|
current = Nodes.First(n => n.Current == next);
|
|
|
|
|
steps++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nodeSteps.Add(steps);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return FindLeastCommonMultiple(nodeSteps);
|
|
|
|
|
}
|
2023-12-08 21:46:28 -06:00
|
|
|
}
|
|
|
|
|
|
2023-12-09 00:47:02 -06:00
|
|
|
/// <summary>
|
|
|
|
|
/// A node in the map.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="current">The current node.</param>
|
|
|
|
|
/// <param name="left">The left node.</param>
|
|
|
|
|
/// <param name="right">The right node.</param>
|
|
|
|
|
/// <returns>An instance of <see cref="Node"/>.</returns>
|
2023-12-08 21:46:28 -06:00
|
|
|
public class Node(
|
|
|
|
|
string current,
|
|
|
|
|
string left,
|
|
|
|
|
string right
|
|
|
|
|
)
|
|
|
|
|
{
|
2023-12-09 00:47:02 -06:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the current node.
|
|
|
|
|
/// </summary>
|
2023-12-08 21:46:28 -06:00
|
|
|
public string Current { get; init; } = current;
|
2023-12-09 00:47:02 -06:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the left node.
|
|
|
|
|
/// </summary>
|
2023-12-08 21:46:28 -06:00
|
|
|
public string Left { get; init; } = left;
|
2023-12-09 00:47:02 -06:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the right node.
|
|
|
|
|
/// </summary>
|
2023-12-08 21:46:28 -06:00
|
|
|
public string Right { get; init; } = right;
|
|
|
|
|
|
2023-12-09 00:47:02 -06:00
|
|
|
/// <summary>
|
|
|
|
|
/// Casts the node to a string.
|
|
|
|
|
/// </summary>
|
2023-12-08 21:46:28 -06:00
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return $"{Current} = ({Left},{Right})";
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-09 00:47:02 -06:00
|
|
|
/// <summary>
|
|
|
|
|
/// Parses a node from a string.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="nodeString">The node string.</param>
|
|
|
|
|
/// <returns>An instance of <see cref="Node"/>.</returns>
|
2023-12-08 21:46:28 -06:00
|
|
|
public static Node Parse(string nodeString)
|
|
|
|
|
{
|
|
|
|
|
var parts = nodeString.Split(
|
|
|
|
|
'=',
|
|
|
|
|
StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries
|
|
|
|
|
);
|
|
|
|
|
var current = parts[0];
|
|
|
|
|
var nextNodes = parts[1].Split(
|
|
|
|
|
',',
|
|
|
|
|
StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries
|
|
|
|
|
);
|
|
|
|
|
var left = nextNodes[0].Trim('(');
|
|
|
|
|
var right = nextNodes[1].Trim(')');
|
|
|
|
|
|
|
|
|
|
return new Node(current, left, right);
|
|
|
|
|
}
|
|
|
|
|
}
|