-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay4.py
More file actions
64 lines (48 loc) · 1.77 KB
/
Copy pathDay4.py
File metadata and controls
64 lines (48 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import collections
import sys
import re
class Guard:
def __init__(self):
self.minutes_asleep = 0
self.counter = collections.Counter()
def add_sleep(self, start, end):
self.minutes_asleep += end - start
self.counter.update(range(start, end))
# read input
with open(sys.argv[1]) as file:
puzzle_input = file.read().split("\n")
# input.txt is the sorted version of the puzzle input, so no sorting is necessary here
guards = collections.defaultdict(Guard)
guard_id = None
start = None
for line in puzzle_input:
minute = int(re.search(r"\[1518-\d+-\d+ \d+:(\d+)\]", line).groups()[0])
if "begins shift" in line:
guard_id_match = re.search(r"Guard #(\d+) begins shift", line)
guard_id = int(guard_id_match.groups()[0])
elif "falls asleep" in line:
start = minute
elif "wakes up" in line:
guards[guard_id].add_sleep(start, minute)
def part_1(): # sleepiest guard -> sleepiest minute
sleepiest_id = None
max_minutes_asleep = 0
for guard_id in guards:
if guards[guard_id].minutes_asleep > max_minutes_asleep:
sleepiest_id = guard_id
max_minutes_asleep = guards[guard_id].minutes_asleep
sleepiest_minute = guards[sleepiest_id].counter.most_common(1)[0][0]
print(f"Part 1: {sleepiest_id} * {sleepiest_minute} = {sleepiest_id * sleepiest_minute}")
def part_2(): # sleepiest minute overall
guard_id = None
minute = None
count = 0
for curr_id in guards:
curr_minute, curr_count = guards[curr_id].counter.most_common(1)[0]
if curr_count > count:
guard_id = curr_id
minute = curr_minute
count = curr_count
print(f"Part 2: {guard_id} * {minute} = {guard_id * minute}")
part_1()
part_2()