Advertisement
Guest User

AdventCodeDay4.2

a guest
Dec 4th, 2018
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.94 KB | None | 0 0
  1. class Roster(object):
  2.     def __init__(self):
  3.         self.guards = []
  4.  
  5.     def update_time(self, time):
  6.         for x in self.guards:
  7.             x.update(time)
  8.  
  9.     def change_state(self, id):
  10.         for x in self.guards:
  11.             if x.id == id:
  12.                 x.change_state()
  13.  
  14.     def print_all_guards(self):
  15.         for x in self.guards:
  16.             print(x.id)
  17.  
  18.  
  19. class Guard(object):
  20.     def __init__(self, id):
  21.         self.id = id
  22.         self.minutes_awake = 0
  23.         self.minutes_asleep = 0
  24.         self.awake = True
  25.         self.sleep_log = {}
  26.  
  27.     def update(self, time):
  28.         if self.awake:
  29.             self.minutes_awake += 1
  30.         else:
  31.             self.minutes_asleep += 1
  32.             if time.minute not in  self.sleep_log:
  33.                 self.sleep_log[time.minute] = 1
  34.             else:
  35.                 self.sleep_log[time.minute] += 1
  36.  
  37.     def change_state(self):
  38.         self.awake = not self.awake
  39.  
  40.  
  41. class TimeStamp(object):
  42.     def __init__(self, year, month, day, hour, minute):
  43.         self.year = int(year)
  44.         self.month = int(month)
  45.         self.day = int(day)
  46.         self.hour = int(hour)
  47.         self.minute = int(minute)
  48.  
  49.     def display_time(self):
  50.         print(self.year, self.month, self.day, self.hour, self.minute)
  51.  
  52.     def increment_time_by_minute(self):
  53.         self.minute += 1
  54.         if self.minute == 60:
  55.             self.minute = 0
  56.             self.hour += 1
  57.             if self.hour > 23:
  58.                 self.hour = 0
  59.                 self.day += 1
  60.                 if self.day == 32:
  61.                     self.day = 1
  62.                     self.month += 1
  63.                     if self.month == 13:
  64.                         self.month = 1
  65.                         self.year += 1
  66.  
  67.  
  68. def compare_times(a, b):
  69.     if a.year != b.year:
  70.         return False
  71.     if a.month != b.month:
  72.         return False
  73.     if a.day != b.day:
  74.         return False
  75.     if a.hour != b.hour:
  76.         return False
  77.     if a.minute != b.minute:
  78.         return False
  79.     return True
  80.  
  81.  
  82. with open("input.txt") as f:
  83.     schedule = []
  84.     for line in f:
  85.         time = line[:18]
  86.         action = line[19:].rstrip()
  87.         date, time = time.split()
  88.         date = date[1:]
  89.         time = time[:-1]
  90.         year, month, day = date.split("-")
  91.         hour, minute = time.split(":")
  92.         schedule.append((TimeStamp(year, month, day, hour, minute), action))
  93.  
  94. guard_minutes = {}
  95. roster = Roster()
  96. # get numbers of all guards and build their objects
  97. all_guards = []
  98. for item in schedule:
  99.     if 'Guard' in item[1]:
  100.         g_num = item[1].split()[1]
  101.         if g_num not in all_guards:
  102.             all_guards.append(g_num)
  103.  
  104. # build guard objects
  105. for guard in all_guards:
  106.     roster.guards.append(Guard(guard))
  107.  
  108. # organize schedule
  109. schedule.sort(key=lambda x: (x[0].year, x[0].month, x[0].day, x[0].hour, x[0].minute))
  110.  
  111. # get starting time
  112. time = schedule[0][0]
  113.  
  114. last_guard_id = "-9999"
  115. while len(schedule) > 0:
  116.     # ----- main time loop -------------
  117.  
  118.     # check current time against next time event
  119.     # if the same then pop it off
  120.     if compare_times(time, schedule[0][0]):
  121.         # pop off the array
  122.         t = schedule.pop(0)
  123.         # check kind of event
  124.         if "Guard" in t[1]:
  125.             # guard event update last guard
  126.             last_guard_id = t[1].split()[1]
  127.  
  128.         # change the state of the last guard seen
  129.         else:
  130.             roster.change_state(last_guard_id)
  131.  
  132.     # update the guards
  133.     roster.update_time(time)
  134.  
  135.     # increment time
  136.     time.increment_time_by_minute()
  137.  
  138.  
  139. # results maybe?
  140. max_time_on_a_minute = 0
  141. minute_id = 0
  142. guard_min = 0
  143. for x in roster.guards:
  144.     for item in x.sleep_log.items():
  145.         if item[1] > max_time_on_a_minute:
  146.             max_time_on_a_minute = item[1]
  147.             minute_id = item[0]
  148.             guard_min = x.id
  149.  
  150. print(minute_id, guard_min)
  151. ans = minute_id * int(guard_min[1:])
  152. print(ans)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement