In the 2022 AMC 10A competition, Problem #9 presents a classic map coloring challenge on a partitioned rectangle:
Contest Problem Statement: "A rectangle is partitioned into 5 regions as shown. Each region is to be painted a solid color—red, orange, yellow, blue, or green—so that regions that touch are painted different colors, and colors can be used more than once. How many different colorings are possible?"
Analyzing the Adjacency Constraints
Two regions "touch" if they share an edge (a boundary line segment). Merely touching at a single corner point does not count.
Looking at the diagram:
T1 (Top Left) touches: T2, B1, and B2.
T2 (Top Right) touches: T1, B2, and B3.
B1 (Bottom Left) touches: T1 and B2.
B2 (Bottom Middle) spans the center division and touches all other four regions: B1, T1, T2, and B3!
B3 (Bottom Right) touches: B2 and T2.
The Declarative Prolog Solution
In procedural languages (like C or Java), you would write nested for loops and nested if conditions. In Prolog, you simply declare what makes a coloring valid:
Pick a color for each region from the 5 available colors.
Ensure every pair of touching regions receives different colors (using \=).
Use findall/3 to collect all valid combinations and count them!
Move the mouse over a dotted box for more information.
By evaluating all combinations that satisfy these constraints, Prolog's backtracking search discovers exactly 540 valid colorings without any manual algebraic casework!
Now you try.
Replace ???? with T2 and click Run to compute the 540 valid 5-region colorings. Then explore Example 1 to see individual color combinations, or Example 2 to test what happens with only 3 or 4 colors!
Type your code here:
See your results here:
The code has ???? in the adjacency inequality between T1 and T2. Replace ???? with T2 and click Run to compute all 540 valid colorings!
Prolog returns:
Count = 540
This matches option (D) 540 on the 2022 AMC 10A exam.
Share your code
Show a friend, family member, or teacher what you've done!