diff options
author | Tomasz Kramkowski <tk@the-tk.com> | 2021-12-07 07:25:46 +0000 |
---|---|---|
committer | Tomasz Kramkowski <tk@the-tk.com> | 2021-12-07 07:25:46 +0000 |
commit | d7616fc88e78a9a1d6f7f2b912bd863e422e18e4 (patch) | |
tree | cf55b5eab6e4e2472c2a6fe91acd690100fcb96c | |
parent | 1af52e0bc147a86348636f39f9826e50b889b9d9 (diff) | |
download | aoc2021-d7616fc88e78a9a1d6f7f2b912bd863e422e18e4.tar.gz aoc2021-d7616fc88e78a9a1d6f7f2b912bd863e422e18e4.tar.xz aoc2021-d7616fc88e78a9a1d6f7f2b912bd863e422e18e4.zip |
day 7: python
-rw-r--r-- | 7.py | 12 |
1 files changed, 12 insertions, 0 deletions
@@ -0,0 +1,12 @@ +from utils import open_day +from statistics import median + +crabs = list(map(int, open_day(7).read().split(','))) + +p = int(median(sorted(crabs))) +print(sum(abs(n - p) for n in crabs)) +s = sum(crabs) +l = len(crabs) +t = (s + sum(crab * l > s for crab in crabs)) // l +def sigma_n(n): return n * (n + 1) // 2 +print(sum(sigma_n(abs(n - t)) for n in crabs)) |