mgla

Advent of Code - 2025 - Day 6

Dec 8th, 2025 (edited)
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.69 KB | None | 0 0
  1. var input = await File.ReadAllLinesAsync("input.txt");
  2.  
  3. var parsedInput = input
  4.     .Select(line => line.Trim().Split(' ', StringSplitOptions.RemoveEmptyEntries))
  5.     .ToArray();
  6. var columns = new List<Column>();
  7.  
  8. for (var i = 0; i < parsedInput[0].Length; i++)
  9. {
  10.     var values = parsedInput[..^1].Select((line, idx) => line[i]).ToArray();
  11.     var operation = parsedInput[^1][i][0];
  12.     columns.Add(new Column(values, operation));
  13. }
  14.  
  15. Console.WriteLine($"Part 1: {columns.Sum(col => col.Compute())}");
  16.  
  17. columns.Clear();
  18. var lineLength = input[0].Length;
  19. var caret = 0;
  20. var lineCount = input.Length - 1;
  21.  
  22. while (caret < lineLength)
  23. {
  24.     var values = new string[lineCount];
  25.  
  26.     var segmentLength = 0;
  27.  
  28.     var endReached = false;
  29.     var isSpace = false;
  30.  
  31.     //Calculate segment length from bottom row
  32.     while (!endReached)
  33.     {
  34.         if (caret + segmentLength >= lineLength)
  35.         {
  36.             // End of line reached - add one more to align with previous segments
  37.             segmentLength++;
  38.             break;
  39.         }
  40.  
  41.         var currentChar = input[lineCount][caret + segmentLength];
  42.  
  43.         if (isSpace && currentChar != ' ')
  44.         {
  45.             endReached = true;
  46.             continue;
  47.         }
  48.  
  49.         segmentLength++;
  50.         isSpace = currentChar == ' ';
  51.     }
  52.  
  53.     // Extract segments from each line
  54.     for (var line = 0; line < lineCount; line++)
  55.     {
  56.         values[line] = input[line].Substring(caret, segmentLength - 1);
  57.     }
  58.  
  59.     var operation = input[lineCount][caret];
  60.     columns.Add(new Column(values, operation));
  61.     caret += segmentLength;
  62. }
  63.  
  64. Console.WriteLine($"Part 2: {columns.Sum(col => col.ComputeRtl())}");
  65.  
  66. record struct Column(string[] Values, char Operation)
  67. {
  68.     public readonly long Compute() => Operation switch
  69.     {
  70.         '+' => Values.Sum(long.Parse),
  71.         '*' => Values.Aggregate(1L, (a, b) => a * long.Parse(b)),
  72.         _ => throw new InvalidOperationException($"Unknown operation: {Operation}")
  73.     };
  74.  
  75.     public readonly long ComputeRtl()
  76.     {
  77.         var parsedValues = new string[Values[0].Length];
  78.  
  79.         for (var col = 0; col < Values[0].Length; col++)
  80.         {
  81.             var sb = new System.Text.StringBuilder();
  82.             for (var row = 0; row < Values.Length; row++)
  83.             {
  84.                 sb.Append(Values[row][col]);
  85.             }
  86.             parsedValues[col] = sb.ToString().Trim();
  87.         }
  88.  
  89.         return Operation switch
  90.         {
  91.             '+' => parsedValues.Sum(long.Parse),
  92.             '*' => parsedValues.Aggregate(1L, (a, b) => a * long.Parse(b)),
  93.             _ => throw new InvalidOperationException($"Unknown operation: {Operation}")
  94.         };
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment