add: part 1
This commit is contained in:
parent
dd0989dafd
commit
9459f5cc97
6
2023/day10/example1.txt
Normal file
6
2023/day10/example1.txt
Normal file
@ -0,0 +1,6 @@
|
||||
......,
|
||||
.S-7...
|
||||
.|.|..,
|
||||
.L7L-7.
|
||||
..L--J.
|
||||
.......
|
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)
|
Loading…
Reference in New Issue
Block a user