Project Euler 166 - Criss Cross

Official link: https://projecteuler.net/problem=166

Thought Process

Honestly, I did nothing special here. Same as basically everyone else, denote the values

a b c d
e f g h
i j k l
m n o p

Then I let:

  1. s = a + b + c + d

Then notice the following few equations

  1. s - a - e - i = m

  2. s - b - f - j = n

  3. s - c - g - k = o

  4. s - e - f - g = h

  5. s - i - j - k = l

  6. s - a - f - k = p

  7. and we can notice g = s - d - j - m

This brings us to only needing 9 variables

After each variable creation, just check that it's between 0 and 9 as a quick stopper and its done. There are many ways to make this much faster, Nayuki shows how to do it using 8 variables, some people on thread managed 7, Stephane used some tricks to make his code much faster, but these type of problems don't bring me much joy so I left it as is!

Interactive Code

No interactive code, my code is given below