Program.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System.Diagnostics;
  2. using Day15;
  3. if (args.Any(a => a == "--debug"))
  4. {
  5. Console.WriteLine("DEBUG");
  6. Console.WriteLine(Debugger.Launch());
  7. }
  8. if (args.Length < 1)
  9. {
  10. Console.WriteLine("Requires 1 args: inputFileName");
  11. return -1;
  12. }
  13. var inputFile = args[0];
  14. var parser = new Parser();
  15. var steps = parser.Parse(inputFile).ToList();
  16. var sum = 0;
  17. foreach (var s in steps)
  18. {
  19. var hash = s.GetHash();
  20. sum += hash;
  21. }
  22. Console.WriteLine();
  23. Console.WriteLine($"Total: {sum}");
  24. var boxes = Enumerable.Range(0, 256).Select(_ => new List<Lens>()).ToList();
  25. foreach (var s in steps)
  26. {
  27. s.Execute(boxes);
  28. foreach (var pair in boxes.Select((box, index) => (Box: box, Index: index)))
  29. {
  30. if (pair.Box.Count > 0)
  31. {
  32. var contents = string.Join(" ", pair.Box.Select(lens => lens.Visual()));
  33. Console.WriteLine($"Box {pair.Index}: {contents}");
  34. }
  35. }
  36. Console.WriteLine("-----");
  37. }
  38. var power = boxes
  39. .SelectMany((box, boxIndex) => box.Select((lens, lensIndex) => (boxIndex + 1) * (lensIndex + 1) * lens.FocalLength))
  40. .Sum();
  41. Console.WriteLine();
  42. Console.WriteLine($"Power: {power}");
  43. return 0;