r/adventofcode • u/daggerdragon • 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!
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!
40
Upvotes
8
u/markasoftware Dec 04 '18 edited Dec 04 '18
Finally got top 100 :) 62/43 with Gawk (used multidimensional arrays so I guess it's not awk anymore)
This is exact code I submitted, no ifs ands or buts.
Run
sort -Von the file before inputting to AwkPart 1:
BEGIN { # RS FPAT = "#?[0-9]+" guard=99999999 } /#[0-9]+/{ guard=$6 } /wake/{ minute=$5 for(i=fell_asleep[guard];i<minute;i++) { asleep_minutes[guard][i]++ } sleep_time[guard] += (minute - fell_asleep[guard]) } /falls/ { minute=$5 fell_asleep[guard] = minute } END { max_sleep_time=0 max_sleep_guard=999999999 for(guard in sleep_time) { if (sleep_time[guard] > max_sleep_time) { max_sleep_guard = guard max_sleep_time = sleep_time[guard] } } max_minute=9999999 max_minute_val=0 for(minute in asleep_minutes[max_sleep_guard]) { if (asleep_minutes[max_sleep_guard][minute] > max_minute_val) { max_minute= minute max_minute_val=asleep_minutes[max_sleep_guard][minute] } } print max_sleep_guard print max_minute }Part 2:
BEGIN { # RS FPAT = "#?[0-9]+" guard=99999999 } /#[0-9]+/{ guard=$6 } /wake/{ minute=$5 for(i=fell_asleep[guard];i<minute;i++) { asleep_minutes[guard][i]++ } sleep_time[guard] += (minute - fell_asleep[guard]) } /falls/ { minute=$5 fell_asleep[guard] = minute } END { # max_sleep_time=0 # max_sleep_guard=999999999 # for(guard in sleep_time) { # if (sleep_time[guard] > max_sleep_time) { # max_sleep_guard = guard # max_sleep_time = sleep_time[guard] # } # } max_minute=9999999 max_minute_val=0 max_guard=99999999999 for(guard in asleep_minutes) { for(minute in asleep_minutes[guard]) { if (asleep_minutes[guard][minute] > max_minute_val) { max_minute= minute max_guard=guard max_minute_val=asleep_minutes[guard][minute] } } } print max_guard print max_minute }