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

Show parent comment

1

u/willkill07 Dec 03 '17

I found the bug in my code. Essentially, I manage 8 โ€œspokesโ€ to determine where the userโ€™s input number would be. I determine that correctly but my logic was off when (1) the number is between spokes 0 and 7 (ascii figure below) and (2) when the number lies on a spoke. I also didnโ€™t do a check prior to the initial update, so any number within the first revolution wouldnโ€™t be calculated. Iโ€™ll work on correcting this.

3  2  1
 \ | /
  \|/
4โ€”-*โ€”-0
  /|\
 / | \
5  6  7

I like the closed-form solution for part 1. For part 2 I think Iโ€™m going to use an unordered_map<uint64_t,int> and build the spiral out with the key being (x << 32 | y) and the value bring the placed int. Create an efficient spiral generator and do neighbor checks in a nested loop. (About to get up and work on that )

1

u/gbeier Dec 03 '17

Nice. I like the approach.

I wish I'd read your reply before wrapping up my part 2 solution; that's the first I've heard of unordered_map and I wound up writing a pointless operator< for the struct I used as a key for my map. (The fact that the operator< is pointless is either the only comment in my code for this solution or very nearly so.)

Here's what I landed on, in a pastebin so as to make it easy to not look until you're finished if you prefer:

https://pastebin.com/U3JN58j1

I defined a structure to represent a point as (x,y) and used that as a key to map<point, int> then traverse the spiral populating the map, stopping when I've exceeded the test value.

1

u/[deleted] Dec 03 '17

hard to get that unordered_map to work. here's my solution with a map and pair<int,int> key. https://pastebin.com/2r0NzGj0

1

u/willkill07 Dec 04 '17

I got the unordered_map working pretty easily with an operator uint64_t() as part of the Point struct.

https://github.com/willkill07/AdventOfCode2017/blob/731e47170c80db9fceb6fd41746fa9a2f58e6f74/src/Day03.cpp#L27