The last penalty rule keeps the whole symbol close to half dark and half light, so no scanner threshold is starved. Today you write it and finish the scoring toolkit.
Score a grid by how far its proportion of dark modules strays from 50 percent.
A scanner decides dark-versus-light against a threshold, and a symbol skewed heavily toward one color gives it less to work with. Rule 4 nudges the grid toward a 50/50 balance. Compute the percentage of dark modules, find the two multiples of five on either side of it, subtract 50 from each and take absolute values, divide each by 5, keep the smaller, and multiply by 10.
The effect is a staircase: anything from 45 to 55 percent scores 0, and each further 5 percent of imbalance adds 10. A perfectly balanced grid scores 0; an all-dark grid scores 100; 60 percent dark scores 20. This rule is usually small compared to rule 3, but it is the tiebreaker that favors a well-textured symbol. With all four rules in hand, the next lesson totals them across the eight masks - but first you need the format information, because it is placed before scoring.
// percent of dark modules, then distance from 50% in 5% steps.ratio := dark * 100 / totalprev := (ratio / 5) * 5next := prev + 5lo := min(abs(prev-50), abs(next-50))penalty := (lo / 5) * 10