Agent in toy environment only learns to act optimally with small discount factors
This is an episodic problem, and there should be no issue in theory with most learning algorithms coping without a discount factor (or setting gamma = 1).


  Are there any explanations to why the agent only works for small discount factors in this example?


The most likely explanations are:


You have a mistake in your implementation or use of DQN.
You have an incorrect setting of a neural network hyperparameter. My initial thought would be you have mistakenly put a non-linearity like softmax or sigmoid on the NN outputs (it needs to be a linear output). Or it could just be that 10 hidden neurons is not enough for this representation
Convergence requires far longer to train than you thought, once you introduce non-linear relationships between state and expected reward.


It is not surprising that the simpler puzzle trains more easily, as the agent can reduce it down to a linear counting puzzle without really needing to "look" at the representation. I would expect the Q values to be very poor approximations to the correct values after so few iterations, but the problem is simple enough that the incorrect values still produce an optimal behaviour.