day 13 python cleanup

convert our less function into a real cmp function
main
magical 2022-12-13 10:39:36 -08:00
parent 6c175f2d8e
commit 85e623a9db
1 changed files with 7 additions and 14 deletions

View File

@ -4,38 +4,31 @@ data = []
for chunk in open("input").read().split("\n\n"): for chunk in open("input").read().split("\n\n"):
data.append([json.loads(x) for x in chunk.splitlines(False)]) data.append([json.loads(x) for x in chunk.splitlines(False)])
def less(a, b): def cmp(a, b):
if type(a) == int and type(b) == int: if type(a) == int and type(b) == int:
return a < b return (a > b) - (a < b)
if type(a) == list and type(b) == int: if type(a) == list and type(b) == int:
b = [b] b = [b]
elif type(a) == int and type(b) == list: elif type(a) == int and type(b) == list:
a = [a] a = [a]
for x, y in zip(a,b): for x, y in zip(a,b):
if less(x,y): r = cmp(x,y)
return True if r != 0:
elif less(y,x): return r
return False return cmp(len(a),len(b))
return len(a)<len(b)
#print(data) #print(data)
t = 0 t = 0
for i,(a,b) in enumerate(data): for i,(a,b) in enumerate(data):
#print("<" if less(a,b) else ">=") #print("<" if less(a,b) else ">=")
if less(a,b): if cmp(a,b) < 0:
t += i + 1 t += i + 1
print(t) print(t)
import functools import functools
def cmp(a, b):
if less(a,b):
return -1
if less(b,a):
return 1
return 0
big = [e for x in data for e in x] big = [e for x in data for e in x]
big.append([[2]]) big.append([[2]])