""" day19.py - advent of code for day 19 programming workshop class exercise """ data = open('input19.txt').readlines() #print(data) print("The number of lines is ", len(data)) print("The 0'th line : '{}'".format(data[0])) # starting point: row = 0 col = 0 while data[row][col] == ' ': col = col + 1 print("Starting at row={}, col={} there is a '{}'".format( row, col, data[row][col])) direction = 'down' steps = 0 def move(row, col, direction): """ return new row, col, direction after making a move """ row += {'down': 1, 'up': -1, 'right': 0, 'left': 0}[direction] col += {'down': 0, 'up': 0, 'right': 1, 'left': -1}[direction] if data[row][col] == '+': # change direction if direction == 'up' or direction == 'down': if data[row][col+1] == ' ': direction = 'left' else: direction = 'right' else: if data[row+1][col] == ' ': direction = 'up' else: direction = 'down' return (row, col, direction) letters = [] here = data[row][col] while here != ' ': if here.isalpha(): letters.append(here) (row, col, direction) = move(row, col, direction) here = data[row][col] steps += 1 print("The answer to part 1 is {}".format(letters)) print("The answer to part 2 is {}".format(steps))