add: part 1
This commit is contained in:
		
							
								
								
									
										57
									
								
								2023/day10/src/part1.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								2023/day10/src/part1.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,57 @@
 | 
			
		||||
text: str = open("input.txt", "r").read()
 | 
			
		||||
 | 
			
		||||
_value: int = 0 
 | 
			
		||||
 | 
			
		||||
lines = text.splitlines()
 | 
			
		||||
 | 
			
		||||
def get_symbol_vector(symbol: str):
 | 
			
		||||
	match symbol:
 | 
			
		||||
		case "|":
 | 
			
		||||
			return ((0, -1), (0, +1))
 | 
			
		||||
		case "L":
 | 
			
		||||
			return ((0, -1), (+1, 0))
 | 
			
		||||
		case "J":
 | 
			
		||||
			return ((-1, 0), (0, -1))
 | 
			
		||||
		case "7":
 | 
			
		||||
			return ((-1, 0), (0, +1))
 | 
			
		||||
		case "F":
 | 
			
		||||
			return ((+1, 0), (0, +1))
 | 
			
		||||
		case "-":
 | 
			
		||||
			return ((+1, 0), (-1, 0))
 | 
			
		||||
 | 
			
		||||
def get_start(lines: [str]):
 | 
			
		||||
	for y, line in enumerate(lines):
 | 
			
		||||
		for x, char in enumerate(line):
 | 
			
		||||
			if (char == "S"):
 | 
			
		||||
				return (x, y)
 | 
			
		||||
 | 
			
		||||
def get_around(lines, x, y):
 | 
			
		||||
	lst = []
 | 
			
		||||
	if (len(lines[y]) != x + 1 and lines[y][x + 1] != '.'):
 | 
			
		||||
		lst.append((x + 1, y))
 | 
			
		||||
	if (x > 0 and lines[y][x - 1] != '.'):
 | 
			
		||||
		lst.append((x - 1, y))
 | 
			
		||||
	if (len(lines) != y + 1 and lines[y + 1][x] != '.'):
 | 
			
		||||
		lst.append((x, y + 1))
 | 
			
		||||
	if (y > 0 and lines[y - 1][x] != '.'):
 | 
			
		||||
		lst.append((x, y - 1))
 | 
			
		||||
	return lst
 | 
			
		||||
 | 
			
		||||
old_x, old_y = get_start(lines)
 | 
			
		||||
x, y = get_around(lines, old_x, old_y)[0]
 | 
			
		||||
nb_moves = 1
 | 
			
		||||
while True:
 | 
			
		||||
	backward, forward = get_symbol_vector(lines[y][x])
 | 
			
		||||
	if (backward == (old_x - x, old_y - y)):
 | 
			
		||||
		old_x, old_y = x, y
 | 
			
		||||
		x, y = map(sum, zip((x, y), forward))
 | 
			
		||||
	else:
 | 
			
		||||
		old_x, old_y = x, y
 | 
			
		||||
		x,y = map(sum, zip((x, y), backward))
 | 
			
		||||
	print(lines[y][x], x, y)
 | 
			
		||||
	nb_moves += 1
 | 
			
		||||
	if (lines[y][x] == "S"):
 | 
			
		||||
		_value = nb_moves // 2
 | 
			
		||||
		break
 | 
			
		||||
 | 
			
		||||
print(_value)
 | 
			
		||||
		Reference in New Issue
	
	Block a user