master
Alfred Melch 6 years ago
parent 08a2687d94
commit 8d721d086f

@ -0,0 +1,101 @@
#!/usr/bin/env python3
import re
from datetime import datetime, timedelta
from pprint import pprint
with open('input.txt', 'r') as f:
notes_raw = [x.strip() for x in f.readlines()]
regex = re.compile(r'\[(.*)\] (.*)')
notes_timestamped = list()
for note in notes_raw:
matches = regex.match(note)
notes_timestamped.append(
(datetime.strptime(matches[1], "%Y-%m-%d %H:%M"), matches[2])
)
notes_timestamped.sort()
# pprint(notes_timestamped)
def mark_map(my_map, mode, start, end):
for i in range(start, end):
my_map[i] = '.' if mode == 'awake' else '#'
class Entry:
def __init__(self, guard_id):
self.guard_id = guard_id
self.notes = list()
def get_date(self):
date = self.notes[0][0]
if date.hour == 23:
date = date + timedelta(days=1)
return date.strftime('%m-%d')
def get_awake_map(self):
mode = 'awake'
awake_map = ['?'] * 60
start = 0 if self.notes[0][0].hour == 23 else self.notes[0][0].minute
for note in self.notes[1:]:
minute = note[0].minute
mark_map(awake_map, mode, start, minute)
start = minute
mode = 'awake' if note[1] == 'wakes up' else 'asleep'
mark_map(awake_map, mode, start, 60)
return awake_map
def __repr__(self):
return '{} #{} {}'.format(self.get_date(), self.guard_id.rjust(4), ''.join(self.get_awake_map()))
regex = re.compile(r'#(\d*)')
days = list()
current_entry = None
for note in notes_timestamped:
action = note[1]
matches = regex.search(action)
if matches:
current_entry = Entry(matches[1])
days.append(current_entry)
current_entry.notes.append(note)
for day in days:
print(day)
# Number of sleep minutes per guard
sleep_count = dict()
for day in days:
guard_id = day.guard_id
if sleep_count.get(guard_id) is None:
sleep_count[guard_id] = 0
sleep_count[guard_id] += day.get_awake_map().count('#')
# pprint(sleep_count)
sleepy_guard = max(sleep_count, key=sleep_count.get)
print('Sleepy guard:', sleepy_guard)
# Number of sleep per minute
minute_count = dict()
for i in range(0, 60):
minute_count[i] = 0
for day in days:
if day.guard_id == sleepy_guard:
awake_map = day.get_awake_map()
for i in range(0, 60):
if awake_map[i] == '#':
minute_count[i] += 1
# pprint(minute_count)
sleepy_minute = max(minute_count, key=minute_count.get)
print('Sleepy Minute:', sleepy_minute)
res = int(sleepy_guard) * sleepy_minute
print(f'{sleepy_guard} * {sleepy_minute} = {res}')

@ -0,0 +1,95 @@
#!/usr/bin/env python3
import re
from datetime import datetime, timedelta
from pprint import pprint
with open('input.txt', 'r') as f:
notes_raw = [x.strip() for x in f.readlines()]
regex = re.compile(r'\[(.*)\] (.*)')
notes_timestamped = list()
for note in notes_raw:
matches = regex.match(note)
notes_timestamped.append(
(datetime.strptime(matches[1], "%Y-%m-%d %H:%M"), matches[2])
)
notes_timestamped.sort()
# pprint(notes_timestamped)
def mark_map(my_map, mode, start, end):
for i in range(start, end):
my_map[i] = '.' if mode == 'awake' else '#'
class Entry:
def __init__(self, guard_id):
self.guard_id = guard_id
self.notes = list()
self._awake_map = None
def get_date(self):
date = self.notes[0][0]
if date.hour == 23:
date = date + timedelta(days=1)
return date.strftime('%m-%d')
def get_awake_map(self):
if self._awake_map is not None:
return self._awake_map
mode = 'awake'
awake_map = ['?'] * 60
start = 0 if self.notes[0][0].hour == 23 else self.notes[0][0].minute
for note in self.notes[1:]:
minute = note[0].minute
mark_map(awake_map, mode, start, minute)
start = minute
mode = 'awake' if note[1] == 'wakes up' else 'asleep'
mark_map(awake_map, mode, start, 60)
self._awake_map = awake_map
return awake_map
def __repr__(self):
return '{} #{} {}'.format(self.get_date(), self.guard_id.rjust(4), ''.join(self.get_awake_map()))
regex = re.compile(r'#(\d*)')
days = list()
current_entry = None
for note in notes_timestamped:
action = note[1]
matches = regex.search(action)
if matches:
current_entry = Entry(matches[1])
days.append(current_entry)
current_entry.notes.append(note)
for day in days:
print(day)
# Number of sleep per guard per minute
sleep_count = dict()
for day in days:
awake_map = day.get_awake_map()
for i in range(0, 60):
idx = f'{day.guard_id}-{i}'
if sleep_count.get(idx) is None:
sleep_count[idx] = 0
if awake_map[i] == '#':
sleep_count[idx] += 1
# pprint(sleep_count)
sleepy_idx = max(sleep_count, key=sleep_count.get)
sleepy_guard, sleepy_minute = sleepy_idx.split('-')
print('Sleepy Guard:', sleepy_guard)
print('Sleepy Minute:', sleepy_minute)
res = int(sleepy_guard) * int(sleepy_minute)
print(f'{sleepy_guard} * {sleepy_minute} = {res}')

@ -0,0 +1,68 @@
--- Day 4: Repose Record ---
You've sneaked into another supply closet - this time, it's across from the prototype suit manufacturing lab. You need to sneak inside and fix the issues with the suit, but there's a guard stationed outside the lab, so this is as close as you can safely get.
As you search the closet for anything that might help, you discover that you're not the first person to want to sneak in. Covering the walls, someone has spent an hour starting every midnight for the past few months secretly observing this guard post! They've been writing down the ID of the one guard on duty that night - the Elves seem to have decided that one guard was enough for the overnight shift - as well as when they fall asleep or wake up while at their post (your puzzle input).
For example, consider the following records, which have already been organized into chronological order:
[1518-11-01 00:00] Guard #10 begins shift
[1518-11-01 00:05] falls asleep
[1518-11-01 00:25] wakes up
[1518-11-01 00:30] falls asleep
[1518-11-01 00:55] wakes up
[1518-11-01 23:58] Guard #99 begins shift
[1518-11-02 00:40] falls asleep
[1518-11-02 00:50] wakes up
[1518-11-03 00:05] Guard #10 begins shift
[1518-11-03 00:24] falls asleep
[1518-11-03 00:29] wakes up
[1518-11-04 00:02] Guard #99 begins shift
[1518-11-04 00:36] falls asleep
[1518-11-04 00:46] wakes up
[1518-11-05 00:03] Guard #99 begins shift
[1518-11-05 00:45] falls asleep
[1518-11-05 00:55] wakes up
Timestamps are written using year-month-day hour:minute format. The guard falling asleep or waking up is always the one whose shift most recently started. Because all asleep/awake times are during the midnight hour (00:00 - 00:59), only the minute portion (00 - 59) is relevant for those events.
Visually, these records show that the guards are asleep at these times:
Date ID Minute
000000000011111111112222222222333333333344444444445555555555
012345678901234567890123456789012345678901234567890123456789
11-01 #10 .....####################.....#########################.....
11-02 #99 ........................................##########..........
11-03 #10 ........................#####...............................
11-04 #99 ....................................##########..............
11-05 #99 .............................................##########.....
The columns are Date, which shows the month-day portion of the relevant day; ID, which shows the guard on duty that day; and Minute, which shows the minutes during which the guard was asleep within the midnight hour. (The Minute column's header shows the minute's ten's digit in the first row and the one's digit in the second row.) Awake is shown as ., and asleep is shown as #.
Note that guards count as asleep on the minute they fall asleep, and they count as awake on the minute they wake up. For example, because Guard #10 wakes up at 00:25 on 1518-11-01, minute 25 is marked as awake.
If you can figure out the guard most likely to be asleep at a specific time, you might be able to trick that guard into working tonight so you can have the best chance of sneaking in. You have two strategies for choosing the best guard/minute combination.
Strategy 1: Find the guard that has the most minutes asleep. What minute does that guard spend asleep the most?
In the example above, Guard #10 spent the most minutes asleep, a total of 50 minutes (20+25+5), while Guard #99 only slept for a total of 30 minutes (10+10+10). Guard #10 was asleep most during minute 24 (on two days, whereas any other minute the guard was asleep was only seen on one day).
While this example listed the entries in chronological order, your entries are in the order you found them. You'll need to organize them before they can be analyzed.
What is the ID of the guard you chose multiplied by the minute you chose? (In the above example, the answer would be 10 * 24 = 240.)
Your puzzle answer was 36898.
--- Part Two ---
Strategy 2: Of all guards, which guard is most frequently asleep on the same minute?
In the example above, Guard #99 spent minute 45 asleep more than any other guard or minute - three times in total. (In all other cases, any guard spent any minute asleep at most twice.)
What is the ID of the guard you chose multiplied by the minute you chose? (In the above example, the answer would be 99 * 45 = 4455.)
Your puzzle answer was 80711.
Both parts of this puzzle are complete! They provide two gold stars: **
At this point, you should return to your advent calendar and try another puzzle.
If you still want to see it, you can get your puzzle input.
You can also [Share] this puzzle.

File diff suppressed because it is too large Load Diff

@ -0,0 +1,2 @@
01: 971 * 38 = 36898
02: 1877 * 43 = 80711
Loading…
Cancel
Save