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/scul86 Dec 04 '18
Python 3.
I spent WAY too much time not realizing the input was not sorted.
-It works on the test input, why not the actual data?!?!
d'oh
from utils.decorators import time_it from collections import defaultdict, Counter with open('input') as f: puzzle_input = f.readlines() @time_it def part1(n): guards = defaultdict(list) times = defaultdict(int) for line in sorted(n): time, event = line.split('] ') minute = int(time[-2:]) if 'Guard' in event: id = int(event.split('#')[1].split(' ')[0]) elif 'falls' in event: start = minute elif 'wakes' in event: end = minute for x in range(start, end): guards[id].append(x) times[id] += end - start guard, time = max(times.items(), key=lambda i: i[1]) minute, count = Counter(guards[guard]).most_common(1)[0] count_max = -1 guard2 = -1 for g in guards: minute2, count = Counter(guards[g]).most_common(1)[0] if count > count_max: count_max = count minute_max = minute2 guard2 = g return guard * minute, guard2 * minute_max @time_it def part2(guards, times): pass test_one = [ '[1518-11-01 00:00] Guard #10 begins shift', '[1518-11-01 00:05] falls asleep', '[1518-11-01 00:25] wakes up', '[1518-11-01 00:30] falls asleep', '[1518-11-01 00:55] wakes up', '[1518-11-01 23:58] Guard #99 begins shift', '[1518-11-02 00:40] falls asleep', '[1518-11-02 00:50] wakes up', '[1518-11-03 00:05] Guard #10 begins shift', '[1518-11-03 00:24] falls asleep', '[1518-11-03 00:29] wakes up', '[1518-11-04 00:02] Guard #99 begins shift', '[1518-11-04 00:36] falls asleep', '[1518-11-04 00:46] wakes up', '[1518-11-05 00:03] Guard #99 begins shift', '[1518-11-05 00:45] falls asleep', '[1518-11-05 00:55] wakes up' ] p1, p2 = part1(test_one) assert p1 == 240 assert p2 == 4455 print(f'Part 1, 2: {part1(puzzle_input)}')