]> git.friedersdorff.com Git - max/advent_of_code_2021.git/commitdiff
Do day 7
authorMaximilian Friedersdorff <max@friedersdorff.com>
Tue, 7 Dec 2021 21:10:11 +0000 (21:10 +0000)
committerMaximilian Friedersdorff <max@friedersdorff.com>
Tue, 7 Dec 2021 21:10:11 +0000 (21:10 +0000)
7_1.py [new file with mode: 0644]
7_2.py [new file with mode: 0644]

diff --git a/7_1.py b/7_1.py
new file mode 100644 (file)
index 0000000..0c4557e
--- /dev/null
+++ b/7_1.py
@@ -0,0 +1,13 @@
+"""Find the least fuel for the sub-crabs"""
+with open("7_input.txt") as f:
+    crab_positions = [int(x) for x in f.read().split(",")]
+
+fuel_costs = []
+for target_position in range(max(crab_positions) + 1):
+    fuel_costs.append(0)
+    for crab_position in crab_positions:
+        fuel_costs[-1] += abs(target_position - crab_position)
+
+min_fuel = min(fuel_costs)
+min_fuel_position = fuel_costs.index(min_fuel)
+print(min_fuel_position, min_fuel)
diff --git a/7_2.py b/7_2.py
new file mode 100644 (file)
index 0000000..95592f7
--- /dev/null
+++ b/7_2.py
@@ -0,0 +1,14 @@
+"""Find the least fuel for the sub-crabs with triangle fuel consumption"""
+with open("7_input.txt") as f:
+    crab_positions = [int(x) for x in f.read().split(",")]
+
+fuel_costs = []
+for target_position in range(max(crab_positions) + 1):
+    fuel_costs.append(0)
+    for crab_position in crab_positions:
+        delta = abs(target_position - crab_position)
+        fuel_costs[-1] += delta * (delta + 1)/2
+
+min_fuel = min(fuel_costs)
+min_fuel_position = fuel_costs.index(min_fuel)
+print(min_fuel_position, min_fuel)