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

1

u/sotsoguk Dec 05 '18 edited Dec 05 '18

Not a very elegant solution, but due to my timezone (CET), work and being sick i was late to the game and lost time to a nasty bug. I set the end of the sleeping intervals 1 minute early. The solution to part 1 was nevertheless correct, which almost drove me crazy ...

import re
import numpy as np
import operator
with open('input.txt') as f:
    lines = f.readlines()

lines = [line.strip() for line in lines]
lines.sort()
guards = {}

activeGuard = 0
startSet = False
startTime = 0
endTime = 0
for l in lines:
    nums = list(map(int,re.findall(r'\d+',l)))
    if len(nums) == 6:
        gnum = nums[5]
        activeGuard = gnum
        if gnum not in guards.keys():
            guards[gnum] = np.zeros(60)
    elif not startSet:
        startTime = nums[4]
        startSet = True
    else:
        startSet = False
        endTime = nums[4]
        guards[activeGuard][startTime:endTime]+=1
        

guardsTime={}
guardsMax = {}
guardsMaxMin = {}
for k,v in guards.items():
    guardsTime[k] = np.sum(v)
for k,v in guards.items():
    guardsMax[k] = max(guards[k])
    guardsMaxMin[k] = np.argmax(guards[k])

maxGuard =  max(guardsMax.items(), key=operator.itemgetter(1))[0]

# Part1
sleepyGuard = max(guardsTime.items(), key=operator.itemgetter(1))[0]
minute=np.argmax(guards[sleepyGuard])
print("Part1:\t" + str(sleepyGuard * minute))

# Part2
print("Part2:\t"+ str(maxGuard*guardsMaxMin[maxGuard]))

Edit: Finally correct code block formatting