Code-Driven Planning in Grid Worlds with Large Language Models