AoC 2016 Day 10: Bot Simulator

Source: Balance Bots

Part 1: Create a sorting machine using input of the following form:

  • value X goes to bot A - an input to bot A
  • bot A gives low to (bot|output) B and high to (bot|output) C - a sorter that takes two inputs and sends them to the specified bots or output channels

Find the bot that compares the values 17 and 61.

A bit of an odd problem. The bots sound like self contained entities, so we’ll create a class for them:

class Bot(object):
    cache = {}

    def __init__(self, name): = name
        self.values = set()
        self.low_output = None
        self.high_output = None
        self.compared = []

        if args.debug:
            print('{} created'.format(self))

    def get(name):
        if not name in Bot.cache:
            Bot.cache[name] = Bot(name)

        return Bot.cache[name]

    def all():
        for name in sorted(list(Bot.cache.keys())):
            yield Bot.get(name)

    def __str__(self):
        return 'Bot<{name}, {values}, low:{low_output}, high:{high_output}>'.format(
            name =,
            values = list(sorted(self.values)),
            low_output = self.low_output,
            high_output = self.high_output,

    def give(self, value):
        if args.debug:
            print('{} given {}'.format(self, value))


        if len(self.values) == 2:

            if not self.low_output or not self.high_output:
                #raise Exception('{} got a second value but has not output'.format(self))
                print('{} got a second value but has not output'.format(self))



After that, we can use regular expressions to parse the input:

re_value = re.compile(r'value (?P<value>\d+) goes to (?P<name>(?:(bot|output)) (\d+))')
re_mapping = re.compile(r'(?P<input>(?:(bot|output)) (\d+)) gives low to (?P<low_output>(?:(bot|output)) (\d+)) and high to (?P<high_output>(?:(bot|output)) (\d+))')

values = list()

with open(args.input, 'r') as fin:
    for line in fin:
        if args.debug:

        m_value = re_value.match(line)
        m_mapping = re_mapping.match(line)

        if m_value:
            values.append(('name'), int('value'))))

        elif m_mapping:
            input ='input')
            low_output ='low_output')
            high_output ='high_output')

            Bot.get(input).low_output = low_output
            Bot.get(input).high_output = high_output

Now that the chain of bots is set up, we can send in the input values and then check which bots compared what:

for name, value in values:

for bot in Bot.all():
    if set(args.targets) in bot.compared:
        print(bot, 'compared', args.targets)

This specifically needs to happen after all of the bots are set up, since you cannot output to a bot if you haven’t see the input before the bots definition. That’s why the values are cached in values.

A neat little bit of code.

Part 2: What is the product output0 * output1 * output2?

I’m actually just simulating outputs as bots that are named output, so find the final values of those:

print('output0 * output1 * output2 = {}'.format(
    list(Bot.get('output 0').values)[0]
    * list(Bot.get('output 1').values)[0]
    * list(Bot.get('output 2').values)[0]

And that’s it.

A neat simulation.

comments powered by Disqus