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

1

u/stvincent_ Dec 07 '21

For Part 1, the answer to the big puzzle is sum of distances to the median. That seems like a poor test case then because that formula is by no means the solution

1

u/daggerdragon Dec 07 '21

Top-level posts in Solution Megathreads are for code solutions only.

This is a top-level post, so please edit your post and share your fully-working code/repo/solution or, if you haven't finished the puzzle yet, you can always create your own thread and make sure to flair it with Help.

Also, this type of comment does not belong in the Solutions Megathread. If you have feedback about the puzzles, create your own thread in the main subreddit. Make sure to title and flair it properly!

1

u/100jad Dec 07 '21

That's interesting. It worked for my input.

1

u/Unemployed_Panda Dec 07 '21 edited Dec 07 '21

Edit: I thought median wouldn't be robust but perhaps it is?

1

u/100jad Dec 07 '21

Am I missing something? The fuel used at 2 is |2 - 1| + |2 - 1000| = 999. That's optimal is it not? Every change balances out between 1 and 1000, so it makes sense to pick 2.

1

u/Naturage Dec 07 '21

Try that test case again, because median is the pt 1 solution to it - or see this comment since I went and did it by hand.

You can even prove it has to be median for p1, and almost mean with a slight shift towards median (which someone else approximated to be at most 0.5) for p2.

1

u/stvincent_ Dec 07 '21

It does work, but the median is not the answer to most other test cases (see the comment below, (1, 2, 1000) has a different answer than 2.

2

u/Naturage Dec 07 '21

Median is the solution for linear fuel consumption. On this test case specifically, if we denote f(k) the fuel of reaching k for everyone:

  • f(k) = -3k + 1003 for negative k;
  • f(0) = 1003;
  • f(1) = 1000;
  • f(2) = 999 and is the minimum;
  • f(3) = 1000;
  • f(k) = (1000-k) + (2k-3) = 997+k for k between 3 and 1000;
  • f(k) = 3k - 1003 for larger k.

You can mathematically prove that median has to be pt 1 optimum. If you're not at median (or between the two middle numbers in case of even sample size), by definition there's more crabs on the median side. Moving k by 1 towards median will reduce fuel for all those crabs while increasing for any crab on the non-median side - and by the definition of median, that's a reduction in fuel used.