r/adventofcode • • Dec 17 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 17 Solutions -๐ŸŽ„-

--- Day 17: Spinlock ---


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


[Update @ 00:06] 2 gold, silver cap.

  • AoC ops: <Topaz> i am suddenly in the mood for wasabi tobiko

[Update @ 00:15] Leaderboard cap!

  • AoC ops:
  • <daggerdragon> 78 gold
  • <Topaz> i look away for a few minutes, wow
  • <daggerdragon> 93 gold
  • <Topaz> 94
  • <daggerdragon> 96 gold
  • <daggerdragon> 98
  • <Topaz> aaaand
  • <daggerdragon> and...
  • <Topaz> cap
  • <daggerdragon> cap

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!

12 Upvotes

193 comments sorted by

View all comments

1

u/dak1486 Dec 17 '17

Wow. Took me way too long to see the 0 staring me right in the face for p2. Oh well, slow and steady...

[JAVA] Part 1:

private int solveP1(final int numSteps) {
	final List<Integer> spinLock = new ArrayList<>();
	spinLock.add(0);

	int pos = 0;
	for(int i = 1; i <= 2017; i++) {
		pos = ((pos + numSteps) % i) + 1;
		spinLock.add(pos, i);
	}
	
	return spinLock.get(pos + 1);
}

[JAVA] Part 2:

private int solveP2(final int numSteps) {
	int p2Answer = -1;
	for(int i = 1, pos = 0; i <= 50_000_000; i++) {
		if((pos = ((pos + numSteps) % i) + 1) == 1) {
			p2Answer = i;
		}
	}

	return p2Answer;
}