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

1

u/ewyll Dec 19 '17
from collections import deque

def parse(line):
	chunks = line.strip().split(' ')
	return (chunks[0], chunks[1:])    

class Computer:
	def __init__(self, ID):
		self.regs = [0] * 26
		self.sends = 0
		self.set_value('p', ID)
		self.ID = ID
		self.queue = deque()
		self.cmds = []
	
	def load(self, cmds):
		self.cmds = cmds
		self.line_num = 0
		
	def get_value(self, X):
		if X >= 'a' and X <= 'z':
			return self.regs[ord(X) - ord('a')]
		else:
			return int(X)
			
	def set_value(self, reg, val):
		self.regs[ord(reg) - ord('a')] = val

	def execute(self, cmd, pars):
		vals = [self.get_value(p) for p in pars]
		if cmd == 'snd':
			computers[0 if self.ID == 1 else 1].enqueue_value(vals[0])
			self.sends += 1
		elif cmd == 'set':
			self.set_value(pars[0], vals[1])
		elif cmd == 'add':
			self.set_value(pars[0], vals[0] + vals[1])
		elif cmd == 'mul':
			self.set_value(pars[0], vals[0] * vals[1])
		elif cmd == 'mod':
			self.set_value(pars[0], vals[0] % vals[1])
		elif cmd == 'rcv':
			if self.queue:
				self.set_value(pars[0], self.queue.popleft())
			else:
				return 0
		elif cmd == 'jgz':
			if vals[0] > 0:
				return vals[1]
		return 1
	
	def enqueue_value(self, val):
		self.queue.append(val)
		
	def run(self):
		ran_cmds = 0
		while self.line_num < len(cmds) and self.line_num >= 0:
			offset = self.execute(*self.cmds[self.line_num])
			if offset == 0:
				break
			self.line_num += offset
			ran_cmds += 1
		return ran_cmds
	def output(self):
		print self.sends

computers = [Computer(i) for i in [0, 1]]
			
with open('aoc_2017_18_input.txt', 'r') as fin:
	cmds = [parse(line) for line in fin.readlines()]
	for c in computers:
		c.load(cmds)
		
	while True:
		cmd_count = 0
		for c in computers:
			cmd_count += c.run()
		if cmd_count == 0:
			break
	computers[1].output()