Skip to content

Instantly share code, notes, and snippets.

@Grezzo
Created May 10, 2016 13:11
Show Gist options
  • Save Grezzo/d29143ee4216b955fea83b13fdaf8fc9 to your computer and use it in GitHub Desktop.
Save Grezzo/d29143ee4216b955fea83b13fdaf8fc9 to your computer and use it in GitHub Desktop.
Draws the solution to GC5Z9V4 in an HTML canvas using JavaScript
<!DOCTYPE html>
<html>
<body>
<canvas id="canv" width="200" height="130" style="border:solid; border-width:1px;"></canvas>
</body>
<script>
var instructions = "00006333 22533133 13323323 36223331 53333623 15362231 35332332 33133133 63133225 33133133 13323323 36233331 31536223
13153333 62353323 32336223 32533233 22633331 53333622 33132253 32336113 33332333 00000000 00000000 33333333 31536231 53333613 31533336 23332253
31331332 33233226 33315362 23131533 33263533 23323313 31336353 31331332 33233233 61323313 37444444.";
var x = 0;
var y = 20;
var direction = 0;
var draw = false;
var canv = document.getElementById("canv");
var ctx = canv.getContext("2d");
for (var i=0; i<instructions.length; i++) {
switch (parseInt(instructions.charAt(i))) {
case 1:
direction = (direction + 3) % 4;
break;
case 2:
direction = (direction + 1) % 4;
break;
case 3:
switch (direction) {
case 0:
x = x + 10;
break;
case 1:
y = y + 10;
break;
case 2:
x = x - 10;
break;
case 3:
y = y - 10;
break;
}
draw ? ctx.lineTo(x, y) : ctx.moveTo(x, y);
break;
case 5:
draw = true;
break;
case 6:
draw = false;
break;
}
}
ctx.setLineDash([2, 4]);
ctx.stroke();
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment