Skip to content

Instantly share code, notes, and snippets.

@pawlos
Created December 18, 2017 08:50
Show Gist options
  • Save pawlos/d2d5d12b1cf2f14995115a8308ce094e to your computer and use it in GitHub Desktop.
Save pawlos/d2d5d12b1cf2f14995115a8308ce094e to your computer and use it in GitHub Desktop.
Solution to Day 18: Duet
#!/bin/python
#aoc_d18.py
inp = [
"set a 1",
"add a 2",
"mul a a",
"mod a 5",
"snd a",
"set a 0",
"rcv a",
"jgz a -1",
"set a 1",
"jgz a -2"]
inp = open('input_d18.txt','r').readlines()
regs = {'p':0}
last_freq = 0
pc = 0
import re
while True:
i = inp[pc]
print i
if i.startswith("set"):
m = re.match("set\s([a-z])\s(-?\d+)",i)
if m:
regs[m.group(1)] = int(m.group(2))
else:
m = re.match("set\s([a-z])\s([a-z])",i)
regs[m.group(1)] = regs[m.group(2)]
if i.startswith("add"):
m = re.match("add\s([a-z])\s(-?\d+)",i)
regs[m.group(1)] += int(m.group(2))
if i.startswith("mul"):
m = re.match("mul\s([a-z])\s([a-z])",i)
if m:
regs[m.group(1)] *= regs[m.group(2)]
else:
m = re.match("mul\s([a-z])\s(-?\d+)",i)
regs[m.group(1)] *= int(m.group(2))
if i.startswith("snd"):
m = re.match("snd\s([a-z])",i)
last_freq = regs[m.group(1)]
if i.startswith("mod"):
m = re.match("mod\s([a-z])\s(-?\d+)",i)
if m:
regs[m.group(1)] %= int(m.group(2))
else:
m = re.match("mod\s([a-z])\s([a-z])",i)
regs[m.group(1)] %= regs[m.group(2)]
if i.startswith("rcv"):
m = re.match("rcv\s([a-z])",i)
if regs[m.group(1)] != 0:
print last_freq
break
if i.startswith("jgz"):
m = re.match("jgz\s([a-z])\s(-?\d+)",i)
if m:
if regs[m.group(1)] > 0:
pc += int(m.group(2))
continue
else:
m = re.match("jgz\s([a-z])\s([a-z])",i)
if regs[m.group(1)] > 0:
pc += regs[m.group(2)]
continue
pc += 1
if pc >= len(inp):
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment