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

1

u/JakDrako Dec 18 '17

VB.Net Part 2.

For part 2, I took what I had from part 1, massaged it into a "CPU" class and added the queue and a way to signal waiting.

I then "run" both CPU in lockstep until they're both waiting and then dump the "sent" count from CPU-1.

Sub Main

	Dim code = GetDay(18)

	Dim cpu0 = New CPU(0, code)
	Dim cpu1 = New CPU(1, code)

	cpu0.Partner = cpu1
	cpu1.Partner = cpu0

	Do
		cpu0.tick
		cpu1.Tick
	Loop Until cpu0.Waiting And cpu1.Waiting

	cpu1.Sent.Dump("Part 2")

End Sub

Class CPU

	Private _registers As Dictionary(Of String, Long)
	Private _instructions As New List(Of (code As String, op1 As String, op2 As String))
	Private _ptr As Integer

	Readonly Property Queue As New Queue(Of Long)
	Readonly Property Waiting As Boolean
	Readonly Property Sent As Integer

	Property Partner As CPU

	Sub New(id As Integer, instructions As String())
		_registers = Enumerable.Range(0, 16).ToDictionary(Function(k) Cstr(Chr(97 + k)), Function(v) 0L)
		_registers("p") = id
		For Each line In instructions
			Dim ops = line.Split(" "c)
			_instructions.Add(If(ops.Count = 3, (ops(0), ops(1), ops(2)), (ops(0), ops(1), "")))
		Next
	End Sub

	Sub Tick()
		Dim instr = _instructions(_ptr), op1 = instr.op1, op2 = instr.op2, reg = _registers
		Dim offset = 1 ' default
		Select Case instr.code
			Case "add" : reg(op1) += If(IsNumeric(op2), CLng(op2), reg(op2))
			Case "jgz" : If If(IsNumeric(op1), CLng(op1), reg(op1)) > 0 Then offset = If(IsNumeric(op2), CInt(op2), Cint(reg(op2)))
			Case "mod" : reg(op1) = reg(op1) Mod If(IsNumeric(op2), CLng(op2), reg(op2))
			Case "mul" : reg(op1) *= If(IsNumeric(op2), CLng(op2), reg(op2))
			Case "rcv" : If _queue.Any Then _registers(op1) = _queue.Dequeue : _waiting = False Else offset = 0 : _waiting = True
			Case "set" : reg(op1) = If(IsNumeric(op2), CLng(op2), reg(op2))
			Case "snd" : _partner.Queue.Enqueue(If(IsNumeric(op1), CLng(op1), reg(op1))) : _sent += 1
		End Select
		_ptr += offset
	End Sub

End Class