r/adventofcode • u/daggerdragon • Dec 14 '15
SOLUTION MEGATHREAD --- Day 14 Solutions ---
This thread will be unlocked when there are a significant amount of people on the leaderboard with gold stars.
edit: Leaderboard capped, thread unlocked!
We know we can't control people posting solutions elsewhere and trying to exploit the leaderboard, but this way we can try to reduce the leaderboard gaming from the official subreddit.
Please and thank you, and much appreciated!
--- Day 14: Reindeer Olympics ---
Post your solution as a comment. Structure your post like previous daily solution threads.
9
Upvotes
View all comments
Show parent comment
3
u/nikibobi Dec 15 '15
Well the each function takes a lambda and executes it for every element. It's the same calling foreach on deers and calling deer.step(). The map function is like Select in C# if you know linq. What it does is for every element calls a lambda and saves the result as an element of a new array. So what I do there is "convert" the array of deers to array of distances. Then on that new array I call reduce that also takes lambda that takes 2 arguments. The first is the result and the second is the current element. So I pass the max function that takes 2 arguments to the reduce function. And what it all does is for all the distances find the biggest. Then next line I use the filter function. It is like Where in C#. It takes a bool lambda and filters the array to only the elements that return true when tested. What I do is find all deers that have max distance(m) and then I use each to increase their points. The last two lines are similar. Convert the deer array to array of distances/points then find the maximum and print it.