Created
December 9, 2017 11:43
-
-
Save BallerIndustries/ab02581d8cbe7f40f035118a87216556 to your computer and use it in GitHub Desktop.
Advent of Code 2017 - Day 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var target = 289326 | |
var oddPowerOfTwo = Math.floor(Math.sqrt(target)); | |
if (oddPowerOfTwo % 2 === 0) { | |
oddPowerOfTwo--; | |
} | |
console.log('oddPowerOfTwo = ' + oddPowerOfTwo); | |
var verticalClimb = oddPowerOfTwo; | |
var horizontalClimb = oddPowerOfTwo + 1; | |
var currentPos = oddPowerOfTwo * oddPowerOfTwo; | |
var result = moveAround(currentPos, target, horizontalClimb, verticalClimb) | |
console.log('result ' + JSON.stringify(result)); | |
function moveAround(currentPos, targetPos, horizontalClimb, verticalClimb) { | |
var xDelta = 0; | |
var yDelta = 0; | |
// Move right one. | |
xDelta++; | |
currentPos++; | |
//var commands = ['UP', 'LEFT', 'DOWN', 'RIGHT']; | |
if (currentPos === targetPos) { | |
return { x: xDelta, y: yDelta }; | |
} | |
// Move up verticalClimb times | |
for (var i = 0; i < verticalClimb; i++) { | |
currentPos++; | |
yDelta--; | |
if (currentPos === targetPos) { | |
return { x: xDelta, y: yDelta }; | |
} | |
} | |
// Move right horizontalClimb times | |
for (var i = 0; i < horizontalClimb; i++) { | |
currentPos++; | |
xDelta--; | |
if (currentPos === targetPos) { | |
return { x: xDelta, y: yDelta }; | |
} | |
} | |
verticalClimb++; | |
// Move down verticalClimb + 1 times | |
for (var i = 0; i < verticalClimb; i++) { | |
currentPos++; | |
yDelta++; | |
if (currentPos === targetPos) { | |
return { x: xDelta, y: yDelta }; | |
} | |
} | |
horizontalClimb++; | |
// Move right horizontalClimb times | |
for (var i = 0; i < horizontalClimb; i++) { | |
currentPos++; | |
xDelta++; | |
if (currentPos === targetPos) { | |
return { x: xDelta, y: yDelta }; | |
} | |
} | |
throw "Failed to reach target position"; | |
} | |
//var x = 0; | |
//var y = 0; | |
//console.log(JSON.stringify(makeGrid(10, 10))); | |
// function printGrid(grid) { | |
// for (var i = 0; i < grid.length; i++) { | |
// for (var j = 0; j < grid[i].length; j++) { | |
// } | |
// } | |
// } | |
// function makeGrid(w, h) { | |
// var grid = []; | |
// for (var i = 0; i < w; i++) { | |
// var row = []; | |
// for (var j = 0; j < h; j++) { | |
// row.push(0); | |
// } | |
// grid.push(row); | |
// } | |
// return grid; | |
// } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment