2018-09-30 23:01:58 +01:00
---
id: 5900f5371000cf542c51004a
title: 'Problem 459: Flipping game'
2020-11-27 19:02:05 +01:00
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302133
2021-01-13 03:31:00 +01:00
dashedName: problem-459-flipping-game
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2021-07-30 17:20:31 +02:00
The flipping game is a two player game played on a $N$ by $N$ square board.
2020-11-27 19:02:05 +01:00
2018-09-30 23:01:58 +01:00
Each square contains a disk with one side white and one side black.
2020-11-27 19:02:05 +01:00
The game starts with all disks showing their white side.
2018-09-30 23:01:58 +01:00
2021-07-30 17:20:31 +02:00
A turn consists of flipping all disks in a rectangle with the following properties:
- the upper right corner of the rectangle contains a white disk
- the rectangle width is a perfect square (1, 4, 9, 16, ...)
- the rectangle height is a triangular number (1, 3, 6, 10, ...)
<img class="img-responsive center-block" alt="flipping all disks in a 4x3 rectangle on a 5x5 board" src="https://cdn.freecodecamp.org/curriculum/project-euler/flipping-game-1.png" style="background-color: white; padding: 10px;">
2018-09-30 23:01:58 +01:00
Players alternate turns. A player wins by turning the grid all black.
2021-07-30 17:20:31 +02:00
Let $W(N)$ be the number of winning moves for the first player on a $N$ by $N$ board with all disks white, assuming perfect play.
$W(1) = 1$, $W(2) = 0$, $W(5) = 8$ and $W({10}^2) = 31\\,395$.
For $N = 5$, the first player's eight winning first moves are:
2018-09-30 23:01:58 +01:00
2021-07-30 17:20:31 +02:00
<img class="img-responsive center-block" alt="eight winning first moves for N = 5" src="https://cdn.freecodecamp.org/curriculum/project-euler/flipping-game-2.png" style="background-color: white; padding: 10px;">
2018-09-30 23:01:58 +01:00
2021-07-30 17:20:31 +02:00
Find $W({10}^6)$.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2018-09-30 23:01:58 +01:00
2021-07-30 17:20:31 +02:00
`flippingGame()` should return `3996390106631` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
2021-07-30 17:20:31 +02:00
assert.strictEqual(flippingGame(), 3996390106631);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
```js
2021-07-30 17:20:31 +02:00
function flippingGame() {
2020-09-15 09:57:40 -07:00
2018-09-30 23:01:58 +01:00
return true;
}
2021-07-30 17:20:31 +02:00
flippingGame();
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
// solution required
```