-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7.py
34 lines (23 loc) · 1.05 KB
/
day7.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from common.day import Day
def compute_fuel_consumption_part_1(position: int, target_position: int) -> int:
return abs(position - target_position)
def compute_fuel_consumption_part_2(position: int, target_position: int) -> int:
return sum(range(1, abs(position - target_position) + 1))
class Day7(Day):
def part1(self) -> int:
return self.compute_min_fuel(compute_fuel_consumption_part_1)
def part2(self) -> int:
return self.compute_min_fuel(compute_fuel_consumption_part_2)
def compute_min_fuel(self, compute_fuel_consumption) -> int:
positions = self.read_input_integers_one_line()
self.logger.debug(positions)
target_positions = list(range(min(positions), max(positions)))
fuel_list = []
for tp in target_positions:
fuel = 0
for p in positions:
fuel += compute_fuel_consumption(p, tp)
fuel_list.append((tp, fuel))
min_fuel = min(fuel_list, key=lambda x: x[1])
self.logger.debug(min_fuel)
return min_fuel[1]