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

Show parent comment

1

u/0rac1e Dec 04 '18

Python 3

Re-implementation of the above in Python, just because.

I laugh in the face of PEP8!

from collections import Counter, defaultdict

# Helper functions
get_guard          = lambda x: int(x[26:].split()[0])
get_mins           = lambda x: int(x[15:17])
total_sleep_length = lambda x: sum(x[1].values())
most_slept_minute  = lambda x: max(x[1].values())
magic_value        = lambda x: x[0] * max(x[1].items(), key=lambda y: y[1])[0]

# State variables
sleep = defaultdict(Counter)

# Data collection
for line in sorted(open('input').readlines()):
    if   'Guard' in line: guard = get_guard(line)
    elif 'falls' in line: falls = get_mins(line)
    elif 'wakes' in line: sleep[guard].update(range(falls, get_mins(line)))

# Output solution
for func in (total_sleep_length, most_slept_minute):
    print(magic_value(max(sleep.items(), key=func)))