Skip to content

Instantly share code, notes, and snippets.

@incon
Last active December 3, 2017 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save incon/f444207283f961fa5579d10a3052412d to your computer and use it in GitHub Desktop.
Save incon/f444207283f961fa5579d10a3052412d to your computer and use it in GitHub Desktop.
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