From 44e0e4ed7559bc409946a0f98e68f06a17d43d03 Mon Sep 17 00:00:00 2001 From: Seongbeom Park Date: Sat, 5 Mar 2022 01:33:12 +0900 Subject: [PATCH 1/5] request others/dodge-the-lasers --- others/dodge-the-lasers/constraints.txt | 21 ++++++++ others/dodge-the-lasers/readme.md | 68 +++++++++++++++++++++++++ others/dodge-the-lasers/solution.py | 2 + 3 files changed, 91 insertions(+) create mode 100644 others/dodge-the-lasers/constraints.txt create mode 100644 others/dodge-the-lasers/readme.md create mode 100644 others/dodge-the-lasers/solution.py diff --git a/others/dodge-the-lasers/constraints.txt b/others/dodge-the-lasers/constraints.txt new file mode 100644 index 0000000..f201987 --- /dev/null +++ b/others/dodge-the-lasers/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/others/dodge-the-lasers/readme.md b/others/dodge-the-lasers/readme.md new file mode 100644 index 0000000..d4fc886 --- /dev/null +++ b/others/dodge-the-lasers/readme.md @@ -0,0 +1,68 @@ +Dodge the Lasers! +================= + +Oh no! You've managed to escape Commander Lambda's collapsing space station in an escape pod with the rescued bunny workers - but Commander Lambda isnt about to let you get away that easily. Lambda sent an elite fighter pilot squadron after you -- and they've opened fire! + +Fortunately, you know something important about the ships trying to shoot you down. Back when you were still Lambda's assistant, the Commander asked you to help program the aiming mechanisms for the starfighters. They undergo rigorous testing procedures, but you were still able to slip in a subtle bug. The software works as a time step simulation: if it is tracking a target that is accelerating away at 45 degrees, the software will consider the targets acceleration to be equal to the square root of 2, adding the calculated result to the targets end velocity at each timestep. However, thanks to your bug, instead of storing the result with proper precision, it will be truncated to an integer before adding the new velocity to your current position. This means that instead of having your correct position, the targeting software will erringly report your position as sum(i=1..n, floor(i*sqrt(2))) - not far enough off to fail Commander Lambdas testing, but enough that it might just save your life. + +If you can quickly calculate the target of the starfighters' laser beams to know how far off they'll be, you can trick them into shooting an asteroid, releasing dust, and concealing the rest of your escape. Write a function solution(str_n) which, given the string representation of an integer n, returns the sum of (floor(1*sqrt(2)) + floor(2*sqrt(2)) + ... + floor(n*sqrt(2))) as a string. That is, for every number i in the range 1 to n, it adds up all of the integer portions of i*sqrt(2). + +For example, if str_n was "5", the solution would be calculated as +floor(1*sqrt(2)) + +floor(2*sqrt(2)) + +floor(3*sqrt(2)) + +floor(4*sqrt(2)) + +floor(5*sqrt(2)) += 1+2+4+5+7 = 19 +so the function would return "19". + +str_n will be a positive integer between 1 and 10^100, inclusive. Since n can be very large (up to 101 digits!), using just sqrt(2) and a loop won't work. Sometimes, it's easier to take a step back and concentrate not on what you have in front of you, but on what you don't. +Fortunately, you know something important about the ships trying to shoot you down. Back when you were still Commander Lambdas assistant, she asked you to help program the aiming mechanisms for the starfighters. They undergo rigorous testing procedures, but you were still able to slip in a subtle bug. The software works as a time step simulation: if it is tracking a target that is accelerating away at 45 degrees, the software will consider the targets acceleration to be equal to the square root of 2, adding the calculated result to the targets end velocity at each timestep. However, thanks to your bug, instead of storing the result with proper precision, it will be truncated to an integer before adding the new velocity to your current position. This means that instead of having your correct position, the targeting software will erringly report your position as sum(i=1..n, floor(i*sqrt(2))) - not far enough off to fail Commander Lambdas testing, but enough that it might just save your life. + +If you can quickly calculate the target of the starfighters' laser beams to know how far off they'll be, you can trick them into shooting an asteroid, releasing dust, and concealing the rest of your escape. Write a function solution(str_n) which, given the string representation of an integer n, returns the sum of (floor(1*sqrt(2)) + floor(2*sqrt(2)) + ... + floor(n*sqrt(2))) as a string. That is, for every number i in the range 1 to n, it adds up all of the integer portions of i*sqrt(2). + +For example, if str_n was "5", the solution would be calculated as +floor(1*sqrt(2)) + +floor(2*sqrt(2)) + +floor(3*sqrt(2)) + +floor(4*sqrt(2)) + +floor(5*sqrt(2)) += 1+2+4+5+7 = 19 +so the function would return "19". + +str_n will be a positive integer between 1 and 10^100, inclusive. Since n can be very large (up to 101 digits!), using just sqrt(2) and a loop won't work. Sometimes, it's easier to take a step back and concentrate not on what you have in front of you, but on what you don't. + +Languages +========= + +To provide a Java solution, edit Solution.java +To provide a Python solution, edit solution.py + +Test cases +========== +Your code should pass the following test cases. +Note that it may also be run against hidden test cases not shown here. + +-- Java cases -- +Input: +Solution.solution('77') +Output: + 4208 + +Input: +Solution.solution('5') +Output: + 19 + +-- Python cases -- +Input: +solution.solution('77') +Output: + 4208 + +Input: +solution.solution('5') +Output: + 19 + +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. diff --git a/others/dodge-the-lasers/solution.py b/others/dodge-the-lasers/solution.py new file mode 100644 index 0000000..1effa86 --- /dev/null +++ b/others/dodge-the-lasers/solution.py @@ -0,0 +1,2 @@ +def solution(s): + # Your code here -- 2.47.2 From 0500c95614d1c8addb029013f6161750eaa24902 Mon Sep 17 00:00:00 2001 From: Seongbeom Park Date: Sun, 6 Mar 2022 05:04:53 +0900 Subject: [PATCH 2/5] WIP: changing algorithm from linear to divide and conquer --- .../{readme.md => readme.txt} | 0 others/dodge-the-lasers/solution.py | 65 ++++++++++++++++++- 2 files changed, 64 insertions(+), 1 deletion(-) rename others/dodge-the-lasers/{readme.md => readme.txt} (100%) diff --git a/others/dodge-the-lasers/readme.md b/others/dodge-the-lasers/readme.txt similarity index 100% rename from others/dodge-the-lasers/readme.md rename to others/dodge-the-lasers/readme.txt diff --git a/others/dodge-the-lasers/solution.py b/others/dodge-the-lasers/solution.py index 1effa86..f51436e 100644 --- a/others/dodge-the-lasers/solution.py +++ b/others/dodge-the-lasers/solution.py @@ -1,2 +1,65 @@ +import math +from decimal import * + +getcontext().prec = 102 +sqrt_2 = Decimal(2).sqrt() + def solution(s): - # Your code here + total = 0 + for i in range(int(s)+1): + total += int(math.floor(i*sqrt_2)) + #print i, total + return str(total) + +def check_known_pattern(start, end, memo): + if '0'*(end-start+1) in memo: # TODO: checkk value in the memo + return '0'*(end-start+1) + return False + +def generate_dfs(start, end, memo): + pattern = check_known_pattern(start, end, memo) + if pattern: + yield pattern, memo[pattern] + else: + new_pattern = '' + new_value = 0 # TODO: calculate new_value + pivot = int(math.floor((start+end)/2)) # TODO: power of 2 is optimal + for pattern, value in generate_dfs(start, pivot, memo): + new_pattern += pattern + new_value += value + yield pattern, value + for pattern, value in generate_dfs(pivot + 1, end, memo): + new_pattern += pattern + new_value += value + yield pattern, value + memo[new_pattern] = new_value + +memo = {'0': 1, '1': 1} # TODO: upgrade memo +total = 0 +for pattern, value in generate_dfs(1, 5, memo): + total += value + print pattern, value +print total, memo + +exit(0) + + + +tests = [ + ['77', '4208'], + ['1', '1'], + ['2', '3'], + ['3', '7'], + ['4', '12'], + ['5', '19'], + ['6', '27'], + ['7', '36'], + ['8', '47'], + ['9', '59'], + ['10', '73'], + ] + + +for test in tests: + result = solution(test[0]) + print(test[0], result == test[1], result, test[1]) -- 2.47.2 From a6f644ad07c2ee7ec9f186b50692f9c3bfc48117 Mon Sep 17 00:00:00 2001 From: Seongbeom Park Date: Mon, 7 Mar 2022 18:39:24 +0900 Subject: [PATCH 3/5] add reference --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 119ed3d..3b6085e 100644 --- a/README.md +++ b/README.md @@ -30,3 +30,9 @@ ## Encrypted message CFcSBwgCCBoHRhkKU1cGAA4AGU5YQR5THBwNFwoGGAxTQQMQVBUSBg4EAAwQRhUQVBUHFAQTGRpT QQMQVBkPERkECQAWDVwXX1BGEwgJBAwCBFRVHQRGUlFBShwaDVZTGBUFVUdBShsVA1tZBwNGUlFB ShoVB1wXX1BGFAQOSklOQR5HGh5AVRY= + +## Others +### dodge-the-laser +* Reference + * [Beatty sequence](https://en.wikipedia.org/wiki/Beatty_sequence) + * How to find A001951 A Beatty sequence: a(n)=floor(n\*sqrt(2)), stackexchange, https://math.stackexchange.com/questions/2052179/how-to-find-sum-i-1n-left-lfloor-i-sqrt2-right-rfloor-a001951-a-beatty-s -- 2.47.2 From 3c7dd9524d5b1f079ba0c59f1d3662407caa81ca Mon Sep 17 00:00:00 2001 From: Seongbeom Park Date: Mon, 7 Mar 2022 21:09:50 +0900 Subject: [PATCH 4/5] use beatty sum Verifying solution... Test 1 passed! Test 2 passed! Test 3 passed! [Hidden] Test 4 failed [Hidden] Test 5 failed [Hidden] Test 6 failed [Hidden] Test 7 failed [Hidden] Test 8 failed [Hidden] Test 9 failed [Hidden] Test 10 failed [Hidden] --- others/dodge-the-lasers/solution.py | 54 +++++++++-------------------- 1 file changed, 16 insertions(+), 38 deletions(-) diff --git a/others/dodge-the-lasers/solution.py b/others/dodge-the-lasers/solution.py index f51436e..f3dc8df 100644 --- a/others/dodge-the-lasers/solution.py +++ b/others/dodge-the-lasers/solution.py @@ -1,49 +1,26 @@ import math from decimal import * -getcontext().prec = 102 -sqrt_2 = Decimal(2).sqrt() - def solution(s): - total = 0 - for i in range(int(s)+1): - total += int(math.floor(i*sqrt_2)) - #print i, total - return str(total) + getcontext().prec = 10000 + sqrt_2 = Decimal(2).sqrt() -def check_known_pattern(start, end, memo): - if '0'*(end-start+1) in memo: # TODO: checkk value in the memo - return '0'*(end-start+1) - return False + return str(beatty_sum(sqrt_2, int(s))) -def generate_dfs(start, end, memo): - pattern = check_known_pattern(start, end, memo) - if pattern: - yield pattern, memo[pattern] - else: - new_pattern = '' - new_value = 0 # TODO: calculate new_value - pivot = int(math.floor((start+end)/2)) # TODO: power of 2 is optimal - for pattern, value in generate_dfs(start, pivot, memo): - new_pattern += pattern - new_value += value - yield pattern, value - for pattern, value in generate_dfs(pivot + 1, end, memo): - new_pattern += pattern - new_value += value - yield pattern, value - memo[new_pattern] = new_value - -memo = {'0': 1, '1': 1} # TODO: upgrade memo -total = 0 -for pattern, value in generate_dfs(1, 5, memo): - total += value - print pattern, value -print total, memo - -exit(0) +def beatty_sum(r, n): + if n == 0: + return 0 + if n == 1: + return 1 + floor_r_n = int(math.floor(r*n)) + + return natural_number_sum(floor_r_n) - 2 * natural_number_sum(floor_r_n - n) - beatty_sum(r, floor_r_n - n) +def natural_number_sum(n): + return n*(n+1)/2 +def ten(n): + return 10**n tests = [ ['77', '4208'], @@ -57,6 +34,7 @@ tests = [ ['8', '47'], ['9', '59'], ['10', '73'], + [ten(100), '0'] ] -- 2.47.2 From ae631d3c0a44e76741a5cde1a1b80ee258923b70 Mon Sep 17 00:00:00 2001 From: Seongbeom Park Date: Mon, 7 Mar 2022 22:00:25 +0900 Subject: [PATCH 5/5] finish level5/dodge-the-lasers --- README.md | 9 +++++---- .../dodge-the-lasers/constraints.txt | 0 {others => level5}/dodge-the-lasers/readme.txt | 0 .../dodge-the-lasers/solution.py | 18 ++++++++---------- 4 files changed, 13 insertions(+), 14 deletions(-) rename {others => level5}/dodge-the-lasers/constraints.txt (100%) rename {others => level5}/dodge-the-lasers/readme.txt (100%) rename {others => level5}/dodge-the-lasers/solution.py (57%) diff --git a/README.md b/README.md index 3b6085e..487e9f3 100644 --- a/README.md +++ b/README.md @@ -28,11 +28,12 @@ * Completed in: 6 hrs, 49 mins, 26 secs. * DP & Optimization -## Encrypted message -CFcSBwgCCBoHRhkKU1cGAA4AGU5YQR5THBwNFwoGGAxTQQMQVBUSBg4EAAwQRhUQVBUHFAQTGRpT QQMQVBkPERkECQAWDVwXX1BGEwgJBAwCBFRVHQRGUlFBShwaDVZTGBUFVUdBShsVA1tZBwNGUlFB ShoVB1wXX1BGFAQOSklOQR5HGh5AVRY= - -## Others ### dodge-the-laser +* Completed in: 2 days, 20 hrs, 22 mins, 39 secs. * Reference * [Beatty sequence](https://en.wikipedia.org/wiki/Beatty_sequence) * How to find A001951 A Beatty sequence: a(n)=floor(n\*sqrt(2)), stackexchange, https://math.stackexchange.com/questions/2052179/how-to-find-sum-i-1n-left-lfloor-i-sqrt2-right-rfloor-a001951-a-beatty-s + +## Encrypted message +CFcSBwgCCBoHRhkKU1cGAA4AGU5YQR5THBwNFwoGGAxTQQMQVBUSBg4EAAwQRhUQVBUHFAQTGRpT QQMQVBkPERkECQAWDVwXX1BGEwgJBAwCBFRVHQRGUlFBShwaDVZTGBUFVUdBShsVA1tZBwNGUlFB ShoVB1wXX1BGFAQOSklOQR5HGh5AVRY= + diff --git a/others/dodge-the-lasers/constraints.txt b/level5/dodge-the-lasers/constraints.txt similarity index 100% rename from others/dodge-the-lasers/constraints.txt rename to level5/dodge-the-lasers/constraints.txt diff --git a/others/dodge-the-lasers/readme.txt b/level5/dodge-the-lasers/readme.txt similarity index 100% rename from others/dodge-the-lasers/readme.txt rename to level5/dodge-the-lasers/readme.txt diff --git a/others/dodge-the-lasers/solution.py b/level5/dodge-the-lasers/solution.py similarity index 57% rename from others/dodge-the-lasers/solution.py rename to level5/dodge-the-lasers/solution.py index f3dc8df..3891747 100644 --- a/others/dodge-the-lasers/solution.py +++ b/level5/dodge-the-lasers/solution.py @@ -2,25 +2,23 @@ import math from decimal import * def solution(s): - getcontext().prec = 10000 - sqrt_2 = Decimal(2).sqrt() + getcontext().prec = len(s)+1 + return str(beatty_sum_sqrt2(int(s))) - return str(beatty_sum(sqrt_2, int(s))) - -def beatty_sum(r, n): +def beatty_sum_sqrt2(n): if n == 0: return 0 if n == 1: return 1 - floor_r_n = int(math.floor(r*n)) - - return natural_number_sum(floor_r_n) - 2 * natural_number_sum(floor_r_n - n) - beatty_sum(r, floor_r_n - n) + floor_r_n = int(Decimal(2*(n**2)).sqrt()) + + return natural_number_sum(floor_r_n) - 2 * natural_number_sum(floor_r_n - n) - beatty_sum_sqrt2(floor_r_n - n) def natural_number_sum(n): return n*(n+1)/2 def ten(n): - return 10**n + return str(10**n) tests = [ ['77', '4208'], @@ -34,7 +32,7 @@ tests = [ ['8', '47'], ['9', '59'], ['10', '73'], - [ten(100), '0'] + [ten(100), '70710678118654752440084436210484903928483593768847403658833986899536623923105351942519376716382078638821760123411090095254685423841027253480565451739737157454059823250037671948325191776995310741236436'] ] -- 2.47.2