adventofcode

https://adventofcode.com/
Log | Files | Refs

puzzle4.py (1161B)


      1 file = sorted(open('files/puzzle4.txt').readlines())
      2 guards = {}
      3 current_guard = ""
      4 start = 0
      5 
      6 for line in file:
      7     if '#' in line:
      8         current_guard = line.split('#')[1].split(' ')[0]
      9         if current_guard not in guards:
     10             guards[current_guard] = [0 for idx in range(60)]
     11     else:
     12         seconds = int(line.split(']')[0].split(':')[1])
     13         if "falls asleep" in line:
     14             start = seconds
     15         else:
     16             for second in range(start, seconds):
     17                 guards[current_guard][second] += 1
     18 
     19 sleepiest_guard = ""
     20 current_total = 0
     21 most_predictable_guard = ""
     22 current_frequency = 0
     23 
     24 for guard, seconds in guards.items():
     25     total = sum(seconds)
     26     frequency = max(seconds)
     27 
     28     if total > current_total:
     29         current_total = total
     30         sleepiest_guard = guard
     31 
     32     if frequency > current_frequency:
     33         current_frequency = frequency
     34         most_predictable_guard = guard
     35 
     36 print int(sleepiest_guard) * guards[sleepiest_guard].index(max(guards[sleepiest_guard]))
     37 print int(most_predictable_guard) * guards[most_predictable_guard].index(max(guards[most_predictable_guard]))