Clean day 13.
This commit is contained in:
parent
652756a341
commit
7ac9981ae5
@ -10,37 +10,25 @@ pairs = [tuple(json.loads(p) for p in block.split("\n")) for block in blocks]
|
|||||||
|
|
||||||
|
|
||||||
def compare(lhs: list[int | list], rhs: list[int | list]) -> int:
|
def compare(lhs: list[int | list], rhs: list[int | list]) -> int:
|
||||||
if len(lhs) != len(rhs):
|
|
||||||
m = min(len(lhs), len(rhs))
|
|
||||||
r = compare(lhs[:m], rhs[:m])
|
|
||||||
|
|
||||||
if r != 0:
|
for lhs_a, rhs_a in zip(lhs, rhs):
|
||||||
return r
|
if isinstance(lhs_a, int) and isinstance(rhs_a, int):
|
||||||
|
if lhs_a != rhs_a:
|
||||||
|
return rhs_a - lhs_a
|
||||||
else:
|
else:
|
||||||
return 1 if len(rhs) > len(lhs) else -1
|
if not isinstance(lhs_a, list):
|
||||||
else:
|
lhs_a = [lhs_a]
|
||||||
for lhs_a, rhs_a in zip(lhs, rhs):
|
elif not isinstance(rhs_a, list):
|
||||||
if isinstance(lhs_a, int) and isinstance(rhs_a, int):
|
rhs_a = [rhs_a]
|
||||||
if lhs_a != rhs_a:
|
assert isinstance(rhs_a, list) and isinstance(lhs_a, list)
|
||||||
return 1 if rhs_a > lhs_a else -1
|
r = compare(lhs_a, rhs_a)
|
||||||
elif isinstance(lhs_a, list) and isinstance(rhs_a, list):
|
if r != 0:
|
||||||
r = compare(lhs_a, rhs_a)
|
return r
|
||||||
if r != 0:
|
|
||||||
return r
|
return len(rhs) - len(lhs)
|
||||||
else:
|
|
||||||
if not isinstance(lhs_a, list):
|
|
||||||
lhs_a = [lhs_a]
|
|
||||||
else:
|
|
||||||
assert isinstance(rhs_a, int)
|
|
||||||
rhs_a = [rhs_a]
|
|
||||||
assert isinstance(rhs_a, list)
|
|
||||||
r = compare(lhs_a, rhs_a)
|
|
||||||
if r != 0:
|
|
||||||
return r
|
|
||||||
return 0
|
|
||||||
|
|
||||||
|
|
||||||
answer_1 = sum(i + 1 for i, (lhs, rhs) in enumerate(pairs) if compare(lhs, rhs) == 1)
|
answer_1 = sum(i + 1 for i, (lhs, rhs) in enumerate(pairs) if compare(lhs, rhs) > 0)
|
||||||
print(f"answer_1 is {answer_1}")
|
print(f"answer_1 is {answer_1}")
|
||||||
|
|
||||||
dividers = [[[2]], [[6]]]
|
dividers = [[[2]], [[6]]]
|
||||||
|
Loading…
Reference in New Issue
Block a user