DEV Community

Discussion on: AoC Day 21: Chronal Conversion

Collapse
 
jmgimeno profile image
Juan Manuel Gimeno

First I have translated the input into "assembler" using the disassembler I did for day 19, and then I reassembled the solution into C++.

In the code, the program exits when register 5 is equal to register 0.

  • For part1 the value of register 5 the first time the condition is evaluated, is the solution.
  • For part2 you have to check for a cycle in the values of register 5 at the condiction. The last one before the cycle is the solution.

So I've added the code to check those two conditions.

#include <iostream>
#include <set>

using namespace std;

int main() {
  set<int> seen;
  int last_r5 = 0;
  int r0, r1, r3, r4, r5;

  l6: 
  r1 = r5 | 65536;
  r5 = 10678677;

  l8:
  r4 = r1 & 255;
  r5 += r4;
  r5 &= 16777215;
  r5 *= 65899;
  r5 &= 16777215;
  if (256 > r1) goto l28;
  r4 = 0;

  l18:
  r3 = r4 + 1;
  r3 *= 256;
  if (r3 > r1) goto l26;
  r4 += 1;
  goto l18;

  l26:
  r1 = r4;
  goto l8;

  l28:
  if (last_r5 == 0) {
    cout << "Part1: " << r5 << endl;
  }
  if (seen.find(r5) == seen.end()) {
    seen.insert(r5);
    last_r5 = r5;
  } else {
    cout << "Part2: " << last_r5 << endl;
    return 0;
  }
  goto l6;
}