days 4 and 5
This commit is contained in:
parent
55c0fa7151
commit
2d046d6f0b
47
2018/04.py
47
2018/04.py
@ -1,4 +1,4 @@
|
|||||||
from collections import defaultdict
|
from collections import defaultdict, Counter
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import re
|
import re
|
||||||
|
|
||||||
@ -14,13 +14,44 @@ with open('data/04.txt') as f:
|
|||||||
events.sort(key=lambda e: e[0])
|
events.sort(key=lambda e: e[0])
|
||||||
|
|
||||||
|
|
||||||
|
spans = []
|
||||||
|
sleep_start = None
|
||||||
|
current_guard_id = None
|
||||||
|
for dt, guard_id, event_type in events:
|
||||||
|
if event_type == 'begins shift':
|
||||||
|
current_guard_id = guard_id
|
||||||
|
elif event_type == 'falls asleep':
|
||||||
|
sleep_start = dt
|
||||||
|
elif event_type == 'wakes up':
|
||||||
|
spans.append((current_guard_id, sleep_start, dt))
|
||||||
|
|
||||||
by_minute = defaultdict(lambda: defaultdict(list))
|
by_minute = defaultdict(lambda: defaultdict(list))
|
||||||
by_guard = defaultdict(lambda: defaultdict(list))
|
by_guard = defaultdict(lambda: defaultdict(list))
|
||||||
total_time_asleep = defaultdict(int)
|
total_time_asleep = Counter()
|
||||||
|
for guard_id, start, end in spans:
|
||||||
|
for i in range((end - start).seconds // 60):
|
||||||
|
minute = start.minute + i
|
||||||
|
by_minute[minute][guard_id].append(start.date())
|
||||||
|
by_guard[guard_id][minute].append(start.date())
|
||||||
|
total_time_asleep[guard_id] += 1
|
||||||
|
|
||||||
current_guard_id = None
|
sleepiest_guard = total_time_asleep.most_common()[0][0]
|
||||||
spans = []
|
sleepiest_minute, dates = sorted(
|
||||||
for event in events:
|
(m for m in by_guard[sleepiest_guard].items()),
|
||||||
dt, guard_id, event_type = event
|
key=lambda x: len(x[1]),
|
||||||
if event_type = 'begins shift':
|
reverse=True
|
||||||
current_guard_id = guard_id
|
)[0]
|
||||||
|
|
||||||
|
print(f'Part 1: {sleepiest_guard * sleepiest_minute}\n')
|
||||||
|
|
||||||
|
|
||||||
|
sleepiest_minute = defaultdict(lambda: (0, 0))
|
||||||
|
for guard_id, minutes in by_guard.items():
|
||||||
|
for minute, dates in minutes.items():
|
||||||
|
times_asleep = len(dates)
|
||||||
|
if times_asleep > sleepiest_minute[minute][1]:
|
||||||
|
sleepiest_minute[minute] = (guard_id, times_asleep)
|
||||||
|
|
||||||
|
minute, (guard_id, num_times_asleep) = sorted(sleepiest_minute.items(), key=lambda x: x[1][1], reverse=True)[0]
|
||||||
|
|
||||||
|
print(f'Part 2: {minute * guard_id}\n')
|
43
2018/05.py
Normal file
43
2018/05.py
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
from collections import Counter
|
||||||
|
|
||||||
|
class Polymer:
|
||||||
|
def __init__(self, data, remove=None):
|
||||||
|
self.units = list(filter(lambda u: u.lower() != remove, data))
|
||||||
|
self.index = 0
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return len(self.units)
|
||||||
|
|
||||||
|
def test(self, i):
|
||||||
|
try:
|
||||||
|
a, b = self.units[i], self.units[i + 1]
|
||||||
|
except IndexError:
|
||||||
|
return False
|
||||||
|
if a != b and a.lower() == b.lower():
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def react(self):
|
||||||
|
while self.test(self.index):
|
||||||
|
del self.units[self.index:self.index + 2]
|
||||||
|
if self.index > 0:
|
||||||
|
self.index -= 1
|
||||||
|
|
||||||
|
def process(self):
|
||||||
|
while self.index < len(self) - 1:
|
||||||
|
self.react()
|
||||||
|
self.index += 1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
with open('data/05.txt') as f:
|
||||||
|
data = f.read().strip()
|
||||||
|
|
||||||
|
|
||||||
|
results = []
|
||||||
|
for unit_type in set(data.lower()):
|
||||||
|
p = Polymer(data, remove=unit_type)
|
||||||
|
p.process()
|
||||||
|
results.append(len(p))
|
||||||
|
|
||||||
|
print(min(results))
|
1
2018/data/05.txt
Normal file
1
2018/data/05.txt
Normal file
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user