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 os
import ast
input = open(os.path.dirname(__file__) + input = open(os.path.dirname(__file__) +
"/input.txt", "r").read().split('\n\n') "/input.txt", "r").read().split('\n\n')
correct = []
def compare(a, b): def compare(a, b):
compa = a
compb = b
typea = type(a) typea = type(a)
typeb = type(b) typeb = type(b)
print(">> compare", a, b)
res = 0
if typea == int and typeb == int: if typea == int and typeb == int:
print("int comparison", a, b)
if a < b: if a < b:
res = 1 return -1
elif a == b: elif a == b:
res = 0 return 0
else: else:
res = -1 return 1
if typea == list and typeb == list: elif typea == list and typeb == list:
while res == 0: i = 0
if len(a) == 0 and len(b) == 0: while i < len(a) and i < len(b):
print("both lists are empty, continue") res = compare(a[i], b[i])
res = 0 if res == 1:
break return 1
elif len(a) == 0: if res == -1:
print("left out, TRUE") return -1
res = 1 i += 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))
if (typea == int and typeb == list): if len(a) == i and i < len(b):
res = compare([a], b) return -1
if (typeb == int and typea == list): elif len(b) == i and i < len(a):
res = compare(a, [b]) 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): for idx, i in enumerate(input):
print(">>> IDX:", idx+1)
a, b = i.split('\n') a, b = i.split('\n')
a = ast.literal_eval(a) a = eval(a)
b = ast.literal_eval(b) b = eval(b)
print("a:", a, "b:", b) packets.append(a)
packets.append(b)
if compare(a, b) == 1: if compare(a, b) == -1:
correct.append(idx+1) p1 += idx+1
print(correct) print("p1:", p1)
print(sum(correct))
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)