From c0521b1fd55039e19bb839e246a60ad80af0c57a Mon Sep 17 00:00:00 2001 From: Frederik Baerentsen Date: Sat, 16 Dec 2023 23:19:28 -0500 Subject: [PATCH] Finished 2023-12-16 p2 --- 2023/day16/part2.py | 201 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 2023/day16/part2.py diff --git a/2023/day16/part2.py b/2023/day16/part2.py new file mode 100644 index 0000000..72ef9e2 --- /dev/null +++ b/2023/day16/part2.py @@ -0,0 +1,201 @@ +import sys +import os +from pprint import pprint +import time +import math + +import functools + +#colors +from termcolor import colored + +sys.setrecursionlimit(5000) + +start_time = time.time() + +grid = [] + +def p(x,*args): + for idx,i in enumerate(x): + for jdx,j in enumerate(i): + if j == '#': + print(colored(j,'red'),end='') + elif len(args) > 0: + if (idx,jdx) == args[1]: + print(colored(j,'red'),end='') + elif (idx,jdx) in args[0]: + print(colored(j,'green'),end='') + else: + print(j,end='') + else: + print(j,end='') + print() + +input_f = '' +if len(sys.argv) == 1: + input_f = 'test' +else: + input_f = sys.argv[1] + +with open(input_f) as file: + for line in file: + grid.append(list(line.rstrip())) + +#@functools.cache +def travel(grid,l,direction,visited,seen): + current = (l[0],l[1]) + + try: + if seen[current] == direction: + return + except: + seen[current] = direction + + + if len(sys.argv) == 3: + p(grid,visited,current) + print() + time.sleep(float(sys.argv[2])) + + r = l[0] + c = l[1] + next_loc = () + + #if r >= len(grid) or c >= len(grid[0]) or r < 0 or c < 0: + # return + #else: + #print('Current: ' + str(current) + ' [' + grid[r][c] + ']', end=' ') + #print('Current: ' + str(current) , end=' ') + #print('moving ' + direction + ' to ',end='') + + if direction == 'r': + #print(str((r,c+1))) + if c+1 < len(grid[0]): + next_loc = (r,c+1) + else: + return + #travel(n,c+1,'r') + + if direction == 'l': + #print(str((r,c-1))) + if c-1 >= 0: + next_loc = (r,c-1) + else: + return + #travel(n,c-1,'l') + + if direction == 'd': + #print(str((r+1,c))) + if r+1 < len(grid): + next_loc = (r+1,c) + else: + return + #travel(n+1,c,'d') + + if direction == 'u': + #print(str((r-1,c))) + if r-1 >= 0: + next_loc = (r-1,c) + else: + return + #travel(n-1,c,'u') + + visited.append(next_loc) + + match grid[next_loc[0]][next_loc[1]]: + case '.': + travel(grid,next_loc,direction,visited,seen) + case '|': + if direction == 'l' or direction == 'r': + travel(grid,next_loc,'u',visited,seen) + travel(grid,next_loc,'d',visited,seen) + else: + travel(grid,next_loc,direction,visited,seen) + + case '\\': + if direction == 'r': + travel(grid,next_loc,'d',visited,seen) + if direction == 'l': + travel(grid,next_loc,'u',visited,seen) + if direction == 'd': + travel(grid,next_loc,'r',visited,seen) + if direction == 'u': + travel(grid,next_loc,'l',visited,seen) + + case '-': + if direction == 'u' or direction == 'd': + travel(grid,next_loc,'r',visited,seen) + travel(grid,next_loc,'l',visited,seen) + else: + travel(grid,next_loc,direction,visited,seen) + + case '/': + if direction == 'r': + travel(grid,next_loc,'u',visited,seen) + if direction == 'l': + travel(grid,next_loc,'d',visited,seen) + if direction == 'd': + travel(grid,next_loc,'l',visited,seen) + if direction == 'u': + travel(grid,next_loc,'r',visited,seen) + return visited + +def print_visited(x,visited): + for idx,i in enumerate(x): + for jdx,j in enumerate(i): + + if (idx,jdx) in visited: + print(colored('#','red'),end='') + else: + print(j,end='') + + print() + + +starting_points = [] + +longest = 0 +visited = [] + +seen = {} + + +for i in range(len(grid)): + cords = (i,-1) + visited = [] + seen = {} + travel(grid,cords,'r',visited,seen) + if len(set(visited)) > longest: + longest = len(set(visited)) + +for i in range(0,len(grid[0])): + cords = (-1,i) + visited = [] + seen = {} + travel(grid,cords,'d',visited,seen) + if len(set(visited)) > longest: + longest = len(set(visited)) + +for i in range(len(grid)): + cords = (i,len(grid)) + visited = [] + seen = {} + travel(grid,cords,'l',visited,seen) + if len(set(visited)) > longest: + longest = len(set(visited)) + +for i in range(0,len(grid[0])): + cords = (len(grid),i) + visited = [] + seen = {} + travel(grid,cords,'u',visited,seen) + if len(set(visited)) > longest: + longest = len(set(visited)) + +print(longest) +print("--- %s seconds ---" % (time.time() - start_time)) + + + + +