If we let k=2n, the positions John visits during his journey are the positive integer multiples of k. Because k∣22018, he will arrive at position 22018 and then visit the same positions in the circle for each subsequent time he moves around the circle.
We will say "John carries empty-full" if he carries one full and one empty bucket. When John starts the game, he carries empty-full.
Claim 1. Suppose John carries empty-full when arriving at position s. Then he can move to position s+2k without losing the game exactly when one of the two buckets at positions s and s+k is empty and the other is full of water. Moreover, he will then carry empty-full when he arrives at position s+2k.
Proof of Claim 1.
Case (i): There is a full bucket at position s. In this case, John will move from position s to position s+k with two full buckets. He will lose the game if he finds a full bucket at position s+k. Otherwise, he does not lose, drops one of the full buckets there and picks up the empty one, so that he leaves carrying empty-full.
Case (ii): There is an empty bucket at position s. In this case, John will move from position s to position s+k with two empty buckets. He will lose the game if he finds an empty bucket at position s+k. Otherwise he does not lose the game and drops one of the empty buckets there and picks up the full one, so that he leaves carrying empty-full. □
Claim 2. A necessary and sufficient condition for John to win the game is that for each positive integer t, one of the two buckets at positions (2t−1)k and 2tk is empty and the other is full of water.
Proof of Claim 2. This follows easily from claim 1 using induction.
Because n<2018 we see that 22018 is an even multiple of k. Hence, when John leaves position 22018 without having lost the game, he will carry empty-full and the condition of claim 2 for the multiples of k is still satisfied (because each full bucket was replaced by an empty one and vice versa). This means that John can move indefinitely without losing the game, if he can finish one circle successfully.
Claim 3. The filling level (full or empty) of the bucket at position 22018 determines the filling level of all other buckets if John does not lose the game.
Proof of Claim 3. Each position s (1≤s<22018) can be written in a unique way in the form s=b⋅2m, where b is odd and 0≤m<2018. Write
22018−s=2m1+2m2+2m3+⋯+2mr
with integers 0≤m1<m2<m3<⋯<mr. This representation is unique and for each 1<i≤r we see that 2mi is the highest power of 2 that divides s+2m1+2m2+⋯+2mi−1 and 2mi is the highest power of 2 that divides s. Hence, according to claim 2, for i=r,r−1,…,2, the filling level of the bucket at position s+2m1+2m2+⋯+2mi−1 is determined by the filling level of the bucket at position s+2m1+2m2+⋯+2mi−1+2mi in order to ensure that John can win if n=mi was chosen. Moreover, the filling level of the bucket at position s is determined by the filling level of the bucket at position s+2m1.
Claim 3 shows that there are at most two ways to fill the buckets before the game starts that guarantee John to win the game. We will now group the positions into two types, called type +1 and type −1, such that John will not lose the game if all the buckets of the same type have the same filling level and buckets of different types have different filling level.
Define the binary weight w(s) of a non-negative integer s to be the number of digits 1 in the binary representation of s. In other words, if s=2m1+2m2+2m3+⋯+2mr with 0≤m1<m2<m3<⋯<mr, then w(s)=r and w(0)=0. We define the type t(s) of a position s (1≤s≤22018) as
t(s)=(−1)w(s−1).
Claim 4. If all buckets at positions of type +1 are empty and all buckets at positions of type −1 are filled with water at the start of the game, the conditions of claim 2 are satisfied for each k=2n (0≤n<2018).
Proof of Claim 4. Let k=2n and s=(2t+1)k for a non-negative integer t.
Then s−1=2tk+(k−1)=t⋅2n+1+(2n−1). The binary representation of t⋅2n+1 ends in at least n+1 zeros. The binary representation of 2n−1 consists of n digits 1 at the last n places. Therefore, w(s−1)=w(t)+n. On the other hand, s+k−1=(2t+1)k+(k−1)=t⋅2n+1+2n+(2n−1) has a digit 1 in its binary representation at place n+1 (from the end), otherwise coincides with the binary representation of s−1. Hence, w(s+k−1)=w(t)+1+n=w(s−1)+1. This implies t(s+k)=−t(s) as required. □
Of course, claim 4 is true when we exchange filled and empty in its statement. Therefore, it will be possible for John win the game precisely when t(2018)=t(2019). Because 2017=2016+1=63⋅32+1=(26−1)⋅25+20 has binary representation (1111100001), we have w(2017)=7, which gives t(2018)=(−1)w(2017)=−1. The binary representation of 2018 is (1111100010) and so w(2018)=7, which gives t(2019)=(−1)w(2018)=−1. Therefore, when John fills all buckets of type −1 with water he will not lose the game.