-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Rob Grant <rob.grant@nanoporetech.com>
- Loading branch information
1 parent
c3227da
commit caf5ce2
Showing
1 changed file
with
30 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |