diff --git a/2023/day16/part1.py b/2023/day16/part1.py new file mode 100644 index 0000000..31e0594 --- /dev/null +++ b/2023/day16/part1.py @@ -0,0 +1,168 @@ +import sys +import os +from pprint import pprint +import time +import math + +#colors +from termcolor import colored + +sys.setrecursionlimit(3000) + +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())) + + +visited = [] +visited.append((0,0)) + +seen = {} + +def travel(grid,l,direction): + global visited + 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('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(grid,r,c+1,'r') + + if direction == 'l': + #print(str((r,c-1))) + if c-1 >= 0: + next_loc = (r,c-1) + else: + return + #travel(grid,r,c-1,'l') + + if direction == 'd': + #print(str((r+1,c))) + if r+1 < len(grid): + next_loc = (r+1,c) + else: + return + #travel(grid,r+1,c,'d') + + if direction == 'u': + #print(str((r-1,c))) + if r-1 >= 0: + next_loc = (r-1,c) + else: + return + #travel(grid,r-1,c,'u') + + visited.append(next_loc) + + match grid[next_loc[0]][next_loc[1]]: + case '.': + travel(grid,next_loc,direction) + case '|': + if direction == 'l' or direction == 'r': + travel(grid,next_loc,'u') + travel(grid,next_loc,'d') + else: + travel(grid,next_loc,direction) + + case '\\': + if direction == 'r': + travel(grid,next_loc,'d') + if direction == 'l': + travel(grid,next_loc,'u') + if direction == 'd': + travel(grid,next_loc,'r') + if direction == 'u': + travel(grid,next_loc,'l') + + case '-': + if direction == 'u' or direction == 'd': + travel(grid,next_loc,'r') + travel(grid,next_loc,'l') + else: + travel(grid,next_loc,direction) + + case '/': + if direction == 'r': + travel(grid,next_loc,'u') + if direction == 'l': + travel(grid,next_loc,'d') + if direction == 'd': + travel(grid,next_loc,'l') + if direction == 'u': + travel(grid,next_loc,'r') + + +travel(grid,(0,-1),'r') + +t_grid = grid + +for i in visited: + t_grid[i[0]][i[1]] = '#' + +print() +p(t_grid) + +count = 0 +for idx,i in enumerate(t_grid): + for jdx,j in enumerate(i): + if t_grid[idx][jdx] == '#': + count += 1 + +print(count) +print(len(visited)) +print(len(set(visited))) + + + + + +