Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Roster(object):
- def __init__(self):
- self.guards = []
- def update_time(self, time):
- for x in self.guards:
- x.update(time)
- def change_state(self, id):
- for x in self.guards:
- if x.id == id:
- x.change_state()
- def print_all_guards(self):
- for x in self.guards:
- print(x.id)
- class Guard(object):
- def __init__(self, id):
- self.id = id
- self.minutes_awake = 0
- self.minutes_asleep = 0
- self.awake = True
- self.sleep_log = {}
- def update(self, time):
- if self.awake:
- self.minutes_awake += 1
- else:
- self.minutes_asleep += 1
- if time.minute not in self.sleep_log:
- self.sleep_log[time.minute] = 1
- else:
- self.sleep_log[time.minute] += 1
- def change_state(self):
- self.awake = not self.awake
- class TimeStamp(object):
- def __init__(self, year, month, day, hour, minute):
- self.year = int(year)
- self.month = int(month)
- self.day = int(day)
- self.hour = int(hour)
- self.minute = int(minute)
- def display_time(self):
- print(self.year, self.month, self.day, self.hour, self.minute)
- def increment_time_by_minute(self):
- self.minute += 1
- if self.minute == 60:
- self.minute = 0
- self.hour += 1
- if self.hour > 23:
- self.hour = 0
- self.day += 1
- if self.day == 32:
- self.day = 1
- self.month += 1
- if self.month == 13:
- self.month = 1
- self.year += 1
- def compare_times(a, b):
- if a.year != b.year:
- return False
- if a.month != b.month:
- return False
- if a.day != b.day:
- return False
- if a.hour != b.hour:
- return False
- if a.minute != b.minute:
- return False
- return True
- with open("input.txt") as f:
- schedule = []
- for line in f:
- time = line[:18]
- action = line[19:].rstrip()
- date, time = time.split()
- date = date[1:]
- time = time[:-1]
- year, month, day = date.split("-")
- hour, minute = time.split(":")
- schedule.append((TimeStamp(year, month, day, hour, minute), action))
- guard_minutes = {}
- roster = Roster()
- # get numbers of all guards and build their objects
- all_guards = []
- for item in schedule:
- if 'Guard' in item[1]:
- g_num = item[1].split()[1]
- if g_num not in all_guards:
- all_guards.append(g_num)
- # build guard objects
- for guard in all_guards:
- roster.guards.append(Guard(guard))
- # organize schedule
- schedule.sort(key=lambda x: (x[0].year, x[0].month, x[0].day, x[0].hour, x[0].minute))
- # get starting time
- time = schedule[0][0]
- last_guard_id = "-9999"
- while len(schedule) > 0:
- # ----- main time loop -------------
- # check current time against next time event
- # if the same then pop it off
- if compare_times(time, schedule[0][0]):
- # pop off the array
- t = schedule.pop(0)
- # check kind of event
- if "Guard" in t[1]:
- # guard event update last guard
- last_guard_id = t[1].split()[1]
- # change the state of the last guard seen
- else:
- roster.change_state(last_guard_id)
- # update the guards
- roster.update_time(time)
- # increment time
- time.increment_time_by_minute()
- # results maybe?
- max_time_on_a_minute = 0
- minute_id = 0
- guard_min = 0
- for x in roster.guards:
- for item in x.sleep_log.items():
- if item[1] > max_time_on_a_minute:
- max_time_on_a_minute = item[1]
- minute_id = item[0]
- guard_min = x.id
- print(minute_id, guard_min)
- ans = minute_id * int(guard_min[1:])
- print(ans)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement