1,3,5,8 work

This commit is contained in:
Tyrel Souza 2022-12-13 00:47:25 -05:00
parent a7578c2bc2
commit d8700a280f
3 changed files with 111 additions and 15 deletions

View File

@ -33,7 +33,7 @@ def part1(mx):
graph = build_graph(mx)
path = find_path(graph, start, end)
print(len(path.nodes) -1)
print(len(path.nodes) - 1)
def part2(mx):
@ -58,21 +58,25 @@ def part2(mx):
except algorithm.NoPathError:
# This 'a' is in a puddle of 'c's, so it's not pathable
pass
shortest = min(paths, key= lambda x: x.total_cost)
matrix.apply_to_all(mx, lambda x: chr(x+ord('a'))) # Back to letters, single char prettier
all_visited = []
for p in paths:
all_visited.extend(p.nodes)
all_visited = list(set(all_visited))
matrix.highlight(mx, red=all_visited, blink_green=shortest.nodes)
shortest = min(paths, key=lambda x: x.total_cost)
# matrix.apply_to_all(mx, lambda x: chr(x+ord('a'))) # Back to letters, single char prettier
# all_visited = []
# for p in paths:
# all_visited.extend(p.nodes)
# all_visited = list(set(all_visited))
# matrix.highlight(mx, red=all_visited, blink_green=shortest.nodes)
print(shortest.total_cost)
def main():
mx = matrix.load_matrix_file(shared.get_fname(12), matrix.split_word_to_chr_list)
part1(mx)
with shared.elapsed_timer() as elapsed:
part1(mx)
print(elapsed())
mx = matrix.load_matrix_file(shared.get_fname(12), matrix.split_word_to_chr_list)
part2(mx)
with shared.elapsed_timer() as elapsed:
part2(mx)
print(elapsed())
if __name__ == "__main__":

92
2022/python/day13.py Normal file
View File

@ -0,0 +1,92 @@
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()

View File

@ -237,7 +237,7 @@ def pmx(*matrices, pad=True, space=True):
print("".join([f(x) for x in c]))
def ppmx(*matrices, pad=True, space=True, zero='.'):
def ppmx(*matrices, pad=True, space=True, zero="."):
"""
print a matrix of anything, Falsy values turns to `.` for clarity
"""
@ -268,21 +268,21 @@ def highlight(matrix, red=[], green=[], blue=[], blink_green=[]):
"""
mx = deepcopy(matrix)
for (y, x) in red:
if (y,x) in blue or (y,x) in green or (y,x) in blink_green:
if (y, x) in blue or (y, x) in green or (y, x) in blink_green:
continue
new = f"{colors.RED}{mx[y][x]}{colors.ENDC}"
mx[y][x] = new
for (y, x) in green:
if (y,x) in blue or (y,x) in blink_green:
if (y, x) in blue or (y, x) in blink_green:
continue
new = f"{colors.GREEN}{mx[y][x]}{colors.ENDC}"
mx[y][x] = new
for (y, x) in blue:
if (y,x) in blink_green:
if (y, x) in blink_green:
continue
new = f"{colors.BLUE}{mx[y][x]}{colors.ENDC}"
mx[y][x] = new
for (y, x) in blink_green:
new = f"{colors.BLINK}{colors.GREEN}{mx[y][x]}{colors.ENDC}"
mx[y][x] = new
ppmx(mx, pad=False, space=True, zero='0')
ppmx(mx, pad=False, space=True, zero="0")