Last active
December 3, 2017 15:04
-
-
Save incon/f444207283f961fa5579d10a3052412d to your computer and use it in GitHub Desktop.
This file contains 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
function createMemorySpiralPart1() { | |
map = [[5, 4, 3], [6, 1, 2], [7, 8, 9]]; | |
current = 9; | |
mapSize = 4; | |
row = 2; | |
location = 2; | |
while (current < input) { | |
current++; | |
position = { row, location }; | |
if (position.location === 0) { | |
if ( | |
position.row === 0 && | |
map[position.row].length === mapSize && | |
map[position.row + 1].length === mapSize | |
) { | |
map.push([]); | |
mapSize++; | |
} | |
if (map[position.row].length < mapSize) { | |
if (position.row === mapSize - 1) { | |
map[position.row].push(current); | |
location++; | |
} else { | |
map[position.row].unshift(current); | |
location = 0; | |
} | |
} else { | |
map[position.row + 1].unshift(current); | |
row++; | |
location = 0; | |
} | |
} | |
if (position.location !== 0) { | |
if ( | |
position.row == mapSize - 1 && | |
map[position.row].length === mapSize && | |
map[position.row - 1].length === mapSize | |
) { | |
mapSize++; | |
} | |
if (position.row !== 0) { | |
if (map[position.row].length < mapSize) { | |
map[position.row].push(current); | |
location++; | |
} else { | |
map[position.row - 1].push(current); | |
row--; | |
location++; | |
} | |
} else { | |
map.unshift([current]); | |
row = 0; | |
location = 0; | |
} | |
} | |
} | |
return map; | |
} | |
function findInMap(find, map) { | |
for (let r = 0; r < map.length; r++) { | |
for (let i = 0; i < map[r].length; i++) { | |
if (find === map[r][i]) { | |
return { | |
row: r, | |
location: i | |
}; | |
} | |
} | |
} | |
} | |
function createMemorySpiralPart2() { | |
map = [[5, 4, 2], [10, 1, 1], [11, 23, 25]]; | |
lastValue = 25; | |
mapSize = 4; | |
nextValue = 0; | |
while (lastValue < input) { | |
position = findInMap(lastValue, map); | |
if (position.location === 0) { | |
if ( | |
position.row === 0 && | |
map[position.row].length === mapSize && | |
map[position.row + 1].length === mapSize | |
) { | |
map.push([]); | |
mapSize++; | |
} | |
if (map[position.row].length < mapSize) { | |
if (position.row === mapSize - 1) { | |
lastValue = | |
map[position.row][position.location] + | |
map[position.row - 1][position.location] + | |
map[position.row - 1][position.location + 1] + | |
map[position.row - 1][position.location + 2]; | |
map[position.row].push(lastValue); | |
} else { | |
offset = map[position.row + 1].length - map[position.row].length; | |
lastValue = | |
map[position.row][position.location] + | |
map[position.row + 1][offset] + | |
(offset - 1 >= 0 ? map[position.row + 1][offset - 1] : 0) + | |
(offset - 2 >= 0 ? map[position.row + 1][offset - 2] : 0); | |
map[position.row].unshift(lastValue); | |
} | |
} else { | |
lastValue = | |
map[position.row][position.location] + | |
map[position.row][position.location + 1] + | |
(position.row + 1 < mapSize - 1 | |
? map[position.row + 1][position.location] | |
: 0) + | |
(position.row + 2 < mapSize - 1 | |
? map[position.row + 2][position.location] | |
: 0); | |
map[position.row + 1].unshift(lastValue); | |
} | |
} | |
if (position.location !== 0) { | |
if ( | |
position.row == mapSize - 1 && | |
map[position.row].length === mapSize && | |
map[position.row - 1].length === mapSize | |
) { | |
mapSize++; | |
} | |
if (position.row !== 0) { | |
if (map[position.row].length < mapSize) { | |
offset = map[position.row - 1].length - 1 - position.location; | |
lastValue = | |
map[position.row][position.location] + | |
map[position.row - 1][position.location] + | |
(offset > 0 ? map[position.row - 1][position.location + 1] : 0) + | |
(offset > 1 ? map[position.row - 1][position.location + 2] : 0); | |
map[position.row].push(lastValue); | |
} else { | |
lastValue = | |
map[position.row][position.location] + | |
map[position.row][position.location - 1] + | |
map[position.row - 1][position.location - 1] + | |
(position.row - 2 >= 0 | |
? map[position.row - 2][position.location - 1] | |
: 0); | |
map[position.row - 1].push(lastValue); | |
} | |
} else { | |
lastValue = | |
map[position.row][position.location] + | |
map[position.row][position.location - 1]; | |
map.unshift([lastValue]); | |
} | |
} | |
} | |
return lastValue; | |
} | |
function findInMap(find, map) { | |
for (let r = 0; r < map.length; r++) { | |
for (let i = 0; i < map[r].length; i++) { | |
if (find === map[r][i]) { | |
return { | |
row: r, | |
location: i | |
}; | |
} | |
} | |
} | |
} | |
const input = 277678; | |
// Part 1 | |
console.log("Part 1"); | |
map = createMemorySpiralPart1(); | |
start = findInMap(1, map); | |
end = findInMap(277678, map); | |
console.log( | |
"Distance: " + | |
Math.abs(start.row - end.row + start.location - end.location + "\n") | |
); | |
// Part 2 | |
console.log("Part 2"); | |
console.log("Next highest:", createMemorySpiralPart2()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment