Move python solutions to separate directory
This commit is contained in:
parent
47e97f4533
commit
3903907973
70 changed files with 0 additions and 0 deletions
115
2018/04/solve.py
115
2018/04/solve.py
|
|
@ -1,115 +0,0 @@
|
|||
import re
|
||||
import sys
|
||||
|
||||
# PART 1
|
||||
|
||||
ACTION_RE = r"\[(\d+)-(\d+)-(\d+) (\d+):(\d+)\] (.*)\n"
|
||||
GUARD_RE = r"Guard #(\d+) begins shift"
|
||||
|
||||
def load_actions(filename):
|
||||
actions = []
|
||||
with open(filename, "r") as f:
|
||||
for line in f:
|
||||
match = re.fullmatch(ACTION_RE, line)
|
||||
y, m, d, hour, minute, action = match.groups()
|
||||
y, m, d, hour, minute = int(y), int(m), int(d), int(hour), int(minute)
|
||||
actions.append((y, m, d, hour, minute, action))
|
||||
return actions
|
||||
|
||||
def calculate_guards(actions):
|
||||
guards = {}
|
||||
|
||||
guard = None
|
||||
asleep_since = None
|
||||
|
||||
for action in sorted(actions):
|
||||
_, _, _, _, m, text = action
|
||||
|
||||
match = re.fullmatch(GUARD_RE, text)
|
||||
if match:
|
||||
guard = int(match.group(1))
|
||||
|
||||
elif text == "falls asleep":
|
||||
asleep_since = m
|
||||
|
||||
elif text == "wakes up":
|
||||
l = guards.get(guard, [])
|
||||
guards[guard] = l
|
||||
|
||||
l.append((asleep_since, m))
|
||||
|
||||
return guards
|
||||
|
||||
def sleeps_longest(guards):
|
||||
sleepiest_guard = None
|
||||
sleepy_time = -1
|
||||
|
||||
for guard, sleep_times in guards.items():
|
||||
total = 0
|
||||
for start, end in sleep_times:
|
||||
total += end - start
|
||||
|
||||
if total > sleepy_time:
|
||||
sleepiest_guard = guard
|
||||
sleepy_time = total
|
||||
|
||||
return sleepiest_guard
|
||||
|
||||
def sleepiest_minute(times):
|
||||
counter = {}
|
||||
for start, end in times:
|
||||
for m in range(start, end):
|
||||
counter[m] = counter.get(m, 0) + 1
|
||||
|
||||
max_minute = None
|
||||
amount = -1
|
||||
for m, n in sorted(counter.items()):
|
||||
if n > amount:
|
||||
max_minute = m
|
||||
amount = n
|
||||
|
||||
return max_minute
|
||||
|
||||
# PART 2
|
||||
|
||||
def sleep_times(times):
|
||||
minutes = {}
|
||||
for start, end in times:
|
||||
for m in range(start, end):
|
||||
minutes[m] = minutes.get(m, 0) + 1
|
||||
return minutes
|
||||
|
||||
def sleepy_minutes(guards):
|
||||
minutes = {}
|
||||
for guard, times in guards.items():
|
||||
for m, n in sleep_times(times).items():
|
||||
md = minutes.get(m, {})
|
||||
minutes[m] = md
|
||||
md[guard] = n
|
||||
|
||||
sleepy_minute = m
|
||||
sleepy_guard = None
|
||||
sleep_time = -1
|
||||
|
||||
for m, md in minutes.items():
|
||||
for guard, n in md.items():
|
||||
if n > sleep_time:
|
||||
sleepy_minute = m
|
||||
sleepy_guard = guard
|
||||
sleep_time = n
|
||||
|
||||
return sleepy_guard, sleepy_minute
|
||||
|
||||
def main(filename):
|
||||
actions = load_actions(filename)
|
||||
guards = calculate_guards(actions)
|
||||
print(f"Solutions for {filename}")
|
||||
guard = sleeps_longest(guards)
|
||||
minute = sleepiest_minute(guards[guard])
|
||||
print(f"Part 1: {guard * minute} - Guard {guard} slept most on minute {minute}.")
|
||||
guard_2, minute_2 = sleepy_minutes(guards)
|
||||
print(f"Part 2: {guard_2 * minute_2} - Guard {guard_2} slept most on minute {minute_2}.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
for filename in sys.argv[1:]:
|
||||
main(filename)
|
||||
Loading…
Add table
Add a link
Reference in a new issue