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/RainVector Dec 04 '18

python3

import re
from operator import add
file = open("day4.txt","r")

record = dict()
for line in file:
    (time,content) = [t(s) for t,s in zip((str,str), re.search('^(\[.+\] )(.+)$',str(line)).groups())]
    if(time in record):
        print("It's not right list")
    else:
        record[time] = content
file.close()

# ็ฌฌไธ€ๆญฅ๏ผš่Žทๅ–ๆŽ’ๅบไน‹ๅŽ็š„่ฎฐๅฝ•
sortedRecord = list()
outFile = open("test.txt","w")
for key in sorted(record):
    sortedRecord.append((key,record[key]))
    outFile.write(key + record[key] + '\n')
    
outFile.close()
guards = dict()
guardID = str(" ")

for rec in sortedRecord:
    time = rec[0]
    state = rec[1]
    if(state[0] is 'G'):
        guardID = re.search('^Guard.#([\d.]+) begins shift',str(state)).groups()
        if(guardID not in guards):
            onehourList = [0]*60
            guards[guardID]  = onehourList
    elif(state[0] is 'f'):
        timeBeginSleep = int(re.search('^\[\d{4}-\d{2}-\d{2} \d{2}:(\d{2})\].$',str(time)).groups()[0])
        for i in range(timeBeginSleep,60):
            guards[guardID][i]  += 1
    elif(state[0] is 'w'):
        timeBeginAwake= int(re.search('^\[\d{4}-\d{2}-\d{2} \d{2}:(\d{2})\].$',str(time)).groups()[0])
        for i in range(timeBeginAwake,60):
            guards[guardID][i]  -= 1

# part 1
mostlazyGruadID = -1        
mostSleepTime = 0
mostSuitHour = -1
for guard in guards:
    sleeptime = sum(guards[guard])
    if mostSleepTime < sleeptime:
        mostSleepTime  = sleeptime
        # ่Žทๅ–ๆœ€ๆ‡’guard็š„ID
        mostlazyGruadID = int(guard[0])
        # ่Žทๅ–้‚ฃไธช้‡ๅˆๆœ€ๅคš
        mostSuitHour = guards[guard].index(max(guards[guard]))
        
print(int(mostlazyGruadID) * int(mostSuitHour))




# part 2
"""
ๆ‰พๅˆฐ้ข‘็އๆœ€้ซ˜็š„้‚ฃไธชIDๅ’Œๆฌกๆ•ฐ
"""
mostmost = 0
output = 0
for guard in guards:
    index = guards[guard].index(max(guards[guard]))
    lazyguard = int(guard[0])
    result = index * lazyguard
    if mostmost < guards[guard][index]:
        output = result
        mostmost = guards[guard][index]
        
        
print(output)