AdventOfCode/2023/day10/part1.py

180 lines
4.6 KiB
Python
Raw Normal View History

2023-12-11 10:23:30 +01:00
import sys
2023-12-11 20:36:17 +01:00
import os
2023-12-11 10:23:30 +01:00
from pprint import pprint
2023-12-11 20:36:17 +01:00
import time
2023-12-11 10:23:30 +01:00
#colors
from termcolor import colored
grid = []
log = True
2023-12-11 13:50:58 +01:00
def p(x,steps):
2023-12-11 10:23:30 +01:00
global log
if log:
2023-12-11 13:50:58 +01:00
for idx,i in enumerate(x):
for jdx,j in enumerate(i):
2023-12-11 10:23:30 +01:00
if j == 'S':
print(colored(j,'red'),end='')
2023-12-11 13:50:58 +01:00
elif (idx,jdx) in steps:
print(colored(j,'green'),end='')
2023-12-11 10:23:30 +01:00
else:
print(j,end='')
print()
2023-12-11 13:50:58 +01:00
def start_coords(grid):
for ydx,y in enumerate(grid):
for xdx,x in enumerate(y):
if x == 'S':
return (xdx,ydx)
2023-12-11 10:23:30 +01:00
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.", "─│┌┐└┘ "))
2023-12-11 13:50:58 +01:00
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
2023-12-11 14:15:22 +01:00
#print('Start: ' + str(cur) + ' ' + wall(cur))
2023-12-11 13:50:58 +01:00
dirc = ''
2023-12-11 20:36:17 +01:00
if len(sys.argv) == 3:
p(grid,steps)
time.sleep(float(sys.argv[2]))
2023-12-11 13:50:58 +01:00
2023-12-11 14:15:22 +01:00
#print('Trying to find way')
#print('Previous is ' + str(prev) + wall(prev))
2023-12-11 13:50:58 +01:00
if y+1 < len(grid[0]) and not sub_found:
2023-12-11 14:15:22 +01:00
#print('Can go right')
2023-12-11 13:50:58 +01:00
# Right
new = (list(cur)[0]+1,list(cur)[1])
2023-12-11 14:15:22 +01:00
#print(wall(new))
2023-12-11 13:50:58 +01:00
if grid[x][y+1] in ('','','') and new != prev:
2023-12-11 14:15:22 +01:00
#print('Trying Right')
#print('prev: ' + str(prev) + ' cur: ' + str(cur) + ' new: ' + str(new))
2023-12-11 13:50:58 +01:00
dirc = 'R'
if wall(cur) in ('','','','S'):
2023-12-11 14:25:28 +01:00
if wall(new) == 'S':
print('Found')
found = True
2023-12-11 14:15:22 +01:00
#print('Went Right')
2023-12-11 13:50:58 +01:00
prev = (y,x)
y += 1
2023-12-11 14:31:41 +01:00
#print(wall(new))
2023-12-11 13:50:58 +01:00
sub_found = True
if y > 0 and not sub_found:
2023-12-11 14:15:22 +01:00
#print('Can go left')
2023-12-11 13:50:58 +01:00
# Left
new = (list(cur)[0]-1,list(cur)[1])
if grid[x][y-1] in ('','','') and new != prev:
2023-12-11 14:15:22 +01:00
#print('Trying Left')
#print('prev: ' + str(prev) + ' cur: ' + str(cur) + ' new: ' + str(new))
2023-12-11 13:50:58 +01:00
dirc = 'L'
if wall(cur) in ('','','','S'):
2023-12-11 14:25:28 +01:00
if wall(new) == 'S':
print('Found')
found = True
2023-12-11 14:15:22 +01:00
#print('Went Left')
prev = (y,x)
2023-12-11 13:50:58 +01:00
y -= 1
2023-12-11 14:31:41 +01:00
#print(wall(new))
2023-12-11 13:50:58 +01:00
sub_found = True
if x > 0 and not sub_found:
2023-12-11 14:31:41 +01:00
#print('Can go up')
2023-12-11 13:50:58 +01:00
# Up
new = (list(cur)[0],list(cur)[1]-1)
2023-12-11 14:15:22 +01:00
#print(wall(new))
2023-12-11 14:25:28 +01:00
if grid[x-1][y] in ('','','','S') and new != prev:
2023-12-11 14:31:41 +01:00
#print('Trying Up')
#print('prev: ' + str(prev) + ' cur: ' + str(cur) + ' new: ' + str(new))
2023-12-11 13:50:58 +01:00
dirc = 'U'
2023-12-11 14:15:22 +01:00
2023-12-11 13:50:58 +01:00
if wall(cur) in ('','','','S'):
2023-12-11 14:31:41 +01:00
#print('Went Up')
2023-12-11 14:25:28 +01:00
if wall(new) == 'S':
print('Found')
found = True
2023-12-11 14:15:22 +01:00
prev = (y,x)
2023-12-11 13:50:58 +01:00
x -=1
sub_found = True
if x < len(grid) and not sub_found:
2023-12-11 14:15:22 +01:00
#print('Can go down')
2023-12-11 13:50:58 +01:00
# Down
new = (list(cur)[0],list(cur)[1]+1)
2023-12-11 14:15:22 +01:00
#print(wall(new))
2023-12-11 14:25:28 +01:00
if grid[x+1][y] in ('','','','S') and new != prev:
2023-12-11 14:15:22 +01:00
#print('Trying Down')
#print('prev: ' + str(prev) + ' cur: ' + str(cur) + ' new: ' + str(new))
2023-12-11 13:50:58 +01:00
dirc = 'D'
if wall(cur) in ('','','','S'):
2023-12-11 14:15:22 +01:00
#print('Went Down')
2023-12-11 14:25:28 +01:00
if wall(new) == 'S':
print('Found')
found = True
2023-12-11 14:15:22 +01:00
prev = (y,x)
2023-12-11 13:50:58 +01:00
x += 1
sub_found = True
cur = (y,x)
2023-12-11 14:15:22 +01:00
2023-12-11 13:50:58 +01:00
steps.append((x,y))
2023-12-11 14:31:41 +01:00
#p(grid,steps)
#print('Going ' + dirc + ' to ' + str(cur) + ' ' + wall(cur))
#print('Previous is ' + str(prev) + ' ' + wall(prev))
#print()
#input()
2023-12-11 13:50:58 +01:00
count += 1
2023-12-11 14:15:22 +01:00
#print(cur, start)
p(grid,steps)
2023-12-11 13:50:58 +01:00
print(count)
2023-12-11 14:15:22 +01:00
print(count/2)