|
| 1 | +import sys |
| 2 | +sys.path.append("..") |
| 3 | + |
| 4 | +from aoclib import util |
| 5 | + |
| 6 | +# Find the single giant loop starting at S. How many steps along the loop |
| 7 | +# does it take to get from the starting position to the point farthest |
| 8 | +# from the starting position? |
| 9 | +def solve_10(data): |
| 10 | + # N, E, S, W mappings (clockwise!) |
| 11 | + pipes = {'|': [True, False, True, False], |
| 12 | + '-': [False, True, False, True], |
| 13 | + 'L': [True, True, False, False], |
| 14 | + 'J': [True, False, False, True], |
| 15 | + '7': [False, False, True, True], |
| 16 | + 'F': [False, True, True, False], |
| 17 | + '.': [False, False, False, False], # ground; there is no pipe in this tile. |
| 18 | + 'S': [True, True, True, True] # Start, should be able to enter from all directions? |
| 19 | + } |
| 20 | + # vectors for N, E, S, W |
| 21 | + nswe = [(0,-1), (1,0), (0,1), (-1,0) ] |
| 22 | + |
| 23 | + # S is the starting position of the animal; there is a pipe on this tile, |
| 24 | + # but your sketch doesn't show what shape the pipe has. |
| 25 | + for y0 in range(0,len(data)): |
| 26 | + if -1 != (x0 := data[y0].find('S')): |
| 27 | + break |
| 28 | + print(x0,y0) |
| 29 | + |
| 30 | + x = x0 |
| 31 | + y = y0 |
| 32 | + path = [] # stores the path |
| 33 | + |
| 34 | + loop = True |
| 35 | + while loop: |
| 36 | + for direction in range(0, len(nswe)): |
| 37 | + if not pipes[data[y][x]][direction]: |
| 38 | + continue |
| 39 | + # Get candidate position, I don't even pretend to 100% understand this |
| 40 | + (cx,cy) = tuple(sum(i) for i in zip ((x,y), nswe[direction]) ) |
| 41 | + |
| 42 | + # Edge cases |
| 43 | + if cx < 0 or cx >=len(data[0]) or cy < 0 or cy >=len(data): |
| 44 | + continue |
| 45 | + |
| 46 | + comefrom = (direction + 2) % len(nswe) |
| 47 | + pipe = data[cy][cx] |
| 48 | + if pipes[pipe][comefrom]: # We could go that way |
| 49 | + print(f"{x} {y} {cx} {cy} {comefrom} {pipe}") |
| 50 | + if not path or direction != path[-1]: |
| 51 | + # Let's not go if we're doing a 180 |
| 52 | + path.append(comefrom) |
| 53 | + (x,y) = (cx,cy) |
| 54 | + print(f"OK {x} {y}") |
| 55 | + |
| 56 | + if (x,y) == (x0,y0) and path: |
| 57 | + loop = False |
| 58 | + break |
| 59 | + |
| 60 | + return len(path)/2 |
| 61 | + |
| 62 | +# Oh, but how many tiles are inside the loop? |
| 63 | +def solve_10b(data): |
| 64 | + # Great. How do I define "outside" and "inside"? |
| 65 | + return None |
| 66 | + |
| 67 | +if __name__ == '__main__': |
| 68 | + data = util.getInput("../inputs/10.txt") |
| 69 | + print( solve_10(data) ) |
| 70 | + |
0 commit comments