130 lines
3.9 KiB
Python
130 lines
3.9 KiB
Python
import matrix
|
|
import shared
|
|
import itertools
|
|
import sys
|
|
import os
|
|
|
|
|
|
def blockPrint():
|
|
sys.stdout = open(os.devnull, 'w')
|
|
|
|
|
|
# Restore
|
|
def enablePrint():
|
|
sys.stdout = sys.__stdout__
|
|
|
|
|
|
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]
|
|
NEED = list(range(len(groups)))
|
|
|
|
indexes = []
|
|
for idx, (left, right) in enumerate(groups):
|
|
left = eval(left)
|
|
right = eval(right)
|
|
if idx not in NEED:
|
|
blockPrint()
|
|
print(f"== Pair {idx+1} ==")
|
|
passes = check_group(left, right, idx, 0)
|
|
if idx not in NEED:
|
|
enablePrint()
|
|
if passes is True:
|
|
print(f"{idx+1} {matrix.colors.GREEN}{passes}{matrix.colors.ENDC}")
|
|
indexes.append(idx+1)
|
|
elif passes is False:
|
|
print(f"{idx+1} {matrix.colors.RED}{passes}{matrix.colors.ENDC}")
|
|
else:
|
|
print(f"{idx+1} {matrix.colors.BLUE}{passes}{matrix.colors.ENDC}")
|
|
print('-'*40)
|
|
print(sum(indexes))
|
|
|
|
|
|
spaces = lambda x: " " * x
|
|
|
|
|
|
def check_group(left, right, group, depth=0):
|
|
print(f"{spaces(depth)}- Compare {left} vs {right}, depth {depth}")
|
|
for idx, _ in enumerate(left):
|
|
# print("starting for")
|
|
try:
|
|
while True:
|
|
l = left[idx]
|
|
try:
|
|
r = right[idx]
|
|
except IndexError:
|
|
print(" - Right side ran out of items, so inputs are not in the right order")
|
|
return False
|
|
# print("starting while")
|
|
try:
|
|
if isinstance(l, int) and isinstance(r, int):
|
|
print(f"{spaces(depth)} - Compare {l} vs {r}")
|
|
if l == r:
|
|
#print(".......BREAK")
|
|
raise BREAK()
|
|
if l > r:
|
|
print(f"{spaces(depth)} - Right is Smaller, so inputs IN WRONG ORDER, False")
|
|
return False
|
|
if l < r:
|
|
print(f"{spaces(depth)} - Left is Smaller, so inputs IN RIGHT ORDER, True")
|
|
return True
|
|
except IndexError:
|
|
print("INDEX ERROR RIGHT RAN OUT")
|
|
return False
|
|
|
|
try:
|
|
if isinstance(l, list) and isinstance(r, list):
|
|
print(f"{spaces(depth)} - Both Lists {l} vs {r}")
|
|
check = check_group(l, r, group, depth+1)
|
|
if check is None:
|
|
print("check is none", depth)
|
|
continue
|
|
if check is False:
|
|
"checked list v list and false"
|
|
return False
|
|
break
|
|
|
|
except IndexError:
|
|
print("INDEX ERROR LEFT RAN OUT")
|
|
return True
|
|
|
|
#print(f"TEST: {repr(l)} vs {repr(r)}")
|
|
|
|
if isinstance(l, int) and isinstance(r, list):
|
|
left[idx] = [ l, ]
|
|
print("convert left to list")
|
|
continue
|
|
if isinstance(l, list) and isinstance(r, int):
|
|
right[idx] = [ r, ]
|
|
print(f"convert right {r} to list")
|
|
continue
|
|
except BREAK:
|
|
#print("......broke out of while")
|
|
continue
|
|
else:
|
|
print("Left side ran out of items so inputs are in the right order")
|
|
return True
|
|
|
|
|
|
|
|
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()
|