From dc50caa86016b7af1c05c15f70e7300950629b97 Mon Sep 17 00:00:00 2001 From: Seongbeom Park Date: Mon, 2 May 2022 02:02:36 +0900 Subject: [PATCH 1/2] request extra/fuel-injection-perfection --- .../fuel-injection-perfection/constraints.txt | 21 ++++++ extra/fuel-injection-perfection/readme.txt | 66 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 extra/fuel-injection-perfection/constraints.txt create mode 100644 extra/fuel-injection-perfection/readme.txt diff --git a/extra/fuel-injection-perfection/constraints.txt b/extra/fuel-injection-perfection/constraints.txt new file mode 100644 index 0000000..f201987 --- /dev/null +++ b/extra/fuel-injection-perfection/constraints.txt @@ -0,0 +1,21 @@ +Java +==== +Your code will be compiled using standard Java 8. All tests will be run by calling the solution() method inside the Solution class + +Execution time is limited. + +Wildcard imports and some specific classes are restricted (e.g. java.lang.ClassLoader). You will receive an error when you verify your solution if you have used a blacklisted class. + +Third-party libraries, input/output operations, spawning threads or processes and changes to the execution environment are not allowed. + +Your solution must be under 32000 characters in length including new lines and and other non-printing characters. + +Python +====== +Your code will run inside a Python 2.7.13 sandbox. All tests will be run by calling the solution() function. + +Standard libraries are supported except for bz2, crypt, fcntl, mmap, pwd, pyexpat, select, signal, termios, thread, time, unicodedata, zipimport, zlib. + +Input/output operations are not allowed. + +Your solution must be under 32000 characters in length including new lines and and other non-printing characters. diff --git a/extra/fuel-injection-perfection/readme.txt b/extra/fuel-injection-perfection/readme.txt new file mode 100644 index 0000000..a63c367 --- /dev/null +++ b/extra/fuel-injection-perfection/readme.txt @@ -0,0 +1,66 @@ +Fuel Injection Perfection +========================= + +Commander Lambda has asked for your help to refine the automatic quantum antimatter fuel injection system for the LAMBCHOP doomsday device. It's a great chance for you to get a closer look at the LAMBCHOP -- and maybe sneak in a bit of sabotage while you're at it -- so you took the job gladly. + +Quantum antimatter fuel comes in small pellets, which is convenient since the many moving parts of the LAMBCHOP each need to be fed fuel one pellet at a time. However, minions dump pellets in bulk into the fuel intake. You need to figure out the most efficient way to sort and shift the pellets down to a single pellet at a time. + +The fuel control mechanisms have three operations: + +1) Add one fuel pellet +2) Remove one fuel pellet +3) Divide the entire group of fuel pellets by 2 (due to the destructive energy released when a quantum antimatter pellet is cut in half, the safety controls will only allow this to happen if there is an even number of pellets) + +Write a function called solution(n) which takes a positive integer as a string and returns the minimum number of operations needed to transform the number of pellets to 1. The fuel intake control panel can only display a number up to 309 digits long, so there won't ever be more pellets than you can express in that many digits. + +For example: +solution(4) returns 2: 4 -> 2 -> 1 +solution(15) returns 5: 15 -> 16 -> 8 -> 4 -> 2 -> 1 +Quantum antimatter fuel comes in small pellets, which is convenient since the many moving parts of the LAMBCHOP each need to be fed fuel one pellet at a time. However, minions dump pellets in bulk into the fuel intake. You need to figure out the most efficient way to sort and shift the pellets down to a single pellet at a time. + +The fuel control mechanisms have three operations: + +1) Add one fuel pellet +2) Remove one fuel pellet +3) Divide the entire group of fuel pellets by 2 (due to the destructive energy released when a quantum antimatter pellet is cut in half, the safety controls will only allow this to happen if there is an even number of pellets) + +Write a function called solution(n) which takes a positive integer as a string and returns the minimum number of operations needed to transform the number of pellets to 1. The fuel intake control panel can only display a number up to 309 digits long, so there won't ever be more pellets than you can express in that many digits. + +For example: +solution(4) returns 2: 4 -> 2 -> 1 +solution(15) returns 5: 15 -> 16 -> 8 -> 4 -> 2 -> 1 + +Languages +========= + +To provide a Python solution, edit solution.py +To provide a Java solution, edit Solution.java + +Test cases +========== +Your code should pass the following test cases. +Note that it may also be run against hidden test cases not shown here. + +-- Python cases -- +Input: +solution.solution('15') +Output: + 5 + +Input: +solution.solution('4') +Output: + 2 + +-- Java cases -- +Input: +Solution.solution('4') +Output: + 2 + +Input: +Solution.solution('15') +Output: + 5 + +Use verify [file] to test your solution and see how it does. When you are finished editing your code, use submit [file] to submit your answer. If your solution passes the test cases, it will be removed from your home folder. -- 2.47.2 From 0850b752b219eedff779ea79029e572ad57eb291 Mon Sep 17 00:00:00 2001 From: Seongbeom Park Date: Mon, 2 May 2022 03:01:41 +0900 Subject: [PATCH 2/2] finish extra/fuel-injection-perfection --- README.md | 2 + extra/fuel-injection-perfection/solution.py | 74 +++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 extra/fuel-injection-perfection/solution.py diff --git a/README.md b/README.md index d76d93a..ba6252b 100644 --- a/README.md +++ b/README.md @@ -74,3 +74,5 @@ CFcSBwgCCBoHRhkKU1cGAA4AGU5YQR5THBwNFwoGGAxTQQMQVBUSBg4EAAwQRhUQVBUHFAQTGRpT QQM ### find-the-access-codes * Completed in: 1 hr, 6 mins, 52 secs. +### fuel-injection-perfection +* Completed in: 1 hr, 2 secs. diff --git a/extra/fuel-injection-perfection/solution.py b/extra/fuel-injection-perfection/solution.py new file mode 100644 index 0000000..a6a37d9 --- /dev/null +++ b/extra/fuel-injection-perfection/solution.py @@ -0,0 +1,74 @@ +def solution(n): + # 1. *0: /= 2 + # 2. *01: -= 1 + # 3. *11: += 1 + + # state 0: *0 + # state 1: *1 + # state 2: *11 + result = 0 + state = 0 + count = 0 + b = bin(int(n))[2:] + for c in bin(int(n))[:1:-1]: + if c == '0': + if state == 0: # 0* + count += 1 + elif state == 1: # 01 + result += 1 + 2 # remove one, divide by 2 twice + count = 0 + state = 0 # .*01 => .*00 => .* + elif state == 2: # 01* + result += 1 + count # add one, divide by 2 count times + count = 1 + state = 1 # .*01* => .*10* => .*1 + if c == '1': + if state == 0: # 10* + result += count # divide by 2 count times + count = 1 + state = 1 # .*10* => .*1 + elif state == 1: # 11 + count += 1 + state = 2 + elif state == 2: # 1* + count += 1 + if state == 2: # 11* + if count == 2: # 11 + result += 2 + else: + result += 1 + count # add one, devide by 2 count times + return result + + +tests = [ + ('15', 5), + ('4', 2), + (str(int('1', 2)), 0), + (str(int('10', 2)), 1), + (str(int('11', 2)), 2), + (str(int('100', 2)), 2), + (str(int('101', 2)), 3), + (str(int('110', 2)), 3), + (str(int('111', 2)), 4), + (str(int('1000', 2)), 3), + (str(int('1001', 2)), 4), + (str(int('1010', 2)), 4), + (str(int('1011', 2)), 6), + (str(int('1100', 2)), 4), + (str(int('1101', 2)), 5), + (str(int('1110', 2)), 5), + (str(int('1111', 2)), 5), + (str(int('10001', 2)), 5), + (str(int('111110', 2)), 7), + (str(int('101111', 2)), 7), + (str(int('1101111', 2)), 9), + (str(int('10001000', 2)), 8), + (str(int('11001100', 2)), 10), + (str(int('10101010', 2)), 10), + (str(int('11011000', 2)), 10), + (str(int('10000000', 2)), 7), + ] + +for i, o in tests: + result = solution(i) + print (i, result == o, result, o) -- 2.47.2