Advertisement
mgla

Advent of Code - 2024 - Day 17

Dec 17th, 2024 (edited)
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.29 KB | None | 0 0
  1. var input = await File.ReadAllLinesAsync("input.txt");
  2.  
  3. var regA = long.Parse(input[0][12..]);
  4. var regB = long.Parse(input[1][12..]);
  5. var regC = long.Parse(input[2][12..]);
  6.  
  7. var program = input[4][9..].Split(',').Select(int.Parse).ToArray();
  8.  
  9. Console.WriteLine($"Part 1: {string.Join(',', ParseInstructions())}");
  10.  
  11. // The input ends with 3,0 - so every time we reach the end of the program, we divide the value of RegA by 8 (2^3) and start over from 0.
  12. // So the number of outputs depends solely on the value of RegA: n = RegA / 8
  13. // The program consists of 16 numbers, so in order to generate an output that is identical to the input, the value of RegA must be between 8^15 and 8^16.
  14. // So, a value between 0 and 7 will result in a single output. A value between 8 and 63 will result in two outputs, and so on.
  15. // In order to avoid doing a huge loop, we will try to backtrack the value of RegA by starting from the last output and going back to the first one.
  16. // We will first find the value for RegA that will result in the last output of the program - it will be between 0 and 7 (3 bits).
  17. // We will then try to find the value for RegA that results in the second-to-last and last outputs, and so on.
  18. // For every next value, we shift the value for RegA by 3 bits to the left and try all possible values for the next 3 bits - again, between 0 and 7.
  19. // That way, we will only do at most 8*16 iterations, which is a much more reasonable number than 8^16 - 8^15.
  20.  
  21. var variants = new PriorityQueue<int, long>();
  22. variants.Enqueue(program.Length - 1, 0L);
  23.  
  24. while (variants.TryDequeue(out var offset, out var a))
  25. {
  26.     for (var i = 0; i < 8; i++)
  27.     {
  28.         var nextA = (a << 3) + i;
  29.         regA = nextA;
  30.         var output = ParseInstructions();
  31.  
  32.         if (output.SequenceEqual(program[offset..]))
  33.         {
  34.             if (offset == 0)
  35.             {
  36.                 Console.WriteLine($"Part 2: {nextA}");
  37.                 return;
  38.             }
  39.             variants.Enqueue(offset - 1, nextA);
  40.         }
  41.     }
  42. }
  43.  
  44. return;
  45.  
  46. List<int> ParseInstructions()
  47. {
  48.     var index = 0;
  49.     var result = new List<int>();
  50.  
  51.     while (index < program.Length - 1)
  52.     {
  53.         var instruction = program[index];
  54.         var opcode = program[index + 1];
  55.         var nextIndex = index + 2;
  56.  
  57.         var combo = opcode switch
  58.         {
  59.             4 => regA,
  60.             5 => regB,
  61.             6 => regC,
  62.             _ => opcode
  63.         };
  64.  
  65.         switch (instruction)
  66.         {
  67.             case 0: //adv
  68.                 regA /= (long)Math.Pow(2, combo);
  69.                 break;
  70.             case 1: //bxl
  71.                 regB ^= opcode;
  72.                 break;
  73.             case 2: //bst
  74.                 regB = combo % 8;
  75.                 break;
  76.             case 3 when regA != 0: //jnz
  77.                 nextIndex = opcode;
  78.                 break;
  79.             case 4: //bxc
  80.                 regB ^= regC;
  81.                 break;
  82.             case 5: //out
  83.                 result.Add((int)(combo % 8));
  84.                 break;
  85.             case 6: //bdv
  86.                 regB = regA / (long)Math.Pow(2, combo);
  87.                 break;
  88.             case 7: //cdv
  89.                 regC = regA / (long)Math.Pow(2, combo);
  90.                 break;
  91.         }
  92.  
  93.         index = nextIndex;
  94.     }
  95.  
  96.     return result;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement