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/Sampsc Dec 05 '18

Can someone help me with my solution in python? I'm kind of a newbie and today i was at work all day so i couldn't put much effort into it. Not the most elegant solution but i can't figure out what's wrong

for i in lines:
    month=int(i[6:8])
    day = int(i[9:11])
    hour= int(i[12:14])
    minute[cnt] = int(i[15:17])
    time[cnt]=month*30*24*60 + day*24*60 + hour *60 + minute[cnt]
    if "begins shift" in i:
        guard=i.split("#")[1]
        guard=guard.split(" ")[0]
        guard_id[cnt]=int(guard)
        #if asleep[cnt]==1:
         #   asleep[cnt:]-=1
    if  "asleep" in i:
        asleep[cnt]+=1
    if "wakes" in i:
        awake[cnt]+=1
    cnt += 1

guard_id=guard_id[time.argsort()]
asleep=asleep[time.argsort()]
awake=awake[time.argsort()]
time.sort()

guard=0
sleeptime=np.zeros(int(max(guard_id)+1))
timeasleep=0
seenmins=np.zeros([60, int(max(guard_id)+1)])
lastasleep=0


for i in range(len(lines)):
    if guard_id[i] != 0:
        if lastasleep==1:
            timeasleep += time[i]- 1 - sleepstart
            minend=int(minute[i]-1)
            seenmins[minstart:minend, guardname] += 1
        guardname=int(guard_id[i])
        timeasleep=0
    if awake[i]==1:
        sleeptime[guardname] +=time[i]-1-sleepstart
        minend=int(minute[i]-1)
        seenmins[minstart:minend, guardname] += 1
        lastasleep=0
    if asleep[i]==1:
        sleepstart=time[i]
        minstart=int(minute[i])
        lastasleep=1


print(sleeptime.argmax())
print(seenmins[:,sleeptime.argmax()].argmax())