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

2

u/Porges Dec 04 '17 edited Dec 04 '17

Clunky Dyalog APL solution:

{ n←⍡ β‹„ arr←{(⌽(Γ—/⍴⍡)+β³βŠƒβ΄β΅)βͺβŒ½β‰β΅}⍣{n∊⍺}βͺ1 β‹„ x←|⌊(arr[1;]⍳n)-((2⌷⍴arr)Γ·2) β‹„ yβ†βŒŠ((1⌷⍴arr)Γ·2) β‹„ x+y }

The spiral generation is kind of fun:

{(⌽(Γ—/⍴⍡)+β³βŠƒβ΄β΅)βͺβŒ½β‰β΅}⍣{n∊⍺}βͺ1

βͺ1 take an array consisting of just '1'
{...}⍣{n∊⍺} ... until `n` is in the array,
  βŒ½β‰β΅ rotate the array clockwise, then
    β³βŠƒβ΄β΅ generate an array of the numbers from 1..'number of rows'
    (Γ—/⍴⍡)+ offset those numbers by the number of items in the array (product of number of rows and columns),
    ⌽ reverse the numbers
  βͺ and concatenate them to the top of the array

Then the rest of it is working out the x/y offset based on the fact that the number must be in the top row.

I'm much happier with my Part 2 solution:

{n←⍡ β‹„ {⌊/(⍡>n)/⍡} , {row ← ⍡[1;] β‹„ βŠ–β‰ (+\ (1↓row, 0) + row + (0, Β―1↓row)) βͺ ⍡} ⍣ {∨/ ,⍺>n} βͺ1}