parent
0fe4c0d032
commit
c02f2ec040
1 changed files with 53 additions and 0 deletions
@ -0,0 +1,53 @@ |
||||
input = [[i for i in l] for l in open("input.txt", 'r').read().splitlines()] |
||||
traversed = [] |
||||
|
||||
whereami = (0, 0) |
||||
next = (0,0) |
||||
|
||||
|
||||
for x in range(len(input)): |
||||
for y in range(len(input[x])): |
||||
if input[x][y] == 'S': |
||||
whereami = (x, y) |
||||
if x > 0 and input[x-1][y] in ['|', '7', 'F']: |
||||
next = (x-1, y) |
||||
break |
||||
if x+1 < len(input) and input[x+1][y] in ['|', 'L', 'J']: |
||||
next = (x+1, y) |
||||
break |
||||
if y > 0 and input[x][y-1] in ['-', 'L', 'F']: |
||||
next = (x, y-1) |
||||
break |
||||
if y+1 < len(input[0]) and input[x][y+1] in ['-', '7', 'J']: |
||||
next = (x, y+1) |
||||
break |
||||
|
||||
while True: |
||||
x, y = next |
||||
traversed.append((x,y)) |
||||
if input[x][y] == 'F': |
||||
next = (x+1, y) if whereami == (x,y+1) else (x, y+1) |
||||
elif input[x][y] == '|': |
||||
next = (x+1, y) if whereami == (x-1,y) else (x-1, y) |
||||
elif input[x][y] == '-': |
||||
next = (x, y+1) if whereami == (x,y-1) else (x, y-1) |
||||
elif input[x][y] == 'L': |
||||
next = (x-1, y) if whereami == (x,y+1) else (x, y+1) |
||||
elif input[x][y] == 'J': |
||||
next = (x-1, y) if whereami == (x,y-1) else (x, y-1) |
||||
elif input[x][y] == '7': |
||||
next = (x+1, y) if whereami == (x,y-1) else (x, y-1) |
||||
if next in traversed: |
||||
break |
||||
whereami = (x, y) |
||||
|
||||
print(len(traversed)/2) |
||||
|
||||
area = 0 |
||||
for i in range(len(traversed)-1): |
||||
area += (traversed[i][1] + traversed[i+1][1]) * (traversed[i][0] - traversed[i+1][0]) |
||||
area += (traversed[-1][1] + traversed[0][1]) * (traversed[-1][0] - traversed[0][0]) |
||||
area = abs(area / 2) |
||||
|
||||
points = area - len(traversed) / 2 + 1 |
||||
print(points) |
Loading…
Reference in new issue