r/adventofcode • • Dec 18 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 18 Solutions -๐ŸŽ„-

--- Day 18: Duet ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or 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.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hints^ยค?

Spoiler


[Update @ 00:04] First silver

  • Welcome to the final week of Advent of Code 2017. The puzzles are only going to get more challenging from here on out. Adventspeed, sirs and madames!

[Update @ 00:10] First gold, 44 silver

  • We just had to rescue /u/topaz2078 with an industrial-strength paper bag to blow into. I'm real glad I bought all that stock in PBCO (Paper Bag Company) two years ago >_>

[Update @ 00:12] Still 1 gold, silver cap

[Update @ 00:31] 53 gold, silver cap

  • *mind blown*
  • During their famous kicklines, the Rockettes are not actually holding each others' backs like I thought they were all this time.
  • They're actually hoverhanding each other.
  • In retrospect, it makes sense, they'd overbalance themselves and each other if they did, but still...
  • *mind blown so hard*

[Update @ 00:41] Leaderboard cap!

  • I think I enjoyed the duplicating Santas entirely too much...
  • It may also be the wine.
  • Either way, good night (for us), see you all same time tomorrow, yes?

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!

11 Upvotes

227 comments sorted by

View all comments

2

u/Scroph Dec 18 '17 edited Dec 18 '17

Dlang (D programming language) solution. Part 2 runs in ~211ms on my Atom N455 :

import std.stdio;
import std.range;
import std.conv;
import std.string;

void main()
{
	string[][] program;

	foreach(instruction; stdin.byLineCopy)
		program ~= instruction.split;

	auto p0 = CPU(program, 0);
	auto p1 = CPU(program, 1);
	p0.attach(&p1);
	p1.attach(&p0);

	while(true)
	{
		bool proceed = p0.next_cycle;
		bool proceed2 = p1.next_cycle;
		if(!proceed && !proceed2)
			break;

		if(p0.is_pending && p1.is_pending)
			break;
	}

	//p0.send_count.writeln;
	p1.send_count.writeln;
}

struct CPU
{
	string[][] program;
	State state;
	int send_count;
	private
	{
		long[] pending;
		CPU* other;
		long[string] registers;
		int pc;
		immutable void delegate(string[])[string] callbacks;
	}

	this(string[][] program, long name)
	{
		this.registers["p"] = name;
		this.state = State.RUNNING;
		this.program = program;
		this.callbacks = [
			"snd": &snd,
			"set": &set,
			"add": &add,
			"mul": &mul,
			"mod": &mod,
			"rcv": &rcv,
			"jgz": &jgz,
		];
	}

	void attach(CPU* other)
	{
		this.other = other;
	}

	void queue(long message)
	{
		pending ~= message;
	}

	//helper
	bool is_pending() const
	{
		return state == State.PENDING;
	}

	//helper
	bool is_finished() const
	{
		return state == State.FINISHED;
	}

	//returns false if the program finished
	bool next_cycle()
	{
		if(pc >= program.length)
		{
			state = State.FINISHED;
			return false;
		}
		
		auto instruction = program[pc];
		if(state == State.PENDING && pending.length == 0)
			return true;
		callbacks[instruction[0]](instruction);
		return true;
	}

	private:
	long get_value(string value)
	{
		if(value.isNumeric)
			return value.to!long;

		if(value !in registers)
			registers[value] = 0;
		return registers[value].to!long;
	}
	
	void snd(string[] arguments)
	{
		send_count++;
		state = State.RUNNING;
		other.queue(get_value(arguments[1]));
		pc++;
	}

	void set(string[] arguments)
	{
		state = State.RUNNING;
		registers[arguments[1]] = get_value(arguments[2]);
		pc++;
	}

	void add(string[] arguments)
	{
		state = State.RUNNING;
		registers[arguments[1]] += get_value(arguments[2]);
		pc++;
	}

	void mul(string[] arguments)
	{
		state = State.RUNNING;
		registers[arguments[1]] *= get_value(arguments[2]);
		pc++;
	}

	void mod(string[] arguments)
	{
		state = State.RUNNING;
		registers[arguments[1]] %= get_value(arguments[2]);
		pc++;
	}

	void rcv(string[] arguments)
	{
		if(pending.length == 0)
		{
			state = State.PENDING;
			return;
		}
		state = State.RUNNING;

		auto message = pending.front;
		pending.popFront();
		registers[arguments[1]] = message;
		pc++;
	}

	void jgz(string[] arguments)
	{
		state = State.RUNNING;
		if(get_value(arguments[1]) > 0)
			pc += get_value(arguments[2]);
		else
			pc++;
	}
}

enum State
{
	PENDING, RUNNING, FINISHED
}

Fun fact : I had to store the return values of p0.next_cycle and p1.next_cycle in separate variables before checking their respective values instead of writing if(!p0.next_cycle || !p1.next_cycle) because unless I do this, the compiler will ignore the p1.next_cycle call if !p0.next_cycle ends up being true.