summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--7.py12
1 files changed, 12 insertions, 0 deletions
diff --git a/7.py b/7.py
new file mode 100644
index 0000000..9457068
--- /dev/null
+++ b/7.py
@@ -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))