Nine letters A to I stand for the nine digits 1 to 9, each digit used exactly once (so there is no 0). Reading them as three 3-digit numbers, ABC, DEF and GHI (with A, D and G as hundreds digits), a pandigital sum is an assignment for which
ABC
+ DEF
-----
GHI
holds. For example 218 + 439 = 657 is one: the digits 2,1,8,4,3,9,6,5,7 are all different and cover 1-9. But 218 + 459 = 677 is not, since 7 appears twice (and 3 not at all). Swapping the two addends gives a different assignment, so 439 + 218 = 657 counts separately.
A puzzle setter pins down some of the letters in advance. Implement:
def count_pandigital_sums(hints: dict[str, int]) -> int
hints maps some letters (keys from "A" to "I") to digits from 1 to 9. Return how many pandigital sums agree with every hint. If two hints use the same digit, the answer is 0. hints may be empty.
count_pandigital_sums({"A": 2, "B": 1, "C": 8, "G": 6}) # 2: sums of the form 218 + DEF = 6HI
count_pandigital_sums({"A": 1, "B": 1}) # 0: two letters can't share a digit
count_pandigital_sums({"G": 1}) # 0: a sum of two numbers from 123 up can't start with 1
The function is called many times, so trying all 9! = 362,880 assignments per call (about a second in Python) is too slow.
Show hint
Fill the sum in column by column like long addition, starting with the units (C, F, I) and carrying into the tens and hundreds. Once the two digits of a column are chosen, the result digit is forced, so reject it immediately if it is 0, already used, or contradicts a hint. The hundreds column must not produce a carry.