#!/usr/bin/env python3 lines = (x.strip() for x in open("input.txt")) instructions = tuple(int(char == "R") for char in next(lines)) next(lines) nodes = {line[:3]: (line[7:10], line[12:15]) for line in lines} node = "AAA" cnt = 0 while True: for char in instructions: node = nodes[node][char] cnt += 1 if node == "ZZZ": print(cnt) exit(0)