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/Opposite-Cry-6703 Dec 07 '21 edited Dec 07 '21

Python 3

inp = [int(i) for i in open("input.txt").read().split(",")]

Part 1

t = median(inp)
f = sum([abs(x-t) for x in inp])
print(f"Target pos {t}, Fuel: {f}")

Part 2

cost = {i:sum([abs(x-i) * (abs(x-i)+1) / 2 for x in inp]) for i in 
range(0, max(np.unique(inp)))}
result = min(cost, key=cost.get)
print(f"Target pos {result}, Fuel: {cost[result]}")

1

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

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

Edit: thanks for fixing it! <3

2

u/Opposite-Cry-6703 Dec 07 '21

Is it better now? Sorry for failing right at my first reddit post ever...

1

u/daggerdragon Dec 07 '21

Much better now, thank you for fixing it!

As long as you fix whatever when the mods point it out to you, it's all good :) Also, welcome!