2024-12-05 14:18:29 -06:00
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
2024-12-05 23:10:08 -06:00
|
|
|
using PrintQueue;
|
|
|
|
|
|
2024-12-05 14:18:29 -06:00
|
|
|
if (args.Length is 0)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Please provide a path to the input file.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (File.Exists(args[0]) is false)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("The provided file does not exist.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var isPart2 = args.Length is 2 && args[1] is "part2";
|
2024-12-05 23:10:08 -06:00
|
|
|
var input = await File.ReadAllTextAsync(args[0]);
|
2024-12-05 14:18:29 -06:00
|
|
|
|
|
|
|
|
var stopwatch = new Stopwatch();
|
|
|
|
|
stopwatch.Start();
|
|
|
|
|
|
2024-12-05 23:10:08 -06:00
|
|
|
var parser = new PuzzleParser();
|
2024-12-05 14:18:29 -06:00
|
|
|
|
2024-12-05 23:10:08 -06:00
|
|
|
var parseResult = parser.Parse(input);
|
|
|
|
|
|
|
|
|
|
var validator = new UpdateValidator(parseResult.Rules);
|
|
|
|
|
|
2024-12-05 23:47:44 -06:00
|
|
|
var result = isPart2
|
|
|
|
|
? parseResult.Updates
|
|
|
|
|
.Where(u => validator.Validate(u) is false)
|
|
|
|
|
.Select(u => validator.Sort(u))
|
|
|
|
|
.Select(u => u.GetMiddlePage())
|
|
|
|
|
.Sum()
|
|
|
|
|
: parseResult.Updates
|
|
|
|
|
.Where(u => validator.Validate(u))
|
|
|
|
|
.Select(u => u.GetMiddlePage())
|
|
|
|
|
.Sum();
|
2024-12-05 23:10:08 -06:00
|
|
|
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
Console.WriteLine($"The sum of the page numbers is {result}. ({stopwatch.ElapsedMilliseconds}ms)");
|