The explanation for this problem can be found @ http://www.cemc.uwaterloo.ca/contests/computing/2017/stage%201/juniorEF.pdf
(it's the third problem titled "exactly electrical").
def electrical(a,b,c,d,e) :
charge = e
x_dif = abs(c - a)
y_dif = abs(d - b)
total_dif = (x_dif + y_dif)
if (((total_dif) == charge) or ((total_dif % 2 == 0) and (charge %2 == 0)) or ((total_dif % 2 != 0) and (charge % 2 !=0))) :
return "Y"
else :
return "N"
print(electrical(10,2,10,4,5))
This code can also be found at https://repl.it/@erichasegawa/2017-CCC-Junior-S3.
I'm studying to write the Canadian Computing Competition this week, and I have a question about one of their algorithms; why will my function return "Y" when both the charge and distance are even or uneven, but if one is even and the other isn't (or vice versa) it returns false. I understand that this works, but I don't know why or how it works. If someone could explain this that would be great.