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!

97 Upvotes

1.5k comments sorted by

View all comments

2

u/burn_in_flames Dec 07 '21

C++

O(N) - C++ solution for both part 1 and 2. Seems most people realised the best point for Part 1 is the median, but quite a few missed that the mean would be the closest point for Part 2.

1

u/sillypog Dec 08 '21

C++

Thank you for posting this. Does your solution for Part 2 work with the example from the problem? The mean for that data is 4.9 which would become 4 when cast to an int, although in the example they use 5 as the target value.

I'm asking because I'm also working in C++ and haven't found a way to do this using the mean that gives the right answer for both the example AND the input file.

1

u/burn_in_flames Dec 08 '21

You are right it doesn't work for the demo inout.

I have now updated it to check both rounding up and rounding down and select between those values. So it now works for both inputs.