r/adventofcode • • Dec 13 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 13 Solutions -๐ŸŽ„-

--- Day 13: Packet Scanners ---


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


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!

16 Upvotes

199 comments sorted by

View all comments

1

u/JakDrako Dec 13 '17

VB.Net I love those kinds of problem where the solution to the 1st part gets unbearably slow for the 2nd one if done using brute force.

Like many, I got the 1st part done and then tried to use the same some for the second part... and stopped after some minutes (went to get coffee, was still running when I got back...)

I tried figuring out a formula for the "to and fro" of the values, but ended up simply building a list that goes 0,1,2,3,4,3,2,1 and using "time" modulo length to get the position at time X.

Here's the final cleaned up code for both parts, completes in ~130ms on my PC.

Sub Main

	Dim input = GetDay(13)

	Dim fw = input.Select(Function(x) x.Split(":"c)) _
	              .Select(Function(x) New Firewall(Cint(x(0)), Cint(x(1)))) _
				  .ToDictionary(Function(k) k.depth, Function(v) v)

	' part 1
	Dim sum = 0
	For Each f In fw.Values
		If f.Position(f.depth) = 0 Then sum += (f.depth * f.range)
	Next
	sum.Dump("Part 1")

	' part 2
	Dim delay = 0, caught = False
	Do
		delay += 1
		caught = False
		For Each f In fw.Values ' no need to check where there are no "layers"
			If f.position(f.depth + delay) = 0 Then caught = True : Exit For
		Next
	Loop Until Not caught
	delay.dump("Part 2")

End Sub

Class Firewall
	Property depth As Integer
	Property range As Integer
	Property rng As New List(Of Integer)
	Sub New(depth As Integer, range As Integer)
		_depth = depth
		_range = range
		_rng = Enumerable.Range(0, range).ToList ' build a list 0..N..1 (0,1,2,3,4,3,2,1)
		If range > 1 Then _rng.AddRange(Enumerable.Range(2, range - 2).Select(Function(x) range - x))
	End Sub
	Function Position(time As Integer) As Integer
		Return _rng(time Mod _rng.count)
	End Function
End Class