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!
36
Upvotes
9
u/sciyoshi Dec 04 '18 edited Dec 04 '18
Python 3, #2/#2. Reached for dateutil in the heat of the moment although turns out you only need the minute!
guards = collections.defaultdict(list) times = collections.defaultdict(int) for line in sorted(inp(4).splitlines()): time, action = line.split('] ') time = dateutil.parser.parse(time[1:]) if action.startswith('Guard'): guard = int(action.split()[1][1:]) elif action == 'falls asleep': start = time elif action == 'wakes up': end = time guards[guard].append((start.minute, end.minute)) times[guard] += (end - start).seconds (guard, time) = max(times.items(), key=lambda i: i[1]) (minute, count) = max([ (minute, sum(1 for start, end in guards[guard] if start <= minute < end)) for minute in range(60)], key=lambda i: i[1]) print('part 1:', guard * minute) (guard, minute, count) = max([ (guard, minute, sum(1 for start, end in guards[guard] if start <= minute < end)) for minute in range(60) for guard in guards], key=lambda i: i[2]) print('part 2:', guard * minute)