This repository was archived by the owner on Dec 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paths.py
More file actions
86 lines (77 loc) · 1.96 KB
/
s.py
File metadata and controls
86 lines (77 loc) · 1.96 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Youtube: https://youtu.be/xnivJsoyMqs
import argparse, math, sys, re, functools, operator, itertools, heapq
from collections import defaultdict, Counter, deque
#sys.setrecursionlimit(100000000)
#A = list(map(int, input().split()))
#T = int(input())
def read_lines(f):
while True:
line = f.readline()
if not line:
break
assert line[-1] == '\n'
yield line[:-1]
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-1', '--one', action='store_true', help='Only part 1')
parser.add_argument('-2', '--two', action='store_true', help='Only part 2')
parser.add_argument('input_file', nargs='?')
args = parser.parse_args()
if args.input_file is not None:
f = open(args.input_file)
else:
f = sys.stdin
lines = list(read_lines(f))
if not args.two:
print(part_1(lines))
if not args.one:
print(part_2(lines))
def check_update1(rules, update):
for index, i in enumerate(update):
for j in update[:index]:
if (i, j) in rules:
return False
return True
def part_1(lines):
s = 0
x = lines.index('')
rules = set()
for i in lines[:x]:
rules.add(tuple(map(int, re.fullmatch(r'(\d+)\|(\d+)', i).groups())))
updates = []
for i in lines[x + 1:]:
updates.append(list(map(int, i.split(','))))
for update in updates:
assert len(update) % 2 == 1
if check_update1(rules, update):
s += update[(len(update) - 1) // 2]
return s
def fix_order(rules, update):
fix = []
while update:
x = update[0]
for i in update:
if (i, x) in rules:
x = i
update.remove(x)
fix.append(x)
return fix
def part_2(lines):
s = 0
x = lines.index('')
rules = set()
for i in lines[:x]:
rules.add(tuple(map(int, re.fullmatch(r'(\d+)\|(\d+)', i).groups())))
updates = []
for i in lines[x + 1:]:
updates.append(list(map(int, i.split(','))))
for update in updates:
assert len(update) % 2 == 1
if check_update1(rules, update):
pass
else:
fix = fix_order(rules, update)
s += fix[(len(fix) - 1) // 2]
return s
if __name__ == '__main__':
main()