r/adventofcode • • Dec 05 '16

SOLUTION MEGATHREAD --- 2016 Day 5 Solutions ---

--- Day 5: How About a Nice Game of Chess? ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).


STAYING ON TARGET 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

188 comments sorted by

View all comments

1

u/JakDrako Dec 05 '16

VB.Net, LinqPad

I really liked this puzzle, a lot of fun. I think I'll try a parallel version for the second part.

Part 1

Sub Main
	Dim md5 = System.Security.Cryptography.MD5.Create
	Dim key = "abc"
	Dim pass = ""
	Dim i = 0
	Do
		Dim hash = md5.ComputeHash(Encoding.ASCII.GetBytes(key & i))
		If hash(0) = 0 Andalso hash(1) = 0 Andalso hash(2) < 16 Then
			pass &= hash(2).ToString("X2")(1)
			If pass.Length = 8 Then Exit Do
		End If
		i += 1
	Loop
	pass.ToLowerInvariant.Dump("password")
End Sub

Part 2

Sub Main
	Dim md5 = System.Security.Cryptography.MD5.Create
	Dim key = "abc"
	Dim pass = "........".ToCharArray, found = 0
	Dim i = 0
	Do
		Dim hash = md5.ComputeHash(Encoding.ASCII.GetBytes(key & i))
		If hash(0) = 0 AndAlso hash(1) = 0 AndAlso hash(2) < 16 Then
			Dim pos = hash(2) And 15
			If pos < 8 Then
				If pass(pos) = "." Then
					pass(pos) = hash(3).ToString("X2")(0)
					found += 1
					If found = 8 Then Exit Do
				End If
			End If
		End If
		i += 1
	Loop
	String.Join("", pass).ToLowerInvariant.Dump("Password")
End Sub

2

u/JakDrako Dec 05 '16

VB.Net, LinqPad

Parallel versions

Part 1 (Runs "abc" in ~5 seconds)

Sub Main

	Dim key = "abc"

	Dim chunck = 100000
	Dim dic = New ConcurrentDictionary(Of Integer, Byte())
	Dim maxThreads = New SemaphoreSlim(Environment.ProcessorCount)

	For i = 0 To Integer.MaxValue Step chunck

		maxThreads.Wait

		Dim start = i
		Dim count = chunck - 1

		Dim tsk = Task.Factory.StartNew(
			Sub()
				Dim md5 = Security.Cryptography.MD5.Create
				'Console.WriteLine($"Starting thread for {start} to {start + count}")
				For n = start To start + count
					Dim hash = md5.ComputeHash(Encoding.ASCII.GetBytes(key & n))
					If hash(0) = 0 Andalso hash(1) = 0 Andalso hash(2) < 16 Then dic.TryAdd(n, hash)
				Next
			End Sub).ContinueWith(Sub(t) maxthreads.Release)
		If dic.Count >= 8 Then Exit For
	Next

	Dim pass = String.Join("", dic.OrderBy(Function(kvp) kvp.Key) _
									.Select(Function(kvp) kvp.Value(2).ToString("X2")(1)) _
									.Take(8))
	pass.ToLowerInvariant.dump("Password")

End Sub

Part 2 (Runs "abc" in ~7 seconds)

Sub Main

	Dim key = "abc"

	Dim chunck = 100000
	Dim dicPos = New ConcurrentDictionary(Of Integer, Integer)
	For pos = 0 To 7
		dicpos.TryAdd(pos, Integer.MaxValue)
	Next
	Dim dicHash = New ConcurrentDictionary(Of Integer, Byte())
	Dim maxThreads = New SemaphoreSlim(Environment.ProcessorCount)

	For i = 0 To Integer.MaxValue Step chunck

		maxThreads.Wait

		Dim start = i
		Dim count = chunck - 1

		Dim tsk = Task.Factory.StartNew(
			Sub()
				Dim md5 = Security.Cryptography.MD5.Create
				'Console.WriteLine($"Starting thread for {start} to {start + count}")
				For n = start To start + count
					Dim hash = md5.ComputeHash(Encoding.ASCII.GetBytes(key & n))
					If hash(0) = 0 Andalso hash(1) = 0 Andalso hash(2) < 16 Then
						Dim pos = hash(2) And 15
						If pos < 8 Then
							If n < dicPos(pos) Then
								dicpos(pos) = n
								dicHash(pos) = hash
							End If
						End If
					End If
				Next
			End Sub).ContinueWith(Sub(t) maxthreads.Release)
		Dim done = True
		For Each kvp In dicpos
			If kvp.Value = Integer.MaxValue Then done = False : Exit For
		Next
		If done Then Exit For
	Next

	Dim pass = String.Join("", dicHash.OrderBy(Function(kvp) kvp.Key) _
									  .Select(Function(kvp) kvp.Value(3).ToString("X2")(0)) _
									  .Take(8))
	pass.ToLowerInvariant.dump("Password")

End Sub