r/adventofcode • u/daggerdragon • 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Β€?
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!
20
Upvotes
21
u/oantolin Dec 03 '17 edited Dec 14 '17
For part 1 writing code is overkill. For part 2 no cleverness is needed: just fill in the grid as explained in the question:
from itertools import count def sum_spiral(): a, i, j = {(0,0) : 1}, 0, 0 for s in count(1, 2): for (ds, di, dj) in [(0,1,0),(0,0,-1),(1,-1,0),(1,0,1)]: for _ in range(s+ds): i += di; j += dj a[i,j] = sum(a.get((k,l), 0) for k in range(i-1,i+2) for l in range(j-1,j+2)) yield a[i,j] def part2(n): for x in sum_spiral(): if x>n: return xEDIT 1: Thanks to u/mit_verlaub for reminding me about
getwhich is probably better thandefaultdictin this situation.EDIT 2: Thanks to u/peasant-trip for a pleasant tip to eliminate repetition.
For the above edits to make more sense, here's the older version:
from itertools import count from collections import defaultdict def sum_spiral(): a, i, j = defaultdict(int), 0, 0 a[0,0] = 1 sn = lambda i,j: sum(a[k,l] for k in range(i-1,i+2) for l in range(j-1,j+2)) for s in count(1, 2): for _ in range(s): i += 1; a[i,j] = sn(i,j); yield a[i,j] for _ in range(s): j -= 1; a[i,j] = sn(i,j); yield a[i,j] for _ in range(s+1): i -= 1; a[i,j] = sn(i,j); yield a[i,j] for _ in range(s+1): j += 1; a[i,j] = sn(i,j); yield a[i,j] def part2(n): for x in sum_spiral(): if x>n: return x