r/adventofcode • • Dec 23 '15

SOLUTION MEGATHREAD --- Day 23 Solutions ---

This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!


We know we can't control people posting solutions elsewhere and trying to exploit the leaderboard, but this way we can try to reduce the leaderboard gaming from the official subreddit.

Please and thank you, and much appreciated!


--- Day 23: Opening the Turing Lock ---

Post your solution as a comment or link to your repo. Structure your post like previous daily solution threads.

8 Upvotes

155 comments sorted by

View all comments

Show parent comment

2

u/Johnicholas Dec 23 '15

This is my version, also in C:

#include <assert.h>
#include <stdio.h>
#include <openssl/bn.h>

struct opcode {
  int offset, r;
  enum { HLF, TPL, INC, JMP, JIE, JIO } kind;
};

// globals
struct opcode code[100]; // 100 approximates infinity
int code_length;

void parse() {
  int i;
  char line[80];
  for (i = 0; 1; i += 1) {
    assert(i < 100);
    if (fgets(line, sizeof(line), stdin) == 0) {
      break;
    }
    char r;
    int offset;
    if (sscanf(line, "hlf %c", &r) == 1) {
      code[i].kind = HLF;
      code[i].r = r - 'a';
    } else if (sscanf(line, "tpl %c", &r) == 1) {
      code[i].kind = TPL;
      code[i].r = r - 'a';
    } else if (sscanf(line, "inc %c", &r) == 1) {
      code[i].kind = INC;
      code[i].r = r - 'a';
    } else if (sscanf(line, "jmp %d", &offset) == 1) {
      code[i].kind = JMP;
      code[i].offset = offset;
    } else if (sscanf(line, "jie %c, %d", &r, &offset) == 2) {
      code[i].kind = JIE;
      code[i].r = r - 'a';
      code[i].offset = offset;
    } else if (sscanf(line, "jio %c, %d", &r, &offset) == 2) {
      code[i].kind = JIO;
      code[i].r = r - 'a';
      code[i].offset = offset;
    } else {
      printf("parse error!");
      assert(0);
    }
  }
  code_length = i;
}

int BN_is_even(BIGNUM* it) {
  return !BN_is_bit_set(it, 0);
}

void run(int a, int b) {
  BIGNUM* registers[2] = { BN_new(), BN_new() };
  BN_add_word(registers[0], a);
  BN_add_word(registers[1], b);
  int pc = 0;

  while (pc >= 0 && pc < code_length) {
    struct opcode current = code[pc];
    int r = current.r;
    int offset = current.offset;
    switch (current.kind) {
    case HLF:
      BN_div_word(registers[r], 2);
      pc++;
      break;
    case TPL:
      BN_mul_word(registers[r], 3);
      pc++;
      break;
    case INC:
      BN_add_word(registers[r], 1);
      pc++;
      break;
    case JMP:
      pc += offset;
      break;
    case JIE:
      if (BN_is_even(registers[r])) {
        pc += offset;
      } else {
        pc++;
      }
      break;
    case JIO:
      if (BN_is_one(registers[r])) {
        pc += offset;
      } else {
        pc++;
      }
      break;
    }
  }
  printf("a = %s, b = %s\n", BN_bn2dec(registers[0]), BN_bn2dec(registers[1]));
  BN_free(registers[0]);
  BN_free(registers[1]);
}

int main(int argc, char* argv[]) {
  parse();
  printf("first half: ");
  run(0, 0);
  printf("second half: ");
  run(1, 0);
  return 0;
}

Of course, most of the work is done by libcrypto.

1

u/willkill07 Dec 23 '15

Any motivation behind using bignum from OpenSSL instead of an unsigned int pointer?

1

u/Johnicholas Dec 23 '15 edited Dec 24 '15

I thought the spec was hinting that the registers could and should hold very very large numbers; I only found out that essentially all the inputs only needed intermediate values that fit inside unsigned int after coming here.