diff options
| author | n1c00o <git.n1c00o@gmail.com> | 2021-10-12 20:30:54 +0200 |
|---|---|---|
| committer | n1c00o <git.n1c00o@gmail.com> | 2021-10-12 20:30:54 +0200 |
| commit | f551a3af808e5b89b8744b8b3d77dee08ae63ce9 (patch) | |
| tree | 1e0fea2a6fbb800d691826b2128ea9f83e9ea383 /fuel-injection-perfection/n1c00o | |
| parent | 412d9f7739404b734e03c090b07e4ddfd8a1ee3f (diff) | |
Move my solution into a specific folder
Diffstat (limited to 'fuel-injection-perfection/n1c00o')
| -rw-r--r-- | fuel-injection-perfection/n1c00o/solution.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/fuel-injection-perfection/n1c00o/solution.py b/fuel-injection-perfection/n1c00o/solution.py new file mode 100644 index 0000000..388a739 --- /dev/null +++ b/fuel-injection-perfection/n1c00o/solution.py @@ -0,0 +1,38 @@ +def solution(str_n): + # 3 possibles operations: + # - Add one fuel + # - Remove one fuel + # - Divide fuel by 2 (allowed only if fuel is an even number) + fuel = int(str_n) + actions = 0 + + # Execute until we get 1 fuel remaining + while fuel != 1: + # If fuel is not an even number, we need to add or remove one fuel + if fuel % 2 != 0: + # We have to check if it is more interesting to add or remove one fuel, depending on the result of (fuel + 1) % 2 and (fuel - 1) + # i.e. 13 -> Adding 1 = 14 ; 14 // 2 = 7 ; we'd need to update 7 to get an even number + # 13 -> Removing 1 = 12 ; 12 // 2 = 6 ; we wouldn't need to add change 6 + + # If removing one is more efficient + if ((fuel - 1) // 2) % 2 == 0: + fuel -= 1 + actions += 1 + # Exception! If it results in 2, then it is more efficient to go from 3 to 2 + elif fuel - 1 == 2: + fuel -= 1 + actions += 1 + # if not, we add one + else: + fuel += 1 + actions += 1 + + # Consume fuel + fuel //= 2 + actions += 1 + + return actions + + +print(solution('4'), "should equal 2") +print(solution('15'), "should equal 5") |
