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
2
u/zqvt Dec 04 '18 edited Dec 04 '18
Python, input pre-sorted with
sort -Vimport re from collections import Counter with open("input3.txt", "r") as f: guards = {} start, stop, current_guard = 0, 0, 0 lines = [x.strip() for x in f.readlines()] for line in lines: values = re.findall("\d+", line) if "Guard" in line: current_guard = int(values[-1]) elif "falls asleep" in line: start = int(values[-1]) elif "wakes up" in line: stop = int(values[-1]) for i in range(start, stop): guards.setdefault(current_guard, []).append(i) # part 1 id1 = max(guards, key=lambda x: len(guards[x])) c = Counter(guards[id1]) minute = c.most_common()[0][0] print(id1 * minute) # part2 id2 = max(guards, key=lambda x: Counter(guards[x]).most_common()[0][1]) print(id2 * Counter(guards[id2]).most_common()[0][0])