MCPcopy Create free account
hub / github.com/AxlLind/AdventOfCode / main

Function main

2018/src/04.py:6–23  ·  view source on GitHub ↗
(indata: str)

Source from the content-addressed store, hash-verified

4
5@aoc.main('04')
6def main(indata: str) -> list[int]:
7 actions = []
8 for l in indata.split('\n'):
9 xs = re.findall("\[1518-(\d+)-(\d+) (\d+):(\d+)\] (.+)", l)[0]
10 month, day, hour, minute = [int(i) for i in xs[:4]]
11 actions.append([month, day, hour, minute, xs[4].split(' ')[1]])
12 asleep, guard, slept = 0, -1, defaultdict[int, Counter[int]](Counter)
13 for _, _, _, minute, action in sorted(actions):
14 match action:
15 case 'up':
16 slept[guard].update(range(asleep, minute))
17 case 'asleep':
18 asleep = minute
19 case _:
20 guard = int(action[1:])
21 g1 = max(slept, key=lambda g: sum(slept[g].values()))
22 g2 = max(slept, key=lambda g: slept[g].most_common(1)[0][1])
23 return [g * slept[g].most_common(1)[0][0] for g in [g1,g2]]
24
25if __name__ == "__main__":
26 main()

Callers 1

04.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected