19.py
1 from lib import * 2 3 input = read_input(2017, 19) 4 5 lines = input.splitlines() 6 7 8 w, h = len(lines[0]), len(lines) 9 x, y = lines[0].index("|"), 0 10 dx, dy = 0, 1 11 out = "" 12 get = lambda x, y: lines[y][x] if y in range(h) and x in range(w) else " " 13 while True: 14 c = get(x, y) 15 f = get(x + dx, y + dy) 16 l = get(x + dy, y - dx) 17 r = get(x - dy, y + dx) 18 if c.isalpha(): 19 out += c 20 if f == " ": 21 if l != " ": 22 dx, dy = dy, -dx 23 elif r != " ": 24 dx, dy = -dy, dx 25 else: 26 break 27 x += dx 28 y += dy 29 30 print(out) 31 32 33 w, h = len(lines[0]), len(lines) 34 x, y = lines[0].index("|"), 0 35 dx, dy = 0, 1 36 out = 0 37 get = lambda x, y: lines[y][x] if y in range(h) and x in range(w) else " " 38 while True: 39 out += 1 40 f = get(x + dx, y + dy) 41 l = get(x + dy, y - dx) 42 r = get(x - dy, y + dx) 43 if f == " ": 44 if l != " ": 45 dx, dy = dy, -dx 46 elif r != " ": 47 dx, dy = -dy, dx 48 else: 49 break 50 x += dx 51 y += dy 52 53 print(out)