r/adventofcode • • Dec 16 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 16 Solutions -🎄-

--- Day 16: Chronal Classification ---


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.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 16

Transcript:

The secret technique to beat today's puzzles is ___.


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 at 00:39:03!

14 Upvotes

139 comments sorted by

View all comments

1

u/xiaoxiae Dec 16 '18

A concise way to solve problem 1 in Python 3

from re import findall


def optResultMatch(before, after, index, result):
    """Checks, whether the result of an opt code on a set of registers before
    match the result after."""
    return int(before[:index] + [result] + before[(index + 1):] == after)


def workingOptCodes(b, a, i):
    """Test all of the opt-codes and return the number of those that work."""
    return sum([optResultMatch(b, a, i[3], ocode(b, i)) for ocode in optcodes])


# the optcodes as lambda functions that take registers and instructions
optcodes = [lambda reg, inst: reg[inst[1]] + reg[inst[2]],          # addr
            lambda reg, inst: reg[inst[1]] + inst[2],               # addi
            lambda reg, inst: reg[inst[1]] * reg[inst[2]],          # mulr
            lambda reg, inst: reg[inst[1]] * inst[2],               # muli
            lambda reg, inst: reg[inst[1]] & reg[inst[2]],          # banr
            lambda reg, inst: reg[inst[1]] & inst[2],               # bani
            lambda reg, inst: reg[inst[1]] | reg[inst[2]],          # borr
            lambda reg, inst: reg[inst[1]] | inst[2],               # bori
            lambda reg, inst: reg[inst[1]],                         # setr
            lambda reg, inst: inst[1],                              # seti
            lambda reg, inst: int(inst[1] > reg[inst[2]]),          # gtir
            lambda reg, inst: int(reg[inst[1]] > inst[2]),          # gtri
            lambda reg, inst: int(reg[inst[1]] > reg[inst[2]]),     # gtrr
            lambda reg, inst: int(inst[1] == reg[inst[2]]),         # eqir
            lambda reg, inst: int(reg[inst[1]] == inst[2]),         # eqri
            lambda reg, inst: int(reg[inst[1]] == reg[inst[2]])]    # eqrr


input = open("16.in", "r")
data = input.read().splitlines()

i, total = 0, 0
while data[i] != "":
    before = [int(number) for number in findall("\d+", data[i])]
    after = [int(number) for number in findall("\d+", data[i + 2])]
    instruction = [int(s) for s in data[i + 1].split(" ")]

    if workingOptCodes(before, after, instruction) >= 3:
        total += 1

    i += 4

print(total)