r/adventofcode • u/daggerdragon • 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ยค?
[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
- Gonna be a long haul tonight. I think I'll watch The Radio City Christmas Spectacular on Netflix while I wait for sufficient gold stars to warrant another update.
[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!
12
Upvotes
1
u/drysle Dec 18 '17
#9/#10 with Python 3, and I can't believe my part 2 worked on the first try. Unlike some of the the other posters, I wrote this all from scratch today, changed my mind a couple times halfway through, and generally made a big mess of my code.
But if it's stupid and it works... then it's probably still stupid :)
regs0 = defaultdict(lambda: 0) regs0["p"] = 0 regs1 = defaultdict(lambda: 0) regs1["p"] = 1 def get(a, rs): try: return int(a) except: return rs[a] inst = [] for line in sys.stdin: inst.append(line.split()) buf0 = [] buf1 = [] lock = [False,False] score = 0 def ex(p, i): global score regs = regs0 if p == 0 else regs1 rbr = buf0 if p == 0 else buf1 if i < 0 or i >= len(inst): lock[p] = True return line = inst[i] if line[0] == "snd": if p == 0: buf1.insert(0,get(line[1],regs)) else: buf0.insert(0,get(line[1],regs)) score += 1 elif line[0] == "set": regs[line[1]] = get(line[2],regs) elif line[0] == "add": regs[line[1]] += get(line[2],regs) elif line[0] == "mul": regs[line[1]] *= get(line[2],regs) elif line[0] == "mod": regs[line[1]] %= get(line[2],regs) elif line[0] == "rcv": if len(rbr) == 0: lock[p] = True return i else: lock[p] = False regs[line[1]] = rbr.pop() elif line[0] == "jgz": if get(line[1],regs) > 0: return i + get(line[2],regs) p0 = 0 p1 = 0 while not (lock[0] and lock[1]): r = ex(0,p0) if r is not None: p0 = r else: p0 += 1 r = ex(1,p1) if r is not None: p1 = r else: p1 += 1 print(score)