From a85d9d65ea9c5528ab81b6a3a705b2c337ac6973 Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Mon, 11 Dec 2023 14:31:41 +0100 Subject: [PATCH] Finished on 2023-12-10 p1 --- 2023/day10/part1.py | 22 +++--- 2023/day10/part2.py | 174 ++++++++++++++++++++++++++++++++++++++++++++ 2023/day10/t2.1 | 9 +++ 3 files changed, 194 insertions(+), 11 deletions(-) create mode 100644 2023/day10/part2.py create mode 100644 2023/day10/t2.1 diff --git a/2023/day10/part1.py b/2023/day10/part1.py index 1ba0be1..afc3c6a 100644 --- a/2023/day10/part1.py +++ b/2023/day10/part1.py @@ -100,7 +100,7 @@ while not found: #print('Went Right') prev = (y,x) y += 1 - print(wall(new)) + #print(wall(new)) sub_found = True if y > 0 and not sub_found: @@ -118,21 +118,21 @@ while not found: #print('Went Left') prev = (y,x) y -= 1 - print(wall(new)) + #print(wall(new)) sub_found = True if x > 0 and not sub_found: - print('Can go up') + #print('Can go up') # Up new = (list(cur)[0],list(cur)[1]-1) #print(wall(new)) if grid[x-1][y] in ('│','┌','┐','S') and new != prev: - print('Trying Up') - print('prev: ' + str(prev) + ' cur: ' + str(cur) + ' new: ' + str(new)) + #print('Trying Up') + #print('prev: ' + str(prev) + ' cur: ' + str(cur) + ' new: ' + str(new)) dirc = 'U' if wall(cur) in ('│','└','┘','S'): - print('Went Up') + #print('Went Up') if wall(new) == 'S': print('Found') found = True @@ -162,11 +162,11 @@ while not found: cur = (y,x) steps.append((x,y)) - p(grid,steps) - print('Going ' + dirc + ' to ' + str(cur) + ' ' + wall(cur)) - print('Previous is ' + str(prev) + ' ' + wall(prev)) - print() - input() + #p(grid,steps) + #print('Going ' + dirc + ' to ' + str(cur) + ' ' + wall(cur)) + #print('Previous is ' + str(prev) + ' ' + wall(prev)) + #print() + #input() count += 1 #print(cur, start) p(grid,steps) diff --git a/2023/day10/part2.py b/2023/day10/part2.py new file mode 100644 index 0000000..afc3c6a --- /dev/null +++ b/2023/day10/part2.py @@ -0,0 +1,174 @@ +import sys +from pprint import pprint + +#colors +from termcolor import colored + +grid = [] + +log = True + +def p(x,steps): + global log + if log: + for idx,i in enumerate(x): + for jdx,j in enumerate(i): + if j == 'S': + print(colored(j,'red'),end='') + elif (idx,jdx) in steps: + print(colored(j,'green'),end='') + else: + print(j,end='') + print() + + +def start_coords(grid): + for ydx,y in enumerate(grid): + for xdx,x in enumerate(y): + if x == 'S': + return (xdx,ydx) + +with open(sys.argv[1]) as file: + for line in file: + grid.append(line.rstrip()) + +for ldx,line in enumerate(grid): + grid[ldx] = line.translate(str.maketrans("-|F7LJ.", "─│┌┐└┘ ")) + + + +steps = [] + +#p(grid,steps) + +found = False + +start = start_coords(grid) +cur = start +prev = cur +count = 0 + +def wall(loc): + return grid[loc[1]][loc[0]] + +""" +steps = [(3,3),(2,3),(4,3),(3,2),(3,4)] +tmps = [] +for i in steps: + tmps.append(i) + p(grid,tmps) + input() +exit() +""" +y = cur[0] +x = cur[1] +print('Right is ' + wall((y+1,x)) + grid[x][y+1],end='') +print((y+1,x)) +print('Left is ' + wall((y-1,x)) + grid[x][y-1],end='') +print((y-1,x)) +print('Up is ' + wall((y,x-1)) + grid[x-1][y],end='') +print((y,x-1)) +print('Down is ' + wall((y,x+1)) + grid[x+1][y],end='') +print((y,x+1)) +print() +p(grid,steps) + +while not found: + y = cur[0] + x = cur[1] + + sub_found = False + #print('Start: ' + str(cur) + ' ' + wall(cur)) + + dirc = '' + + #print('Trying to find way') + #print('Previous is ' + str(prev) + wall(prev)) + if y+1 < len(grid[0]) and not sub_found: + #print('Can go right') + # Right + new = (list(cur)[0]+1,list(cur)[1]) + #print(wall(new)) + if grid[x][y+1] in ('─','┐','┘') and new != prev: + #print('Trying Right') + #print('prev: ' + str(prev) + ' cur: ' + str(cur) + ' new: ' + str(new)) + dirc = 'R' + if wall(cur) in ('─','┌','└','S'): + if wall(new) == 'S': + print('Found') + found = True + #print('Went Right') + prev = (y,x) + y += 1 + #print(wall(new)) + sub_found = True + + if y > 0 and not sub_found: + #print('Can go left') + # Left + new = (list(cur)[0]-1,list(cur)[1]) + if grid[x][y-1] in ('─','┌','└') and new != prev: + #print('Trying Left') + #print('prev: ' + str(prev) + ' cur: ' + str(cur) + ' new: ' + str(new)) + dirc = 'L' + if wall(cur) in ('─','┐','┘','S'): + if wall(new) == 'S': + print('Found') + found = True + #print('Went Left') + prev = (y,x) + y -= 1 + #print(wall(new)) + sub_found = True + + if x > 0 and not sub_found: + #print('Can go up') + # Up + new = (list(cur)[0],list(cur)[1]-1) + #print(wall(new)) + if grid[x-1][y] in ('│','┌','┐','S') and new != prev: + #print('Trying Up') + #print('prev: ' + str(prev) + ' cur: ' + str(cur) + ' new: ' + str(new)) + dirc = 'U' + + if wall(cur) in ('│','└','┘','S'): + #print('Went Up') + if wall(new) == 'S': + print('Found') + found = True + prev = (y,x) + x -=1 + sub_found = True + + if x < len(grid) and not sub_found: + #print('Can go down') + # Down + new = (list(cur)[0],list(cur)[1]+1) + #print(wall(new)) + if grid[x+1][y] in ('│','└','┘','S') and new != prev: + #print('Trying Down') + #print('prev: ' + str(prev) + ' cur: ' + str(cur) + ' new: ' + str(new)) + dirc = 'D' + if wall(cur) in ('│','┌','┐','S'): + #print('Went Down') + if wall(new) == 'S': + print('Found') + found = True + prev = (y,x) + x += 1 + sub_found = True + + + cur = (y,x) + + steps.append((x,y)) + #p(grid,steps) + #print('Going ' + dirc + ' to ' + str(cur) + ' ' + wall(cur)) + #print('Previous is ' + str(prev) + ' ' + wall(prev)) + #print() + #input() + count += 1 + #print(cur, start) +p(grid,steps) +print(count) +print(count/2) diff --git a/2023/day10/t2.1 b/2023/day10/t2.1 new file mode 100644 index 0000000..bd9cdf5 --- /dev/null +++ b/2023/day10/t2.1 @@ -0,0 +1,9 @@ +........... +.S-------7. +.|F-----7|. +.||.....||. +.||.....||. +.|L-7.F-J|. +.|..|.|..|. +.L--J.L--J. +...........