Advertisement
tomekjo

AoC_task_4_1

Dec 10th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. import re
  2.  
  3. # loading txt file with data & sorting final list by date & time
  4. textfile = open("input5.txt", "r")
  5. content = textfile.read()
  6. listin = content.split('\n')
  7. listsorted = sorted(listin)
  8.  
  9. # input data to dict of guards where guard id is a key and time of being asleep are values of dict
  10. guards_dict = {}
  11. li = []
  12. for i in listsorted:
  13.     if 'Guard' in i:
  14.         guardID = re.findall('#([0-9]+) ', i)[0]
  15.         if guardID not in guards_dict.keys():
  16.               guards_dict[guardID] = []
  17.     elif 'falls asleep' in i:
  18.         nap_start = int(re.findall(':([0-9]+)]', i)[0])
  19.     elif 'wakes up' in i:
  20.         nap_end = int(re.findall(':([0-9]+)]', i)[0])
  21.         li = guards_dict[guardID]
  22.         li.append((nap_start, nap_end))
  23.         nap_start = 0
  24.         nap_end = 0
  25.         guards_dict[guardID] = li
  26.     else:
  27.         print('sth went wrong')
  28.         break
  29.  
  30. # calculating minutes of being asleep and choosing guard who slept the most
  31. chosen_guard = 0
  32. max_minuts_asleep = 0
  33. for k,v in guards_dict.items():
  34.     print(k,v)
  35.     guard_minuts_asleep = 0
  36.     for i in v:
  37.         guard_minuts_asleep += i[1] - i[0]
  38.     if guard_minuts_asleep > max_minuts_asleep:
  39.         max_minuts_asleep = guard_minuts_asleep
  40.         chosen_guard = k
  41. print('The most sleepy guard is:',chosen_guard,'that slept minutes:', max_minuts_asleep)
  42.  
  43. # finding the most sleepy minute
  44. list_of_minutes = []
  45. for i in range(60):
  46.     list_of_minutes.append(0)
  47.  
  48. for j in guards_dict[chosen_guard]:
  49.     for k in range(j[0], j[1]):
  50.         list_of_minutes[k] += 1
  51.  
  52. most_sleepy_minute = list_of_minutes.index(max(list_of_minutes))
  53. print("The most sleepy minute:", most_sleepy_minute)
  54.  
  55. print("Result of exercise is:", int(chosen_guard) * most_sleepy_minute)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement