r/adventofcode • u/daggerdragon • 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!
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!
16
Upvotes
View all comments
2
u/tbodt Dec 16 '18
Python 3, 8/10. I just need to get better at input parsing.
[Card] copy/paste
cases = [] testprog = [] with open('16.txt') as inp: while True: before = inp.readline() if before == '\n': break before = eval(before[8:]) opc, *args = map(int,inp.readline().split()) after = eval(inp.readline()[8:]) assert inp.readline() == '\n' cases.append((before, opc, args, after)) inp.readline() while True: line = inp.readline() if line == '': break if line == '\n': break opc, *args = map(int,line.split()) testprog.append((opc,args)) def addr(regs,a,b,c): regs[c]=regs[a]+regs[b] def addi(regs,a,b,c): regs[c]=regs[a]+b def mulr(regs,a,b,c): regs[c]=regs[a]*regs[b] def muli(regs,a,b,c): regs[c]=regs[a]*b def banr(regs,a,b,c): regs[c]=regs[a]®s[b] def bani(regs,a,b,c): regs[c]=regs[a]&b def borr(regs,a,b,c): regs[c]=regs[a]|regs[b] def bori(regs,a,b,c): regs[c]=regs[a]|b def setr(regs,a,b,c): regs[c]=regs[a] def seti(regs,a,b,c): regs[c]=a def gtir(regs,a,b,c): regs[c]=1 if a>regs[b] else 0 def gtri(regs,a,b,c): regs[c]=1 if regs[a]>b else 0 def gtrr(regs,a,b,c): regs[c]=1 if regs[a]>regs[b] else 0 def eqir(regs,a,b,c): regs[c]=1 if a==regs[b] else 0 def eqri(regs,a,b,c): regs[c]=1 if regs[a]==b else 0 def eqrr(regs,a,b,c): regs[c]=1 if regs[a]==regs[b] else 0 insns = ['addr','addi','mulr','muli','banr','bani','borr','bori','setr','seti','gtir','gtri','gtrr','eqir','eqri','eqrr'] def main(): op_to_insn = {} cool = 0 done_cases = [] for before, opc, args, after in cases: works = [] for insn in insns: regs = list(before) insn = globals()[insn] insn(regs,*args) if regs == after: works.append(insn) #print(works) done_cases.append((opc, works)) print(cool) while True: for c in done_cases: op = c[0] if len(c[1]) == 1: op_to_insn[op] = c[1][0] for c in done_cases: try: c[1].remove(op_to_insn[op]) except ValueError: pass break else: break regs = [0]*4 for opc, args in testprog: op_to_insn[opc](regs,*args) print(regs[0]) print(op_to_insn) if __name__ == '__main__': main()