Start solving 2018 problems
I have also updated get.py to download the problems as
`d<day>.txt` instead of `i<day>.txt`. That allows me
to get the day input via `__input__.replace('.py', '.txt')`
which is a little more concise. I don't know why
I didn't do this earlier.
This commit is contained in:
67
2018/d4.py
Normal file
67
2018/d4.py
Normal file
@@ -0,0 +1,67 @@
|
||||
from lib import str_to_ints
|
||||
from collections import defaultdict
|
||||
|
||||
|
||||
def part_1(data):
|
||||
shifts = sorted(data.splitlines())
|
||||
guards = defaultdict(list)
|
||||
guard = None
|
||||
for s in shifts:
|
||||
try:
|
||||
_, _, _, h, m, guard = str_to_ints(s)
|
||||
except ValueError:
|
||||
_, _, _, h, m = str_to_ints(s)
|
||||
assert guard is not None
|
||||
guards[guard].append((h, m))
|
||||
|
||||
guard_sleep_times = {}
|
||||
guard_heat_maps = {}
|
||||
for guard, times in guards.items():
|
||||
heat_map = [0 for _ in range(60)]
|
||||
total_time = 0
|
||||
start = None
|
||||
for h, m in times:
|
||||
if start is None:
|
||||
start = (h, m)
|
||||
else:
|
||||
delta = (h - start[0]) * 60 + (m - start[1])
|
||||
total_time += delta
|
||||
for t in range(start[1], m):
|
||||
heat_map[t] += 1
|
||||
start = None
|
||||
guard_sleep_times[guard] = total_time
|
||||
guard_heat_maps[guard] = heat_map
|
||||
|
||||
max_guard, max_time = None, 0
|
||||
for guard, time in guard_sleep_times.items():
|
||||
if time > max_time:
|
||||
max_guard = guard
|
||||
max_time = time
|
||||
|
||||
assert type(max_guard) is int
|
||||
max_minute, max_asleep = 0, 0
|
||||
for minute, asleep in enumerate(guard_heat_maps[max_guard]):
|
||||
if asleep > max_asleep:
|
||||
max_minute = minute
|
||||
max_asleep = asleep
|
||||
print(max_guard * max_minute)
|
||||
|
||||
max_guard, max_minute, max_asleep = 0, 0, 0
|
||||
for guard in guard_sleep_times.keys():
|
||||
for minute, asleep in enumerate(guard_heat_maps[guard]):
|
||||
if asleep > max_asleep:
|
||||
max_minute = minute
|
||||
max_asleep = asleep
|
||||
max_guard = guard
|
||||
print(max_guard * max_minute)
|
||||
|
||||
|
||||
def main():
|
||||
input_file = __file__.replace(".py", ".txt")
|
||||
with open(input_file) as f:
|
||||
data = f.read()
|
||||
part_1(data)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user