Calcudoku 5 × 5 5 \times 5 puzzle with multiple solutions

Logic Level 4

Consider the following 5 × 5 5 \times 5 Calcudoku puzzle:

This is a puzzle with multiple solutions. For the k k -th solution, let S k S_k be the sum of the entries on the main diagonal from the top left to the bottom right. As your answer enter k = 1 N S k \displaystyle \sum_{k = 1}^N S_k , where N N is the number of solutions.


The answer is 85.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

Piotr Idzik
Jun 28, 2020

My python code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
solution of:
    https://brilliant.org/problems/calcudoku-5-times-5-puzzle-with-multiple-solutions/
"""
import itertools
def check_conditions(in_data):
    """
    Verifies, if in_data (storing only the first n-rows) satisfies
    the "arithmetic" conditions of the puzzle
    https://brilliant.org/problems/calcudoku-5-times-5-puzzle-with-multiple-solutions/
    (for which only the first n-rows are needed).
    If the input data has the size n, it assumes,
    that the result of this function for the first n-1 "rows" was True.
    """
    res = True
    if len(in_data) == 2:
        res = in_data[0][0]*in_data[1][0]*in_data[0][1] == 60 and \
              in_data[0][2]*in_data[1][1]*in_data[1][2] == 8 and \
              in_data[0][3]*in_data[0][4]*in_data[1][4] == 15
    elif len(in_data) == 3:
        res = in_data[2][0]+in_data[2][1] == 7 and \
              in_data[1][3]*in_data[2][3]*in_data[2][2]*in_data[2][4] == 24
    elif len(in_data) == 4:
        res = in_data[3][0]+in_data[3][1]+in_data[3][2] == 9 and \
              abs(in_data[3][3]-in_data[3][4]) == 2
    elif len(in_data) == 5:
        res = in_data[4][0]+in_data[4][1]+in_data[4][2] == 7 and \
              in_data[4][3]+in_data[4][4] == 8
    return res

def gen_solution(in_size, in_check_arithmetic_cond):
    """
    generates all Latin squares of the order in_size, which
    satisfy the additional conditions described in in_check_arithmetic_cond
    """
    def are_all_different(in_list):
        """
        checks if all of the entries in in_list are different
        """
        return len(set(in_list)) == len(in_list)
    def add_row(cur_row_data):
        if len(cur_row_data) == in_size:
            yield tuple(cur_row_data)
        else:
            for tmp_row in itertools.permutations(range(1, in_size+1)):
                tmp_row_data = cur_row_data.copy()
                tmp_row_data.append(tmp_row)
                for col_num in range(in_size):
                    cur_col = [tmp_row_data[_][col_num] for _ in range(len(tmp_row_data))]
                    if not are_all_different(cur_col):
                        break
                else:
                    if in_check_arithmetic_cond(tmp_row_data):
                        for _ in add_row(tmp_row_data):
                            yield _
    for _ in add_row([]):
        yield _

def sol_to_str(sol_data):
    """
    returns a string representation of a solutions
    """
    return '\n'.join(' '.join(str(_) for _ in cur_row) for cur_row in sol_data)

PUZZLE_SIZE = 5
print("Solutions:")
RES_SUM = 0
for cur_sol in gen_solution(PUZZLE_SIZE, check_conditions):
    print(sol_to_str(cur_sol)+'\n')
    RES_SUM += sum(cur_sol[_][_] for _ in range(PUZZLE_SIZE))

print(f"Answer: {RES_SUM}")

Mark Hennings
Jun 12, 2020

The diagram shows the solutions and their derivation.

The first column shows the squares that are uniquely defined by the rules, and the numbers that have to go in the remaining regions.

The second column shows the consequences of the two choices for what goes in the fourth square on the top row (outlined), showing in red what squares are then determined by that choice, and the squares that remain to be determined.

The third and fourth columns show what happens, in each of the above two cases, when the middle square in the second row is chosen, showing the additional squares filled in in purple. In three out of four cases the grid is then complete. In the final case, there are still six squares to be filled, and the final column shows in blue how those final squares can be filled in.

Thus there are five solutions to the problem, with main diagonal sums of 20, 16, 18, 15, and 16, for a final total of 85 \boxed{85} .

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...