Skip to content
advent of code 2018

day 04

repose record

approach

the log lines are sorted by their timestamp string, which puts them in chronological order. a single pass then tracks the guard on duty and, on every "wakes" line, marks the minutes since the previous line (when they fell asleep) in that guard's list of 60 counters.

part 1 picks the guard with the largest total asleep and that guard's most common minute. part 2 picks the guard whose single busiest minute is highest. both return the (guard, minute) pair and leave the multiplication to whoever runs them; nothing is called at the bottom of the file.

the trick

one 60-slot counter per guard answers both questions: part 1 compares sums, part 2 compares maxima.

python
def part1():
	maxid=functools.reduce(lambda x,y:x if sum(count[x])>sum(count[y]) else y,count)
	#return count
	return (maxid,functools.reduce(lambda x,y:x if count[maxid][x]>count[maxid][y] else y,range(len(count[maxid]))))

def part2():
	maxid=functools.reduce(lambda x,y:x if max(count[x])>max(count[y]) else y,count)
	return (maxid,functools.reduce(lambda x,y:x if count[maxid][x]>count[maxid][y] else y,range(len(count[maxid]))))