Skip to main content
4 of 14
Shortened by 14 bytes.
Donat
  • 1.5k
  • 7
  • 10

Haskell, 157 143 bytes

minimum[sum[1|7<-v]|v<-foldr(\x->([(x:),(7:)]<*>))[[]]z,e[sum$m(v!!)w|w<-m(p#)q++m(q#)p]]
m=map
p=[0..2];q=m(3*)p
a#x=m(x+)a
e(x:z)=and$m(x==)z

Try it online!

Takes a flat list of nine numbers.

Explanation

minimum[sum[1|7<-v]|                    -- count 7s for all solutions and take minimum
  v<-foldr(\x->([(x:),(7:)]<*>))[[]]z,  -- generate squares with all possible substitutions
  e[sum$m(v!!)w|w<-m(p#)q++m(q#)p]]     -- compute sums and check whether all are equal
m=map                                   -- shorter name for map
p=[0..2];q=m(3*)p                       -- indexes for first row and first column
a#x=m(x+)a                              -- make indexes for all rows or all colums
e(x:z)=and$m(x==)z                      -- check whether all numbers in list are equal
Donat
  • 1.5k
  • 7
  • 10