We notice that we're repeating calculations at each step, so use a map to ensure we do each stone ID once per step.