advent-of-code/2022/python/day13.py

130 lines
3.9 KiB
Python
Raw Normal View History

2022-12-13 05:47:25 +00:00
import matrix
import shared
import itertools
2022-12-13 06:33:34 +00:00
import sys
import os
def blockPrint():
sys.stdout = open(os.devnull, 'w')
# Restore
def enablePrint():
sys.stdout = sys.__stdout__
2022-12-13 05:47:25 +00:00
class BREAK(Exception):
pass
2022-12-13 06:33:34 +00:00
2022-12-13 05:47:25 +00:00
def part1(rows):
groups = [list(v) for k, v in itertools.groupby(rows, key=lambda x: not x) if not k]
2022-12-13 06:33:34 +00:00
NEED = list(range(len(groups)))
indexes = []
2022-12-13 05:47:25 +00:00
for idx, (left, right) in enumerate(groups):
left = eval(left)
right = eval(right)
2022-12-13 06:33:34 +00:00
if idx not in NEED:
blockPrint()
print(f"== Pair {idx+1} ==")
passes = check_group(left, right, idx, 0)
if idx not in NEED:
enablePrint()
2022-12-13 05:47:25 +00:00
if passes is True:
2022-12-13 06:33:34 +00:00
print(f"{idx+1} {matrix.colors.GREEN}{passes}{matrix.colors.ENDC}")
indexes.append(idx+1)
2022-12-13 05:47:25 +00:00
elif passes is False:
2022-12-13 06:33:34 +00:00
print(f"{idx+1} {matrix.colors.RED}{passes}{matrix.colors.ENDC}")
2022-12-13 05:47:25 +00:00
else:
2022-12-13 06:33:34 +00:00
print(f"{idx+1} {matrix.colors.BLUE}{passes}{matrix.colors.ENDC}")
print('-'*40)
print(sum(indexes))
2022-12-13 05:47:25 +00:00
2022-12-13 06:33:34 +00:00
spaces = lambda x: " " * x
def check_group(left, right, group, depth=0):
print(f"{spaces(depth)}- Compare {left} vs {right}, depth {depth}")
2022-12-13 05:47:25 +00:00
for idx, _ in enumerate(left):
2022-12-13 06:33:34 +00:00
# print("starting for")
2022-12-13 05:47:25 +00:00
try:
while True:
2022-12-13 06:33:34 +00:00
l = left[idx]
2022-12-13 05:47:25 +00:00
try:
2022-12-13 06:33:34 +00:00
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")
2022-12-13 05:47:25 +00:00
raise BREAK()
2022-12-13 06:33:34 +00:00
if l > r:
print(f"{spaces(depth)} - Right is Smaller, so inputs IN WRONG ORDER, False")
2022-12-13 05:47:25 +00:00
return False
2022-12-13 06:33:34 +00:00
if l < r:
print(f"{spaces(depth)} - Left is Smaller, so inputs IN RIGHT ORDER, True")
2022-12-13 05:47:25 +00:00
return True
except IndexError:
print("INDEX ERROR RIGHT RAN OUT")
return False
try:
2022-12-13 06:33:34 +00:00
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
2022-12-13 05:47:25 +00:00
except IndexError:
print("INDEX ERROR LEFT RAN OUT")
return True
2022-12-13 06:33:34 +00:00
#print(f"TEST: {repr(l)} vs {repr(r)}")
2022-12-13 05:47:25 +00:00
2022-12-13 06:33:34 +00:00
if isinstance(l, int) and isinstance(r, list):
left[idx] = [ l, ]
2022-12-13 05:47:25 +00:00
print("convert left to list")
continue
2022-12-13 06:33:34 +00:00
if isinstance(l, list) and isinstance(r, int):
right[idx] = [ r, ]
print(f"convert right {r} to list")
2022-12-13 05:47:25 +00:00
continue
except BREAK:
2022-12-13 06:33:34 +00:00
#print("......broke out of while")
continue
else:
print("Left side ran out of items so inputs are in the right order")
return True
2022-12-13 05:47:25 +00:00
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()