Skip to content

Commit

Permalink
Day13 part 1
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Grant <rob.grant@nanoporetech.com>
  • Loading branch information
robertlagrant committed Dec 13, 2022
1 parent c3227da commit caf5ce2
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions day13/run.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,43 @@
from inputs import REAL as data


def compare(left, right, num):
def compare(left, right, num, depth=0):
print(" "*depth*2 + f"- Compare {left} vs {right}")
match([left, right]):
case [int(), int()]:
print(f"{num} 1: {left, right}")
return left <= right
if left < right:
print(" "*(1+depth)*2 + f"- Left side is smaller, so inputs are in the right order")
return 1
elif right < left:
print(" "*(1+depth)*2 + f"- Right side is smaller, so inputs are not in the right order")
return -1

return 0
case [int(), list()]:
print(f"{num} 2: {[left] * left, right}")
return compare([left] * left, right, num)
print(" "*(1+depth)*2 + f"- Mixed types; convert left to [{left}] and retry comparison")
return compare([left], right, num, depth+1)
case [list(), int()]:
print(f"{num} 3: {left, [right] * right}")
return compare(left, [right] * right, num)
case [list(), list()] if len(left) != len(right):
print(f"{num} 4: {len(left)} {len(right)}")
return len(left) <= len(right)
print(" "*(1+depth)*2 + f"- Mixed types; convert right to [{right}] and retry comparison")
return compare(left, [right], num, depth+1)
case [list(), list()]:
print(f"{num} 5: {left, right}")
return all(compare(l, r, num) for l, r in zip(left, right))
for l, r in zip(left, right):
result = compare(l, r, num, depth+1)
if result != 0:
return result

if len(left) < len(right):
print(" "*(1+depth)*2 + f"- Left side ran out of items, so inputs are in the right order")
return 1
elif len(left) == len(right):
return 0

print(" "*(1+depth)*2 + f"- Right side ran out of items, so inputs are not in the right order")
return -1

for i, (left, right) in enumerate([map(eval, pair.split("\n")) for pair in data.split("\n\n")]):
print(left)
print(right)
print(f"== Pair {i+1} ==")

print(compare(left, right, i))
print()


# print("Part 1:", sum(i + 1 for i, (l, r) in enumerate([map(eval, pair.split('\n')) for pair in data.split('\n\n')]) if compare(l, r, i)))
print("Part 1:", sum(i + 1 for i, (l, r) in enumerate([map(eval, pair.split('\n')) for pair in data.split('\n\n')]) if compare(l, r, i) in (0, 1)))

0 comments on commit caf5ce2

Please sign in to comment.