I had to write code along the lines of the following. The code is not that important.
const afterState = [...state]; for (const [iSwitch,count] of Object.entries(switchCountBySwitch)) { for (const clock of SWITCHES[iSwitch]) { afterState[clock] = (afterState[clock] + 3 * count) % 12; } } const success = afterState.every(clock => clock === 0); if (success) { currentMinCount = currentCount; }
I made a mistake by putting a portion of the code inside the wrong scope.
const afterState = [...state]; for (const [iSwitch,count] of Object.entries(switchCountBySwitch)) { for (const clock of SWITCHES[iSwitch]) { afterState[clock] = (afterState[clock] + 3 * count) % 12; } const success = afterState.every(clock => clock === 0); if (success) { currentMinCount = currentCount; } }
No wonder I got the wrong answer. I should make sure to read the code carefully (esp. when debuggers are not available)
Top comments (0)