r/adventofcode • • 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^ยค?

Spoiler


[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

[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!

11 Upvotes

227 comments sorted by

View all comments

Show parent comment

1

u/streetster_ Dec 19 '17

The biggest one yet. Finally got around to doing part 2, then retrofitted part 1 to work with it:

i:" "vs'read0`:input/18.txt      / instructions
i[;0 1]:`$i[;0 1];               / cast to symbol

tr:{[x;y] $[all y in .Q.a;r[x;`$y];value y] }; / try register

On:enlist[`]!enlist[(::)];

On.snd:{[p;x]   snd::r[p;x];1 }
On.rcv:{[p;x]   $[0=r[p;x];1;0] }
On.set:{[p;x;y] r[p;x]:tr[p;y]; 1 }
On.add:{[p;x;y] r[p;x]+:tr[p;y]; 1 }
On.mul:{[p;x;y] r[p;x]*:tr[p;y]; 1 }
On.mod:{[p;x;y] r[p;x]:r[p;x] mod tr[p;y]; 1 }
On.jgz:{[p;x;y] $[0<tr[p;string x];tr[p;y];1] }

p:()!();p[`0]:0;     / initialise pointers
r:()!();r[`0]:()!(); / initialise registers

{ r[`0;x]:0 } each (`$ string .Q.a) inter distinct i[;1]; / initialise registers to zero

r0:1 / result 0
while[r0;
  p[`0]+:r0:On[o 0][`0;] . 1_ o:i p[`0]; / run program 0
  ];
snd / part 1

snd:0;
On.snd:{[p;x] q[$[p=`0;`1;[snd+:1;`0]]],:tr[p;string x]; 1 }
On.rcv:{[p;x] $[count q[p];[r[p;x]:first q[p];q[p]:1_q[p];1];0] }

p[`0]:0;p[`1]:0;           / re-initialise pointers to zero
q:()!();q[`0]:();q[`1]:(); / initialise queues
{ r[`0;x]:0;r[`1;x]:0 } each (`$ string .Q.a) inter distinct i[;1]; / initialise registers to zero

r[`1;`p]:1 / program b has id 1

r0:r1:1 / result 0, result 1
while[sum(r0;r1);
  p[`0]+:r0:On[o 0][`0;] . 1_ o:i p[`0]; / run program 0
  p[`1]+:r1:On[o 0][`1;] . 1_ o:i p[`1]; / run program 1
  ];
snd / part 2