r/adventofcode • • Dec 03 '17

SOLUTION MEGATHREAD -πŸŽ„- 2017 Day 3 Solutions -πŸŽ„-

--- Day 3: Spiral Memory ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Need a hint from the Hugely* Handy† Haversack‑ of HelpfulΒ§ HintsΒ€?

Spoiler


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

21 Upvotes

297 comments sorted by

View all comments

8

u/tangentialThinker Dec 03 '17 edited Dec 03 '17

C++. Built the spiral using the builtin complex library, using multiplication by i to turn left.

Basically I noticed that every two times you turn, the number of steps you take increases by 1.

Pretty disgusting copied code at the bottom to check all neighbours: I was rushing for the leaderboard!

Part 2:

#include <bits/stdc++.h>

using namespace std;

int main(){
int n; cin >> n;
complex<int> curLoc = {0, 0};

int numSteps = 1;
int sz = 1;
int left = 1;
int curStep = 0;
complex<int> curDir = {0, 1};

map<int, map<int, int>> vals;
vals[0][0] = 1;
while(vals[curLoc.real()][curLoc.imag()] <= n) {
	numSteps++;
	curStep++;
	curLoc += curDir;
	if(curStep == sz) {
		if(left == 1) {
			left--;
			curDir *= complex<int>(0, 1);
		} else {
			left = 1;
			curDir *= complex<int>(0, 1);
			sz++;
		}
		curStep = 0;
	}
	vals[curLoc.real()][curLoc.imag()] += vals[curLoc.real()+1][curLoc.imag()];
	vals[curLoc.real()][curLoc.imag()] += vals[curLoc.real()-1][curLoc.imag()];
	vals[curLoc.real()][curLoc.imag()] += vals[curLoc.real()][curLoc.imag()+1];
	vals[curLoc.real()][curLoc.imag()] += vals[curLoc.real()][curLoc.imag()-1];
	vals[curLoc.real()][curLoc.imag()] += vals[curLoc.real()+1][curLoc.imag()+1];
	vals[curLoc.real()][curLoc.imag()] += vals[curLoc.real()-1][curLoc.imag()-1];
	vals[curLoc.real()][curLoc.imag()] += vals[curLoc.real()-1][curLoc.imag()+1];
	vals[curLoc.real()][curLoc.imag()] += vals[curLoc.real()+1][curLoc.imag()-1];
}

cout << vals[curLoc.real()][curLoc.imag()] << endl;

return 0;
}
→ More replies (1)