import [Link].
*;
public class ToggleChallenge {
// Method to get possible digits for a faulty digit
public static List<Integer> getPossibleDigits(Map<Integer, String> validDigits,
String faultyDigit) {
List<Integer> possibleDigits = new ArrayList<>();
for ([Link]<Integer, String> entry : [Link]()) {
int mismatches = 0;
String pattern = [Link]();
// Count mismatches between the faulty digit and the pattern
for (int i = 0; i < [Link](); i++) {
if ([Link](i) != [Link](i)) {
mismatches++;
if (mismatches > 1) break; // Stop early if mismatches exceed 1
}
}
// If mismatches <= 1, it's a possible match
if (mismatches <= 1) {
[Link]([Link]());
}
}
return possibleDigits;
}
// Solve the main problem
public static void solve() {
Scanner scanner = new Scanner([Link]);
// Read the 7-segment digit patterns
List<String> segmentData = new ArrayList<>();
for (int i = 0; i < 3; i++) {
[Link]([Link]().strip());
}
// Read the faulty 7-segment input
List<String> faultyInput = new ArrayList<>();
for (int i = 0; i < 3; i++) {
[Link]([Link]().strip());
}
// Map digits to their segment patterns
Map<Integer, String> digitPatterns = new HashMap<>();
for (int digit = 0; digit < 10; digit++) {
StringBuilder pattern = new StringBuilder();
for (int row = 0; row < 3; row++) {
[Link]([Link](row).substring(digit * 3, (digit +
1) * 3));
}
[Link](digit, [Link]());
}
// Generate possible numbers
List<List<Integer>> possibleNumbers = new ArrayList<>();
int faultyLength = [Link](0).length() / 3;
for (int i = 0; i < faultyLength; i++) {
StringBuilder faultyDigit = new StringBuilder();
for (int row = 0; row < 3; row++) {
[Link]([Link](row).substring(i * 3, (i + 1) *
3));
}
List<Integer> matchingDigits = getPossibleDigits(digitPatterns,
[Link]());
if ([Link]()) {
[Link]("Invalid"); // No newline here
return; // If no valid digit found, print "Invalid" and return
}
[Link](matchingDigits);
}
// Calculate the total sum of all valid combinations
long totalSum = calculateTotalSum(possibleNumbers);
[Link](totalSum); // Print without extra newline
}
// Calculate the total sum of all valid combinations
public static long calculateTotalSum(List<List<Integer>> possibleNumbers) {
long totalSum = 0;
// Use iterative approach to avoid recursion overhead
List<Integer> indices = new
ArrayList<>([Link]([Link](), 0));
int[] currentIndices = new int[[Link]()];
int digitCount = [Link]();
while (true) {
// Build the number from current indices
StringBuilder number = new StringBuilder();
for (int i = 0; i < digitCount; i++) {
[Link]([Link](i).get(currentIndices[i]));
}
totalSum += [Link]([Link]());
// Increment indices for the next combination
int idx = digitCount - 1;
while (idx >= 0) {
currentIndices[idx]++;
if (currentIndices[idx] < [Link](idx).size()) break;
currentIndices[idx] = 0;
idx--;
}
// Exit if all combinations are exhausted
if (idx < 0) break;
}
return totalSum;
}
public static void main(String[] args) {
solve();
}
}