-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_a.py
executable file
·63 lines (51 loc) · 1.63 KB
/
part_a.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
from dataclasses import dataclass
from typing import List
from aox.challenge import Debugger
from utils import BaseChallenge
class Challenge(BaseChallenge):
def solve(self, _input, debugger: Debugger):
"""
>>> Challenge().default_solve()
336721
"""
return CrabSet.from_crabs_text(_input).get_min_fuel_to_align()
@dataclass
class CrabSet:
crabs: List[int]
@classmethod
def from_crabs_text(cls, crabs_text: str) -> "CrabSet":
"""
>>> CrabSet.from_crabs_text("16,1,2,0,4,2,7,1,2,14")
CrabSet(crabs=[16, 1, 2, 0, 4, 2, 7, 1, 2, 14])
"""
return cls(crabs=list(map(int, crabs_text.split(","))))
def get_min_fuel_to_align(self) -> int:
"""
>>> CrabSet([16, 1, 2, 0, 4, 2, 7, 1, 2, 14]).get_min_fuel_to_align()
37
"""
closest_position = self.get_closest_position()
return self.get_distance(closest_position)
def get_closest_position(self) -> int:
"""
>>> CrabSet([16, 1, 2, 0, 4, 2, 7, 1, 2, 14]).get_closest_position()
2
"""
return min(
range(min(self.crabs), max(self.crabs) + 1),
key=self.get_distance,
)
def get_distance(self, position: int) -> int:
"""
>>> CrabSet([16, 1, 2, 0, 4, 2, 7, 1, 2, 14]).get_distance(2)
37
"""
return sum(
self.get_crab_distance(crab, position)
for crab in self.crabs
)
def get_crab_distance(self, crab: int, position: int) -> int:
return abs(crab - position)
Challenge.main()
challenge = Challenge()