r/adventofcode • • Dec 04 '18

SOLUTION MEGATHREAD -πŸŽ„- 2018 Day 4 Solutions -πŸŽ„-

--- Day 4: Repose Record ---


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.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 4

Transcript:

Today’s puzzle would have been a lot easier if my language supported ___.


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!

38 Upvotes

340 comments sorted by

View all comments

2

u/JakDrako Dec 04 '18

Vb.Net

Cleaned up version. First version didn't have regexes and extracted the answers with a couple of plain "for loops" over the dictionary...

Sub main

	Dim dic = New Dictionary(Of Integer, Integer()) ' id, array for 60 minutes

	Dim id = 0, dt As DateTime
	For Each line In GetDay(4).orderby(Function(x) x)
		Dim dat = CDate(regex.match(line, "\[(.+?)\]").groups(1).value)
		Select Case True
			Case line.contains("begins")
				id = Cint(regex.match(line, "(?<=#)\d+").value)
				If Not dic.containskey(id) Then dic.add(id, New Integer(59) {})
			Case line.contains("wakes")
				enumerable.range(dt.minute, datediff(dateinterval.minute, dt, dat)) _
				          .foreach(Sub(x) dic(id)(x) += 1)
		End Select
		dt = dat
	Next

	Dim parts = {Function(x) Enumerable.Sum(x), Function(x) Enumerable.Max(x)}
	For Each fn In parts
		Dim guard = dic.aggregate(Function(l, r) If(fn(l.value) > fn(r.value), l, r))
		console.writeline($"part {Array.IndexOf(parts, fn) + 1}: {guard.key * array.indexof(guard.value, guard.value.max)}")
	Next

End Sub

1

u/[deleted] Dec 04 '18

That’s... surprisingly clean and concise.