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!

39 Upvotes

340 comments sorted by

View all comments

1

u/lib20 Dec 11 '18

In Red, part 1, Execution time: 0:00:00.345739

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
foreach [guard minute-profile] body-of guards-sleeping-profile [
	guard-slept: 0
	foreach [minute slept] body-of minute-profile [
		guard-slept: guard-slept + slept	
	]	
	if guard-slept > max-minutes-slept [
		guard-sleeps-most: guard
		max-minutes-slept: guard-slept
	]
]


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

print rejoin ["Execution time: " now/time/precise - time-start]
probe rejoin ["guard sleeps most: " guard-sleeps-most "    sleeps most at minute: " minute-most-slept "    for " max-minutes " times"]
print guard-sleeps-most * minute-most-slept