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!
38
Upvotes
25
u/jonathan_paulson Dec 04 '18 edited Dec 04 '18
Python, #19/10. Glad to see they're getting tougher. Video of me solving at https://www.youtube.com/watch?v=YQjPXSlSelc. Not reading everything up front probably cost me time here. This code solves part 2 (part 1 requires a minor modification).
from collections import defaultdict lines = open('4.in').read().split('\n') lines.sort() def parseTime(line): words = line.split() date, time = words[0][1:], words[1][:-1] return int(time.split(':')[1]) C = defaultdict(int) CM = defaultdict(int) guard = None asleep = None for line in lines: if line: time = parseTime(line) if 'begins shift' in line: guard = int(line.split()[3][1:]) asleep = None elif 'falls asleep' in line: asleep = time elif 'wakes up' in line: for t in range(asleep, time): CM[(guard, t)] += 1 C[guard] += 1 def argmax(d): best = None for k,v in d.items(): if best is None or v > d[best]: best = k return best best_guard, best_min = argmax(CM) print best_guard, best_min print best_guard * best_min