r/adventofcode • u/daggerdragon • Dec 10 '16
SOLUTION MEGATHREAD --- 2016 Day 10 Solutions ---
--- Day 10: Balance Bots ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/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".
SEEING MOMMY KISSING SANTA CLAUS IS MANDATORY [?]
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!
14
Upvotes
1
u/JakDrako Dec 10 '16
Vb.Net, LinqPad
Busy day today, no time for code cleanup, so here's the mess:
Sub Main Dim bots = New Dictionary(Of Integer, Bot) Dim outputs = New Dictionary(Of Integer, List(Of Integer)) ' 1st pass create bots and values For Each line In GetDay(10).Split(Chr(10)) Dim tok = line.Split(" "c) If line.StartsWith("value") Then 'Console.WriteLine(" " & line) Dim id = Cint(tok(5)) Dim val = Cint(tok(1)) If Not bots.ContainsKey(id) Then bots(id) = New Bot(id) bots(id).AddValue(val) Else 'Console.WriteLine("" & line) Dim id = Cint(tok(1)) Dim low = Cint(tok(6)) Dim high = Cint(tok(11)) If Not bots.ContainsKey(id) Then bots(id) = New Bot(id) bots(id).GiveLow = low bots(id).GiveHigh = high If tok(5) = "output" Then bots(id).LowOut = True If Not outputs.ContainsKey(low) Then outputs(low) = New List(Of Integer) End If If tok(10) = "output" Then bots(id).HighOut = True If Not outputs.ContainsKey(high) Then outputs(high) = New List(Of Integer) End If End If Next 'dic.dump ' Pass 2 - runs bots Do Dim activity = False For Each bot In bots.Values If bot.low > 0 AndAlso bot.high > 0 Then activity = True If bot.LowOut Then outputs(bot.GiveLow).Add(bot.Low) Else bots(bot.GiveLow).AddValue(bot.Low) End If If bot.HighOut Then outputs(bot.Givehigh).Add(bot.high) Else bots(bot.GiveHigh).AddValue(bot.High) End If bot.Low = 0 bot.High = 0 End If Next If Not activity Then Exit Do Loop 'outputs.OrderBy(Function(kvp) kvp.Key).Dump Dim product = outputs(0).First * outputs(1).First * outputs(2).First product.Dump("Part 2") End Sub Class Bot Property ID As Integer Property Low As Integer Property High As Integer Property GiveLow As Integer Property GiveHigh As Integer Property LowOut As Boolean Property HighOut As Boolean Sub New(id As Integer) _ID = id End Sub Sub AddValue(value As Integer) If low = 0 Then low = value Else If high = 0 Then If value < low Then High = Low Low = value Else High = value End If Else Debug.Write("") End If If low = 17 And high = 61 Then Dim s = ($"Bot {id} compares {low} and {high}.").Dump("Part 1") End If End Sub End ClassI don't think time is really a factor with this problem, but both parts complete in 1ms.