From 6f82d11e184409e6e27f364c99192c80a87addac Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sun, 7 Aug 2022 12:21:52 +0300 Subject: [PATCH 1/3] Add solution --- project_euler/problem_115/__init__.py | 0 project_euler/problem_115/sol1.py | 62 +++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 project_euler/problem_115/__init__.py create mode 100644 project_euler/problem_115/sol1.py diff --git a/project_euler/problem_115/__init__.py b/project_euler/problem_115/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_115/sol1.py b/project_euler/problem_115/sol1.py new file mode 100644 index 000000000000..1160c72c8444 --- /dev/null +++ b/project_euler/problem_115/sol1.py @@ -0,0 +1,62 @@ +""" +Project Euler Problem 115: https://projecteuler.net/problem=115 + +NOTE: This is a more difficult version of Problem 114 +(https://projecteuler.net/problem=114). + +A row measuring n units in length has red blocks +with a minimum length of m units placed on it, such that any two red blocks +(which are allowed to be different lengths) are separated by at least one black square. + +Let the fill-count function, F(m, n), +represent the number of ways that a row can be filled. + +For example, F(3, 29) = 673135 and F(3, 30) = 1089155. + +That is, for m = 3, it can be seen that n = 30 is the smallest value +for which the fill-count function first exceeds one million. + +In the same way, for m = 10, it can be verified that +F(10, 56) = 880711 and F(10, 57) = 1148904, so n = 57 is the least value +for which the fill-count function first exceeds one million. + +For m = 50, find the least value of n +for which the fill-count function first exceeds one million. +""" + +from itertools import count + + +def solution(m: int = 50) -> int: + """ + Returns for given m the least value of n + for which the fill-count function first exceeds one million + + >>> solution(3) + 30 + + >>> solution(10) + 57 + """ + + fill_count_functions = [1] * m + + for n in count(m): + fill_count_functions.append(1) + + for block_length in range(m, n + 1): + for block_start in range(n - block_length): + fill_count_functions[n] += fill_count_functions[ + n - block_start - block_length - 1 + ] + + fill_count_functions[n] += 1 + + if fill_count_functions[n] > 1_000_000: + break + + return n + + +if __name__ == "__main__": + print(f"{solution() = }") From 10639248ff8fb9c9726b5bb415bc245970018f70 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sun, 7 Aug 2022 12:51:18 +0300 Subject: [PATCH 2/3] Provide descriptive name for the parameter: m --- project_euler/problem_115/sol1.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/project_euler/problem_115/sol1.py b/project_euler/problem_115/sol1.py index 1160c72c8444..15a13516d54d 100644 --- a/project_euler/problem_115/sol1.py +++ b/project_euler/problem_115/sol1.py @@ -27,9 +27,9 @@ from itertools import count -def solution(m: int = 50) -> int: +def solution(min_block_length: int = 50) -> int: """ - Returns for given m the least value of n + Returns for given minimum block length the least value of n for which the fill-count function first exceeds one million >>> solution(3) @@ -39,12 +39,12 @@ def solution(m: int = 50) -> int: 57 """ - fill_count_functions = [1] * m + fill_count_functions = [1] * min_block_length - for n in count(m): + for n in count(min_block_length): fill_count_functions.append(1) - for block_length in range(m, n + 1): + for block_length in range(min_block_length, n + 1): for block_start in range(n - block_length): fill_count_functions[n] += fill_count_functions[ n - block_start - block_length - 1 From b468c3be9178c552eefcc4a7199241cf23f3b532 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 7 Aug 2022 09:51:52 +0000 Subject: [PATCH 3/3] updating DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 98b87f2fe279..b37bb35ec619 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -856,6 +856,8 @@ * [Sol1](project_euler/problem_113/sol1.py) * Problem 114 * [Sol1](project_euler/problem_114/sol1.py) + * Problem 115 + * [Sol1](project_euler/problem_115/sol1.py) * Problem 119 * [Sol1](project_euler/problem_119/sol1.py) * Problem 120