diff --git a/day-07/day-07.py b/day-07/day-07.py new file mode 100644 index 0000000..6829aae --- /dev/null +++ b/day-07/day-07.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 +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): + fuel = 0 + for c in crabs: + d = abs(h - c) + fuel += d * (d + 1) // 2 + return fuel + + +def part_1(input): + result = 0 + c_pos = [int(x) for x in input[0].split(',')] + result = calc_fuel(c_pos, c_pos[0]) + for i in range(min(c_pos), max(c_pos) + 1): + result = min(result, calc_fuel(c_pos, i)) + print("Part 1 result:", result) + + +def part_2(input): + result = 0 + c_pos = [int(x) for x in input[0].split(',')] + result = calc_fuel_exp(c_pos, c_pos[0]) + for i in range(min(c_pos), max(c_pos) + 1): + result = min(result, calc_fuel_exp(c_pos, i)) + print("Part 2 result:", result) + + +input = list() +p = Path(__file__).with_name('input.txt') +with open(p) as f: + input = f.readlines() + part_1(input) + part_2(input)