# 2008 iTest Problems/Problem 87

## Problem

Find the number of $12$-digit "words" that can be formed from the alphabet $\{0,1,2,3,4,5,6\}$ if neighboring digits must differ by exactly $2$.

## Solution

Since the "digits" are numbers, all of the digits of the "word" must have the same parity. We can use casework to split up the cases.

Case 1: All digits are odd

The "outer numbers" are 1 and 5, and the middle number is 3. Since neighboring digits differ by exactly 2, the outer numbers and the middle number must alternate. Denoting $O$ as the outer number, we must have the following: $$\text{O 3 O 3 O 3 O 3 O 3 O 3}$$ $$\text{3 O 3 O 3 O 3 O 3 O 3 O}$$

It does not matter what the outer numbers are, and since both cases have the same number of outer numbers, there are $2^6 \cdot 2 = 128$ words that have odd digits.

Case 2: All digits are even

The "outer numbers" are 0 and 6, and the "middle numbers" are 2 and 4. In the sequence, we can not put two outer numbers in a row, and selecting one starting number determines the rest of the numbers (as there is only one adjacent edge number or middle number). As a result, we can split this case into further sub-cases depending on the number of outer numbers.

• If there are zero outer numbers, the only option is having 12 middle numbers in a row, so there is $1$ option in this case.
• If there is one outer number, there are $12$ options in this case.
• If there are two outer numbers, one of the middle number must be sandwiched between to prevent two outer numbers being next to each other. The 9 other middle numbers can go anywhere, so there are $\tbinom{11}{2} = 55$ options in this case.
• If there are three outer numbers, two of the middle numbers must be sandwiched between to prevent two outer numbers being next to each other. The 7 other middle numbers can go anywhere, so there are $\tbinom{10}{3} = 120$ options in this case.
• Similarly, with four outer numbers, there are $\tbinom{9}{4} = 126$ options. With five outer numbers, there are $\tbinom{8}{5} = 56$ options. With six outer numbers, there are $\tbinom{7}{6} = 7$ options.
• If there are seven (or more) outer numbers, six (or more) middle numbers must be sandwiched between to prevent two outer numbers being next to each other. However, that means the word has 13 digits, so this option does not work.

Since there are 2 ways to pick the starting number, there are $2 \cdot (7 + 56 + 126 + 120 + 55 + 13) = 754$ options if all of the digits are even.

Altogether there are $128 + 754 = \boxed{882}$ words that satisfy the conditions.