Skip to content

Instantly share code, notes, and snippets.

@martoo6
Last active March 20, 2019 20:23
Show Gist options
  • Save martoo6/71f81aec1c18ccdd3cb69cc4d78b8b6e to your computer and use it in GitHub Desktop.
Save martoo6/71f81aec1c18ccdd3cb69cc4d78b8b6e to your computer and use it in GitHub Desktop.
Dan Code Challenge
void setup(){
long code1 = 100100441;
long code2 = 100100444;
long x1l1 = code1/1000000;
long y1l1 = (code1 - (code1/1000000)*1000000 )/1000;
long x1l2 = (code1 - (code1/1000)*1000) /100;
long y1l2 = (code1 - (code1/100)*100) /10;
//The square is like this:
// _ _
// |3|4|
// |-|-|
// |1|2|
// - -
long lastDigit1 = code1 - (code1/10)*10;
long x1l3 = lastDigit1 % 2 == 0 ? 5 : 0;
long y1l3 = lastDigit1 < 3 ? 0 : 5;
long x2l1 = floor(code2/1000000);
long y2l1 = (code2 - (code2/1000000)*1000000 )/1000;
long x2l2 = (code2 - (code2/1000)*1000) /100;
long y2l2 = (code2 - (code2/100)*100) /10;
long lastDigit2 = code2 - (code2/10)*10;
long x2l3 = lastDigit2 % 2 == 0 ? 5 : 0;
long y2l3 = lastDigit2 < 3 ? 0 : 5;
long x1 = x1l1*100 + x1l2*10 + x1l3;
long x2 = x2l1*100 + x2l2*10 + x2l3;
long y1 = y1l1*100 + y1l2*10 + y1l3;
long y2 = y2l1*100 + y2l2*10 + y2l3;
long xMax = Math.max(x1, x2);
long xMin = Math.min(x1, x2);
long yMax = Math.max(y1, y2);
long yMin = Math.min(y1, y2);
println("X: [" + xMin + ", " + xMax + "]" );
println("Y: [" + yMin + ", " + yMax + "]" );
for(long x=xMin; x<=xMax;x+=5) {
for(long y=yMin; y<=yMax;y+=5) {
println(toCode(x, y));
}
}
}
long toCode(long x, long y){
long result = 1;
long xl1 = x / 100;
long xl2 = (x - xl1*100) / 10;
long xl3 = (x - (x/10)*10) == 5 ? 1 : 0;
result+= xl1 * 1000000 + xl2 * 100 + xl3;
long yl1 = y / 100;
long yl2 = (y - yl1*100) / 10;
long yl3 = (y - (y/10)*10) == 5 ? 2 : 0;
result+= yl1 * 1000 + yl2 * 10 + yl3;
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment