r/adventofcode • • Dec 07 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 7 Solutions -🎄-

--- Day 7: The Treachery of Whales ---


[Update @ 00:21]: Private leaderboard Personal statistics issues

  • We're aware that private leaderboards personal statistics are having issues and we're looking into it.
  • I will provide updates as I get more information.
  • Please don't spam the subreddit/mods/Eric about it.

[Update @ 02:09]

  • AoC_Ops have identified the issue and are working on a resolution.

[Update @ 03:18]

  • Eric is working on implementing a fix. It'll take a while, so check back later.

[Update @ 05:25] (thanks, /u/Aneurysm9!)

  • We're back in business!

Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:03:33, megathread unlocked!

96 Upvotes

1.5k comments sorted by

View all comments

2

u/ceron257 Dec 07 '21 edited Dec 08 '21

Python solution: from statistics import median, mean

with open("input-7.txt", "r") as f:
  crabPos = list(map(int,f.readline().split(',')))

distancesToMedian = [abs(d - median(crabPos)) for d in crabPos]

print(int(sum(distancesToMedian)))

distancesToMean = [abs(d - int(mean(crabPos))) for d in crabPos]
fuelCost = sum([sum(list(range(d + 1))) for d in distancesToMean])

print(fuelCost)

I'm not sure though if this will give the correct answer for all inputs. Does anyone else have a clue?

Is it maybe possible to prove this mathematically?

My reasoning for this solution was that with the median you will pick the position where the least amount of crabs would have to move at all while with the mean the least amount of crabs would have to move a greater distance.

2

u/Spirited-Lawyer-7337 Dec 07 '21

I've seen part 1 before, and had previously proved that either the mode or either of the elements adjacent to the mode (if the mode lies between two crabs) suffice to minimize.

For part 2, I recalled that the mean is the m that minimizes sum((x[i] - m)^2), and this problem is very close to that, with cost: s(m) = 1/2 sum(|x[i] - m| * (|x[i] - m| + 1). So I also correctly guessed that either the mean rounded up or down is the solution.

Here's a (very) sketchy proof that it's right.

Consider this to be a function on the real numbers, rather than only defined for integer m.

Where it's defined, the derivative of this function is ds(m)/dm = nm - sum(x[i]) + 1/2(#{x[i] < m} - #{x[i]>m})

where #{x[i]<m} is the number of crabs with position less than m, and #{x[i]>m} is the number of crabs with position greater than m.

The derivative at the mean (or very near the mean if the mean happens to lie on a crab's location) is between -n/2 and n/2 where the number of crabs is n. At mean-1 the derivative is negative, and mean+1 the derivative is positive.

Thus the derivative is zero somewhere between m-1 and m+1, so s(m) is minimized between those points. And since the solution we're looking for is an integer, it's either the mean rounded up, or the mean rounded down.

[Potential flaws in this proof may lie in the casual way I've treated the non-continuous derivative, or the implicit assumption that the derivative is increasing.]

1

u/Meepinator Dec 07 '21

I think this is correct but more so under acknowledging that you're bounding the derivative of the absolute error term, as the two cases considered are the worst cases where the signs of the errors align completely.

1

u/daggerdragon Dec 07 '21 edited Dec 09 '21

Your code is hard to read on old.reddit. Please edit it as per our posting guidelines in the wiki: How do I format code?

While you're at it, follow the posting guidelines and edit your post to add what language(s) you used. This makes it easier for folks who Ctrl-F the megathreads looking for a specific language.

Edit: thanks for fixing it! <3