93 lines
3.1 KiB
Python
93 lines
3.1 KiB
Python
from pprint import pprint as pp
|
|
import matrix
|
|
import shared
|
|
import itertools
|
|
|
|
class BREAK(Exception):
|
|
...
|
|
pass
|
|
|
|
|
|
def part1(rows):
|
|
groups = [list(v) for k, v in itertools.groupby(rows, key=lambda x: not x) if not k]
|
|
for idx, (left, right) in enumerate(groups):
|
|
left = eval(left)
|
|
right = eval(right)
|
|
passes = check_group(idx+1, left, right)
|
|
if passes is True:
|
|
print(f"{matrix.colors.GREEN}{passes}{matrix.colors.ENDC}")
|
|
elif passes is False:
|
|
print(f"{matrix.colors.RED}{passes}{matrix.colors.ENDC}")
|
|
else:
|
|
print(f"{matrix.colors.BLUE}{passes}{matrix.colors.ENDC}")
|
|
print("\n")
|
|
|
|
|
|
def check_group(list_num, left, right):
|
|
if list_num > 0:
|
|
print(f"== Pair {list_num} ==")
|
|
print(f"Compare {left} vs {right}")
|
|
for idx, _ in enumerate(left):
|
|
print("starting for")
|
|
try:
|
|
while True:
|
|
print("starting while")
|
|
try:
|
|
if isinstance(left[idx], int) and isinstance(right[idx], int):
|
|
print(f" - Compare {left[idx]} vs {right[idx]}")
|
|
if left[idx] == right[idx]:
|
|
print(".......BREAK")
|
|
raise BREAK()
|
|
if left[idx] > right[idx]:
|
|
print(" - Right is Smaller, so inputs IN WRONG ORDER")
|
|
print("....RET-FALSE")
|
|
return False
|
|
if left[idx] < right[idx]:
|
|
print(" - Left is Smaller, so inputs IN RIGHT ORDER")
|
|
print("....RET-TRUE")
|
|
return True
|
|
except IndexError:
|
|
print("INDEX ERROR RIGHT RAN OUT")
|
|
return False
|
|
|
|
try:
|
|
if isinstance(left[idx], list) and isinstance(right[idx], list):
|
|
print(f" - Compare {left[idx]} vs {right[idx]}")
|
|
return check_group(-1, left[idx], right[idx])
|
|
except IndexError:
|
|
print("INDEX ERROR LEFT RAN OUT")
|
|
return True
|
|
|
|
#print(f"TEST: {repr(left[idx])} vs {repr(right[idx])}")
|
|
|
|
if isinstance(left[idx], int) and isinstance(right[idx], list):
|
|
left[idx] = [ left[idx], ]
|
|
print("convert left to list")
|
|
continue
|
|
if isinstance(left[idx], list) and isinstance(right[idx], int):
|
|
right[idx] = [ right[idx], ]
|
|
print(f"convert right {right[idx]} to list")
|
|
continue
|
|
except BREAK:
|
|
print("......broke out of while")
|
|
continue
|
|
|
|
|
|
|
|
def part2(rows):
|
|
print(rows)
|
|
|
|
|
|
def main():
|
|
rows = shared.load_rows(13)
|
|
with shared.elapsed_timer() as elapsed:
|
|
part1(rows)
|
|
print(elapsed())
|
|
# with shared.elapsed_timer() as elapsed:
|
|
# part2(rows)
|
|
# print(elapsed())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|