-
Notifications
You must be signed in to change notification settings - Fork 0
/
day05.py
65 lines (58 loc) · 1.99 KB
/
day05.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
import os
from time import perf_counter
def profiler(method):
def profiler_method(*arg, **kw):
t = perf_counter()
ret = method(*arg, **kw)
print(f'{method.__name__} method took : {perf_counter()-t:.4f} sec')
return ret
return profiler_method
# Part 1:
def part1(rules = None, lists = None) -> int:
total = 0
for i, l in enumerate(lists):
invalid = False
for index, page in enumerate(l):
if page not in rules:
continue
invalid = len(set(l[:index]) & set(rules[page])) != 0
if invalid:
break
lists[i] = (l, invalid)
if not invalid:
total += l[int((len(l)-1)/2)]
return total
# Part 2:
def part2(rules = None, lists = None) -> int:
total = 0
for l, invalid in lists:
if invalid:
sorted_list = [l.pop(0)]
for page in l:
for i, page2 in enumerate(sorted_list):
if page2 in rules[page]:
sorted_list.insert(i, page)
break
else:
sorted_list.append(page)
total += sorted_list[int((len(sorted_list)-1)/2)]
return total
def get_input():
with open(os.path.dirname(os.path.realpath(__file__))+'/input', 'r', encoding='utf-8') as f:
rules, lists = (s for s in f.read().rstrip().split('\n\n'))
rules = [s.strip() for s in rules.split('\n')]
rules = [(int(i), int(j)) for i, j in map(lambda r: r.split('|'), rules)]
rule_map = {k: set() for k, _ in rules}
for one, two in rules:
rule_map[one].add(two)
lists = [s.strip() for s in lists.split('\n')]
lists = [[int(s) for s in l.split(',')] for l in lists]
return rule_map, lists
@profiler
def solve():
rules, lists = get_input()
print(f'Part 1: {part1(rules, lists)}')
print(f'Part 2: {part2(rules, lists)}')
if __name__ == "__main__":
solve()