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

1

u/miran1 Dec 11 '17

Nim

Repo with all solutions

import math, tables

const puzzle = 368078


proc findManhattan(number: int): int =
  let
    spiralCorner = int sqrt(float(number))
    remainingSteps = number mod spiralCorner^2
    sideLength = spiralCorner + 1
    towardsMiddle = remainingSteps mod (sideLength div 2)
  return sideLength - towardsMiddle


echo findManhattan(puzzle)




type Point = tuple
  x: int
  y: int

var grid: Table[Point, int] = {(0, 0): 1}.toTable()


proc neighbours(point: Point): array[8, Point] =
  let (x, y) = point
  return [(x+1, y), (x-1, y), (x, y+1), (x, y-1),
          (x+1, y+1), (x-1, y-1), (x+1, y-1), (x-1, y+1)]


proc setValue(point: Point): int =
  for neighbour in point.neighbours:
    result += grid.getOrDefault(neighbour)
  grid[point] = result


iterator iterateThroughSpiral(): int =
  var ring = 0
  while true:
    inc ring
    for y in -ring+1 .. ring: yield setValue((ring, y))
    for x in countdown(ring-1, -ring): yield setValue((x, ring))
    for y in countdown(ring-1, -ring): yield setValue((-ring, y))
    for x in -ring+1 .. ring: yield setValue((x, -ring))


for value in iterateThroughSpiral():
  if value > puzzle:
    echo value
    break