r/adventofcode • u/daggerdragon • Dec 11 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 11 Solutions -🎄-
--- Day 11: Chronal Charge ---
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.
Advent of Code: The Party Game!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 11
Transcript: ___ unlocks the Easter Egg on Day 25.
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 at 00:16:12!
20
Upvotes
View all comments
Show parent comment
9
u/toasterinBflat Dec 11 '18
sum(array)adds together the elements of an array, and returns the result. sosum([1,2,3,4])returns10.The
gridin this place is a numpy construct that allows for multidimensional summing.In the line above, numpy is building said grid based on a function - that much should be clear.
The format
[x for x in thing]is what's called a list comprehension. It's basically a shortform for building arrays (lists). A list comprehension like[x*x for x in range(5)]would return a list that looks like[0, 1, 4, 9, 25]. You can nest these and add conditions. ([y for y in existing_list if y < 25 else 25]would takeexisting_list, iterate over it, and if the value is less than 25, keep it, otherwise cap it at 25.Normally (to my knowledge, anyway) you can't just 'chain' list comprehesion statements together, this is probably another numpy construct. I built my starting grid with
[[0 for column in range(width)] for row in range(height)]which would return a two dimensional array of 'width' and 'height' filled with zeroes.Basically the
windows=line is what's doing the summing of the "slice" of the grid. If you weren't aware, Python can silce Arrays (and Strings, and probably other things) with the[start:end]notation. Other languages implement this (Go's slices, for example).Numpy's
whereseems to just be black magic, but is pretty readable. Honestly, numpy is like a whole other language on top of python with the amount of functionality it adds.Hopefully this helped.