2024 Dec 05th
This commit is contained in:
parent
5ef91396b4
commit
d65743eeb2
1 changed files with 47 additions and 0 deletions
47
Python/2024/05/main.py
Normal file
47
Python/2024/05/main.py
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
input = open("input", 'r').read().split("\n\n")
|
||||
|
||||
# d = [line for line in input]
|
||||
rl = [line.split("|") for line in input[0].split("\n")]
|
||||
d = [[int(v) for v in line.split(",") if v != ""] for line in input[1].split("\n") if line != ""]
|
||||
|
||||
r = {}
|
||||
|
||||
for rule in rl:
|
||||
f,s = rule
|
||||
f,s = int(f),int(s)
|
||||
if s in r:
|
||||
r[s].add(f)
|
||||
else:
|
||||
r[s] = {f}
|
||||
|
||||
def run(p2=False):
|
||||
sm = 0
|
||||
for p in d:
|
||||
sp = set(p)
|
||||
seen = set()
|
||||
if p2:
|
||||
while len(seen) != len(sp):
|
||||
seen = set()
|
||||
for i, v in enumerate(p):
|
||||
reqs = sp & (r[v] if v in r else set())
|
||||
if reqs > seen:
|
||||
ind = max([p.index(j) for j in reqs])
|
||||
p[i], p[ind] = p[ind], p[i]
|
||||
break
|
||||
seen.add(v)
|
||||
sm += p[(len(p)//2)]
|
||||
else:
|
||||
for v in p:
|
||||
reqs = sp & (r[v] if v in r else set())
|
||||
if reqs > seen:
|
||||
break
|
||||
seen.add(v)
|
||||
if len(seen) == len(sp):
|
||||
sm += p[(len(p)//2)]
|
||||
return sm
|
||||
|
||||
|
||||
p1 = run()
|
||||
p2 = run(True)
|
||||
print(p1)
|
||||
print(p2 - p1)
|
||||
Loading…
Add table
Add a link
Reference in a new issue