AdventOfCode2023/day10-1.py

174 lines
5.9 KiB
Python
Raw Normal View History

2023-12-10 16:54:54 -06:00
with open("input_day10.txt",encoding='utf-8') as file:
y_arr = [line.rstrip() for line in file]
class mato_Vector2:
def __init__(self,x,y):
self.x = x
self.y = y
def __str__(self):
return f"({self.x},{self.y})"
def as_tuple(self):
return (self.x,self.y)
def add_Vector2(self,vec):
#self.x += vec.x
#self.y += vec.y
new_vector = mato_Vector2(self.x + vec.x, self.y+vec.y)
return new_vector
def add(self,x,y):
#self.x += x
#self.y += y
new_vector = mato_Vector2(self.x + x, self.y+y)
return new_vector
def __eq__(self, other: object) -> bool:
if self.x == other.x and self.y == other.y:
return True
return False
maze = [[0 for x in range(len(y_arr)) ] for y in range(len(y_arr))]
starting_pos = mato_Vector2(0,0)
start_x = 0
for y in range(len(y_arr)):
for x in range(len(y_arr[y])):
maze[y][x] = y_arr[y][x]
if maze[y][x] == "S":
starting_pos = mato_Vector2(x,y)
#print(maze[y][x],end="")
#print()
print(starting_pos)
current_pos = starting_pos
#now go through the pipes in the right (literally in step 0) direction!
step = 0
alive = True
while alive:
if step==0:
#first step, go right bc im cool :3
delta_pos = mato_Vector2(1,0)
last_pos = current_pos
current_pos = current_pos.add_Vector2(delta_pos)
step+=1
else:
# step is not 0 so do normal stuff bc thats what normal programs do
# first check what current block is
print(f"{starting_pos} {current_pos} {step}")
current_block = maze[current_pos.y][current_pos.x]
match current_block:
case '':
#check bottom
if last_pos==current_pos.add(0,1):
delta_pos = mato_Vector2(0,-1)
#check top
if last_pos==current_pos.add(0,-1):
delta_pos = mato_Vector2(0,1)
case '':
# last position is left
if last_pos==current_pos.add(-1,0):
delta_pos = mato_Vector2(1,0)
# check right
if last_pos==current_pos.add(1,0):
delta_pos = mato_Vector2(-1,0)
case '':
# check left
if last_pos==current_pos.add(-1,0):
delta_pos = mato_Vector2(0,-1)
#check top
if last_pos==current_pos.add(0,-1):
delta_pos = mato_Vector2(-1,0)
case '':
# check left
if last_pos==current_pos.add(-1,0):
delta_pos = mato_Vector2(0,1)
#check bottom
if last_pos==current_pos.add(0,1):
delta_pos = mato_Vector2(-1,0)
case '':
# check right
if last_pos==current_pos.add(1,0):
delta_pos = mato_Vector2(0,-1)
#check top
if last_pos==current_pos.add(0,-1):
delta_pos = mato_Vector2(1,0)
case '':
# check right
if last_pos==current_pos.add(1,0):
delta_pos = mato_Vector2(0,1)
#check bottom
if last_pos==current_pos.add(0,1):
delta_pos = mato_Vector2(1,0)
#now check next position is valid
next_pos = mato_Vector2(current_pos.x+delta_pos.x,current_pos.y+delta_pos.y)
#if next block pos is start position loop is over, so break
if next_pos == starting_pos:
alive = False
total_length = step+1
break
elif next_pos.x < 0 or next_pos.x > 139:
next_block = '.'
elif next_pos.y < 0 or next_pos.y > 139:
next_block = '.'
else:
next_block = maze[next_pos.y][next_pos.x]
valid = False
if next_block == '.':
break
match next_block:
case '':
#check bottom
if current_pos==next_pos.add(0,1):
valid = True
#check top
if current_pos==next_pos.add(0,-1):
valid = True
case '':
# last position is left
if current_pos==next_pos.add(-1,0):
valid = True
# check right
if current_pos==next_pos.add(1,0):
valid = True
case '':
# check left
if current_pos==next_pos.add(-1,0):
valid = True
#check top
if current_pos==next_pos.add(0,-1):
valid = True
case '':
# check left
if current_pos==next_pos.add(-1,0):
valid = True
#check bottom
if current_pos==next_pos.add(0,1):
valid = True
case '':
# check right
if current_pos==next_pos.add(1,0):
valid = True
#check top
if current_pos==next_pos.add(0,-1):
valid = True
case '':
# check right
if current_pos==next_pos.add(1,0):
valid = True
#check bottom
if current_pos==next_pos.add(0,1):
valid = True
if valid == False:
alive = False
else:
#is valid, so update current position
last_pos = current_pos
current_pos = current_pos.add_Vector2(delta_pos)
step += 1
result = total_length/2
print(result)