← RU Pythoncursus

Uitwerking: L-systemen

import turtle as t
t.speed(0)
t.delay(0)
with open(input()) as f:
    ls = f.read().splitlines()
    a, n, d = map(int, ls[:3])
    s = ls[3]
    rs = dict(l.split(':') for l in ls[4:])
for _ in range(d):
    s = ''.join(rs.get(c, c) for c in s)
l = []
def pop():
    p, h = l.pop()
    t.pu()
    t.goto(p)
    t.pd()
    t.seth(h)
for c in s:
    {'F': lambda: t.fd(n),
     '+': lambda: t.rt(a),
     '-': lambda: t.lt(a),
     '[': lambda: l.append((t.pos(), t.heading())),
     ']': lambda: pop()
     }.get(c, lambda: 0)()
t.done()