optimization of candy pattern