This commit is contained in:
@@ -1,14 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
def calc_fuel(crabs, h):
|
|
||||||
fuel = 0
|
|
||||||
for c in crabs:
|
|
||||||
fuel += abs(h - c)
|
|
||||||
return fuel
|
|
||||||
|
|
||||||
|
|
||||||
def calc_fuel_exp(crabs, h):
|
def calc_fuel_exp(crabs, h):
|
||||||
fuel = 0
|
fuel = 0
|
||||||
for c in crabs:
|
for c in crabs:
|
||||||
@@ -16,19 +9,18 @@ def calc_fuel_exp(crabs, h):
|
|||||||
fuel += d * (d + 1) // 2
|
fuel += d * (d + 1) // 2
|
||||||
return fuel
|
return fuel
|
||||||
|
|
||||||
|
|
||||||
def part_1(input):
|
def part_1(input):
|
||||||
result = 0
|
result = 0
|
||||||
c_pos = [int(x) for x in input[0].split(',')]
|
c = [int(x) for x in input[0].strip().split(',')]
|
||||||
result = calc_fuel(c_pos, c_pos[0])
|
c.sort()
|
||||||
for i in range(min(c_pos), max(c_pos) + 1):
|
m = c[len(c) // 2]
|
||||||
result = min(result, calc_fuel(c_pos, i))
|
result = sum([abs(m - h) for h in c])
|
||||||
print("Part 1 result:", result)
|
print("Part 1 result:", result)
|
||||||
|
|
||||||
|
|
||||||
def part_2(input):
|
def part_2(input):
|
||||||
result = 0
|
result = 0
|
||||||
c_pos = [int(x) for x in input[0].split(',')]
|
c_pos = [int(x) for x in input[0].strip().split(',')]
|
||||||
result = calc_fuel_exp(c_pos, c_pos[0])
|
result = calc_fuel_exp(c_pos, c_pos[0])
|
||||||
for i in range(min(c_pos), max(c_pos) + 1):
|
for i in range(min(c_pos), max(c_pos) + 1):
|
||||||
result = min(result, calc_fuel_exp(c_pos, i))
|
result = min(result, calc_fuel_exp(c_pos, i))
|
||||||
|
|||||||
Reference in New Issue
Block a user