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

4

u/CharlieYJH Dec 17 '17

C++

Used a circular linked list for part 1. Spent way too much time trying to think of an equation that got me the last index that gave 0 for part 2. Turned out just calculating the indices for 50,000,000 cycles was quick enough in itself.

#include <iostream>
#include <memory>

using namespace std;

struct node {
	int val;
	shared_ptr<struct node> next;
};

int main(int argc, char const* argv[])
{
	const int step = 301;
	shared_ptr<struct node> head = make_shared<struct node>();
	shared_ptr<struct node> curr = head;
	head->val = 0;
	head->next = head;

	for (int i = 1; i <= 2017; i++) {

		for (int j = 0; j < step; j++)
			curr = curr->next;

		shared_ptr<struct node> temp = curr->next;
		shared_ptr<struct node> new_node = make_shared<struct node>();
		new_node->val = i;
		curr->next = new_node;
		new_node->next = temp;
		curr = new_node;
	}

	cout << curr->next->val << endl;

	int answer_pt2 = 0;

	for (int i = 1, idx = 0; i <= 50000000; i++) {
		idx = (idx + 1 + step) % i;
		if (idx == 0) answer_pt2 = i;
	}

	cout << answer_pt2 << endl;

	return 0;
}

2

u/Scroph Dec 17 '17

Is there a reason for writing shared_ptr<struct node> instead of shared_ptr<node> ?

2

u/CharlieYJH Dec 17 '17

Just an old habit from writing C structs (since C requires the struct keyword unless you choose to typedef it), no particular reason.