Refactoring problem 4
This commit is contained in:
parent
760dfa1746
commit
94fbfb0f1a
@ -106,21 +106,27 @@ def part_1() -> None:
|
|||||||
for i in range(starting_time, ending_time):
|
for i in range(starting_time, ending_time):
|
||||||
schedule_dict[date, guard_no].append(i)
|
schedule_dict[date, guard_no].append(i)
|
||||||
|
|
||||||
list_dict = defaultdict(list)
|
# flatten lists of values for each key
|
||||||
|
flat_dict = defaultdict(list)
|
||||||
for k, v in schedule_dict.items():
|
for k, v in schedule_dict.items():
|
||||||
for val in v:
|
for val in v:
|
||||||
list_dict[k[1]].append(val)
|
flat_dict[k[1]].append(val)
|
||||||
|
|
||||||
max_dict = defaultdict(list)
|
# get most common minute for each guard
|
||||||
longest_dict = defaultdict(int)
|
most_common_dict = defaultdict(list)
|
||||||
for k, v in list_dict.items():
|
# get number of minutes each guard is sleeping
|
||||||
max_dict[k].append(Counter(v).most_common()[0])
|
sleep_length_dict = defaultdict(int)
|
||||||
longest_dict[k] += len(v)
|
for k, v in flat_dict.items():
|
||||||
|
most_common_dict[k].append(Counter(v).most_common()[0])
|
||||||
|
sleep_length_dict[k] += len(v)
|
||||||
|
|
||||||
max_key = max(longest_dict, key=longest_dict.get)
|
max_sleep_length = max(sleep_length_dict, key=sleep_length_dict.get)
|
||||||
max_min = int(max_dict[max_key][0][0])
|
# get the most common minute for the guard who sleep the most
|
||||||
|
most_common_min = most_common_dict[max_sleep_length][0][0]
|
||||||
|
|
||||||
print(int(max_key) * max_min)
|
print(
|
||||||
|
f"The result we are looking for is {int(max_sleep_length) * int(most_common_min)}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
x
Reference in New Issue
Block a user