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

1

u/lib20 Dec 11 '18

In Red, part 2, Execution time: 0:00:00.33641

Red []

time-start: now/time/precise
input-txt: sort read/lines %input.txt	 
input-txt: form input-txt
input-txt: load input-txt

actions: [begins falls wakes]

shifts: parse input-txt [
    collect some collect [
        into [keep pick to end]
        opt [skip set ID issue!] keep (load form ID)
        set action skip keep (index? find actions action) skip
    ]
]


shifts: new-line/all shifts on

range: func [mi [integer!]
			ma [integer!]
			/local bl [block!]
		][
	bl: copy []
	while [mi < ma] [
		append bl mi
		mi: mi + 1
	]	
	bl
]

day: guard: 0
sleep-start: 00:00

guards-sleeping-profile: #()

foreach shift shifts [
	if shift/4 = 1 [
		guard: shift/3
		unless find guards-sleeping-profile guard [
			; create minute profile for that guard
			minute-profile: copy #()
			repeat mi 60 [put minute-profile mi - 1 0]
			put guards-sleeping-profile guard minute-profile
		]
	]
	if shift/4 = 2 [sleep-start: shift/2 sleep-start: sleep-start/minute]
	if shift/4 = 3 [
		sleep-end: shift/2
		sleep-end: sleep-end/minute
		minute-profile: select guards-sleeping-profile guard	

		while [sleep-start < sleep-end] [

			put minute-profile sleep-start (select minute-profile sleep-start) + 1
			sleep-start: sleep-start + 1
		]
		put guards-sleeping-profile guard minute-profile
	]
]

guard-sleeps-most: 0
max-minutes-slept: 0
minute-most-slept: -1
foreach [guard minute-profile] body-of guards-sleeping-profile [
	foreach [minute slept] body-of minute-profile [
		if slept > max-minutes-slept [
			max-minutes-slept: slept
		    guard-sleeps-most: guard
			minute-most-slept: minute
		]
	]	
]

print rejoin ["Execution time: " now/time/precise - time-start]
print rejoin ["part 02: " guard-sleeps-most * minute-most-slept]