90 lines
1.9 KiB
Python
90 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
part_sum = 0
|
|
matrix = []
|
|
|
|
# read matrix like day 3
|
|
input_file = open("input-ex11", "r")
|
|
for line in input_file:
|
|
matrix.append(line.strip())
|
|
input_file.close()
|
|
|
|
zeilen = len(matrix)
|
|
spalten = len(matrix[0])
|
|
|
|
# Ausgabe Matrix und Startposition finden
|
|
for i in range(len(matrix)):
|
|
if "S" in matrix[i]:
|
|
zs = i
|
|
print(matrix[i])
|
|
for j in range(len(matrix[zs])):
|
|
if "S" in matrix[zs][j]:
|
|
ss = j
|
|
start = [zs, ss]
|
|
print()
|
|
print(f"Startposition: {start}")
|
|
|
|
|
|
def come_from(z, s, pre):
|
|
if pre[0]+1 == z:
|
|
return "N"
|
|
elif pre[0]-1 == z:
|
|
return "S"
|
|
elif pre[1]+1 == s:
|
|
return "W"
|
|
elif pre[1]-1 == s:
|
|
return "E"
|
|
else:
|
|
return "A"
|
|
|
|
|
|
def where_to_go(z, s, pre):
|
|
pipe = matrix[z][s]
|
|
come = come_from(z, s, pre)
|
|
if pipe == "S":
|
|
return "Ende"
|
|
if come == "N" and pipe == "|":
|
|
return z+1, s
|
|
elif come == "S" and pipe == "|":
|
|
return z-1, s
|
|
elif come == "E" and pipe == "-":
|
|
return z, s-1
|
|
elif come == "W" and pipe == "-":
|
|
return z, s+1
|
|
elif come == "N" and pipe == "L":
|
|
return z, s+1
|
|
elif come == "E" and pipe == "L":
|
|
return z-1, s
|
|
elif come == "W" and pipe == "7":
|
|
return z+1, s
|
|
elif come == "S" and pipe == "7":
|
|
return z, s-1
|
|
elif come == "N" and pipe == "J":
|
|
return z, s-1
|
|
elif come == "W" and pipe == "J":
|
|
return z-1, s
|
|
elif come == "E" and pipe == "F":
|
|
return z+1, s
|
|
elif come == "S" and pipe == "F":
|
|
return z, s+1
|
|
|
|
|
|
previous = (start[0], start[1])
|
|
aktuell = [2, 1]
|
|
count = 1
|
|
|
|
while True:
|
|
new = where_to_go(aktuell[0], aktuell[1], previous)
|
|
print(aktuell, previous, new)
|
|
if new == "Ende":
|
|
break
|
|
else:
|
|
count += 1
|
|
previous = (aktuell[0], aktuell[1])
|
|
aktuell = [new[0], new[1]]
|
|
|
|
print("Counter:", count)
|
|
print("HalfCounter:", count/2)
|
|
|