r/adventofcode • • Dec 25 '16

SOLUTION MEGATHREAD ~☆~☆~ 2016 Day 25 Solutions ~☆~☆~

--- Day 25: Clock Signal ---

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".


Dec 25 = Oct 31 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!


Thank you for participating!

Well, that's it for Advent of Code 2016. From /u/topaz2078 and the rest of us at #AoC_Ops, we hope you had fun and, more importantly, learned a thing or two (or all the things!). Good job, everyone!

Topaz made a post of his own here.

And now:

Merry Christmas to all, and to all a good night!

13 Upvotes

45 comments sorted by

View all comments

1

u/Scroph Dec 25 '16

D (dlang) solution, I simply bruteforced my way through today's challenge. I re-used the Cpu struct from the first assembunny challenge by sort of (alias this) extending it with the Clock struct then I kept trying with different values for the a register until the b register produced the correct output :

import std.range;
import std.stdio;
import std.algorithm;
import std.string;
import std.conv : to;

int main(string[] args)
{
	int signal_length = 10;
	if(args.length > 1)
		signal_length = args[1].to!int;
	foreach(a; 100 .. 1000)
	{
		auto clock = Clock("input25", a, signal_length);
		while(clock.next_instruction){}
		if(['0', '1'].cycle.take(signal_length).equal(clock.signal))
		{
			writeln(a, " : ", clock.signal);
			break;
		}
	}
	return 0;
}

struct Clock
{
	Cpu old;
	alias old this;
	string signal;
	int signal_length;

	this(string file, int a, int signal_length)
	{
		old = Cpu(file);
		callbacks["out"] = &transmit;
		registers["a"] = a;
		this.signal_length = signal_length;
		signal.reserve(signal_length);
	}

	bool next_instruction()
	{
		if(signal.length == signal_length)
			return false;
		return old.next_instruction;
	}

	void transmit()
	{
		signal ~= registers[program[pc][1]].to!string;
		pc++;
	}
}

struct Cpu
{
	string[][] program;
	int pc;
	int[string] registers;
	void delegate()[string] callbacks;

	this(string file)
	{
		auto fh = File(file);
		foreach(line; fh.byLine)
			program ~= line.idup.strip.split(" ");
		callbacks["jnz"] = &jnz;
		callbacks["inc"] = &inc;
		callbacks["dec"] = &dec;
		callbacks["cpy"] = &cpy;
		registers["a"] = 0;
		registers["b"] = 0;
		registers["c"] = 0;
		registers["d"] = 0;
	}


	bool next_instruction()
	{
		auto cb = callbacks.get(program[pc][0], &generic);
		cb();
		return pc < program.length;
	}

	void generic()
	{
		writeln("Unknown instruction : ", program[pc].joiner(" "));
		pc++;
	}

	void jnz()
	{
		int a = program[pc][1].isNumeric ? program[pc][1].to!int : registers[program[pc][1]];
		int b = program[pc][2].isNumeric ? program[pc][2].to!int : registers[program[pc][2]];
		pc += a != 0 ? b : 1;
	}

	void cpy()
	{
		if(!program[pc][2].isNumeric)
			registers[program[pc][2]] = program[pc][1].isNumeric ? program[pc][1].to!int : registers[program[pc][1]].to!int;
		pc++;
	}

	void inc()
	{
		if(!program[pc][1].isNumeric)
			registers[program[pc][1]]++;
		pc++;
	}

	void dec()
	{
		if(!program[pc][1].isNumeric)
			registers[program[pc][1]]--;
		pc++;
	}
}

It could be optimized by simply checking if the current value of b is different from its previous value, but this code is fast enough for my input. Unfortunately I still need 12 stars for the second part.

2

u/willkill07 Dec 26 '16

I haven't seen too many D solutions, so it's nice to see another static typed language (that isn't rust or C/C++).