day 13 p2

This commit is contained in:
2022-12-13 19:40:44 +02:00
parent 279e82054e
commit 65a5f421a0

88
13/m.py
View File

@@ -1,64 +1,70 @@
from functools import cmp_to_key
import os
import ast
input = open(os.path.dirname(__file__) +
"/input.txt", "r").read().split('\n\n')
correct = []
def compare(a, b):
compa = a
compb = b
typea = type(a)
typeb = type(b)
print(">> compare", a, b)
res = 0
if typea == int and typeb == int:
print("int comparison", a, b)
if a < b:
res = 1
return -1
elif a == b:
res = 0
return 0
else:
res = -1
return 1
if typea == list and typeb == list:
while res == 0:
if len(a) == 0 and len(b) == 0:
print("both lists are empty, continue")
res = 0
break
elif len(a) == 0:
print("left out, TRUE")
res = 1
break
elif len(b) == 0:
print("right out, FALSE")
res = -1
break
print("res", res, "len a", len(a), "len b", len(b))
res = compare(a.pop(0), b.pop(0))
elif typea == list and typeb == list:
i = 0
while i < len(a) and i < len(b):
res = compare(a[i], b[i])
if res == 1:
return 1
if res == -1:
return -1
i += 1
if (typea == int and typeb == list):
res = compare([a], b)
if (typeb == int and typea == list):
res = compare(a, [b])
if len(a) == i and i < len(b):
return -1
elif len(b) == i and i < len(a):
return 1
else:
return 0
return res
elif (typea == int and typeb == list):
return compare([a], b)
elif (typeb == int and typea == list):
return compare(a, [b])
packets = []
p1 = 0
for idx, i in enumerate(input):
print(">>> IDX:", idx+1)
a, b = i.split('\n')
a = ast.literal_eval(a)
b = ast.literal_eval(b)
print("a:", a, "b:", b)
a = eval(a)
b = eval(b)
packets.append(a)
packets.append(b)
if compare(a, b) == 1:
correct.append(idx+1)
if compare(a, b) == -1:
p1 += idx+1
print(correct)
print(sum(correct))
print("p1:", p1)
packets.append([[2]])
packets.append([[6]])
packets = sorted(packets, key=cmp_to_key(lambda a, b: compare(a, b)))
p2 = 1
for idx, p in enumerate(packets):
# print(p)
if p == [[2]] or p == [[6]]:
p2 *= idx+1
print("p2:", p2)