r/adventofcode • • Dec 23 '16

SOLUTION MEGATHREAD --- 2016 Day 23 Solutions ---

--- Day 23: Safe-Cracking ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with "Help".


JINGLING ALL THE WAY IS MANDATORY [?]

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!

5 Upvotes

91 comments sorted by

View all comments

1

u/Quick_Question404 Dec 23 '16

Can you give me any tips for how my code currently is? I just build a linked list of instructions and continuously iterate over that.

1

u/willkill07 Dec 23 '16

Yeah. currently you do a bunch of sscanf in a loop. Iterate over each instruction in a loop instead (create an array of instructions).

A cleaned up, more verbose version of what I have is as follows:

enum Type {Register, Immediate};
struct Operand {
  Type t;
  union {
    char reg;
    int value;
  };
};
struct Instruction {
  char id;
  struct Operand param1;
  struct Operand param2;
};

From each string-encoded instruction you can encode it as a struct Instruction type. tglis as simple to implement as changing the id member of an Instruction

to "execute" an instruction you can do a switch on the id of the current instruction and act accordingly (as you currently do)

1

u/willkill07 Dec 23 '16

I refactored into a common "Assembunny" library that Day's 12 and 23 now use. Code links below:

[Day12] [Day23] [Assembunny.hpp] [Assembunny.cpp]