Skip to main content
14 of 14
deleted 395 characters in body
Donat
  • 1.5k
  • 7
  • 10

Haskell, 157 143 136 134 132 bytes

f z=minimum[sum[1|7<-v]|v<-foldr((<*>).(:[(7:)]).(:))[[]]z,e[sum$(v!!).((4-d)*x+).(d*)<$>l|d<-[1,3],x<-l]]
l=[0..2]
e(x:z)=all(x==)z

Try it online!

Takes a flat list of nine numbers.

Shortened by 11 bytes thanks to ovs (143 should have been 147).

Shortened additionally by 4 bytes.

Explanation

f z=minimum[sum[1|7<-v]|              -- count 7s for all solutions and take minimum
  v<-foldr((<*>).(:[(7:)]).(:))[[]]z, -- generate squares with all possible substitutions
  e                                   -- all sums must be equal
    [sum$                             -- compute sums of rows and columns
      (v!!).((4-d)*x+).(d*)<$>l|      -- transform [0..2] to index list and apply to v
        d<-[1,3],x<-l]]               -- parameters for index lists
l=[0..2]                              -- abbreviation, used in two places
e(x:z)=all(x==)z                      -- check whether all numbers in list are equal
Donat
  • 1.5k
  • 7
  • 10