summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2021-12-07 07:25:46 +0000
committerTomasz Kramkowski <tk@the-tk.com>2021-12-07 07:25:46 +0000
commitd7616fc88e78a9a1d6f7f2b912bd863e422e18e4 (patch)
treecf55b5eab6e4e2472c2a6fe91acd690100fcb96c
parent1af52e0bc147a86348636f39f9826e50b889b9d9 (diff)
downloadaoc2021-d7616fc88e78a9a1d6f7f2b912bd863e422e18e4.tar.gz
aoc2021-d7616fc88e78a9a1d6f7f2b912bd863e422e18e4.tar.xz
aoc2021-d7616fc88e78a9a1d6f7f2b912bd863e422e18e4.zip
day 7: python
-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))