From a69d880bb5e9113ccf09aeaa31a570330f856417 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Sun, 7 Aug 2022 05:07:35 +0300 Subject: [PATCH] feat: add Project Euler problem 114 solution 1 (#6300) Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> --- DIRECTORY.md | 2 + project_euler/problem_114/__init__.py | 0 project_euler/problem_114/sol1.py | 58 +++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 project_euler/problem_114/__init__.py create mode 100644 project_euler/problem_114/sol1.py diff --git a/DIRECTORY.md b/DIRECTORY.md index 843ff77bb..98b87f2fe 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -854,6 +854,8 @@ * [Sol1](project_euler/problem_112/sol1.py) * Problem 113 * [Sol1](project_euler/problem_113/sol1.py) + * Problem 114 + * [Sol1](project_euler/problem_114/sol1.py) * Problem 119 * [Sol1](project_euler/problem_119/sol1.py) * Problem 120 diff --git a/project_euler/problem_114/__init__.py b/project_euler/problem_114/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/project_euler/problem_114/sol1.py b/project_euler/problem_114/sol1.py new file mode 100644 index 000000000..14d8092b2 --- /dev/null +++ b/project_euler/problem_114/sol1.py @@ -0,0 +1,58 @@ +""" +Project Euler Problem 114: https://projecteuler.net/problem=114 + +A row measuring seven units in length has red blocks with a minimum length +of three units placed on it, such that any two red blocks +(which are allowed to be different lengths) are separated by at least one grey square. +There are exactly seventeen ways of doing this. + + |g|g|g|g|g|g|g| |r,r,r|g|g|g|g| + + |g|r,r,r|g|g|g| |g|g|r,r,r|g|g| + + |g|g|g|r,r,r|g| |g|g|g|g|r,r,r| + + |r,r,r|g|r,r,r| |r,r,r,r|g|g|g| + + |g|r,r,r,r|g|g| |g|g|r,r,r,r|g| + + |g|g|g|r,r,r,r| |r,r,r,r,r|g|g| + + |g|r,r,r,r,r|g| |g|g|r,r,r,r,r| + + |r,r,r,r,r,r|g| |g|r,r,r,r,r,r| + + |r,r,r,r,r,r,r| + +How many ways can a row measuring fifty units in length be filled? + +NOTE: Although the example above does not lend itself to the possibility, +in general it is permitted to mix block sizes. For example, +on a row measuring eight units in length you could use red (3), grey (1), and red (4). +""" + + +def solution(length: int = 50) -> int: + """ + Returns the number of ways a row of the given length can be filled + + >>> solution(7) + 17 + """ + + ways_number = [1] * (length + 1) + + for row_length in range(3, length + 1): + for block_length in range(3, row_length + 1): + for block_start in range(row_length - block_length): + ways_number[row_length] += ways_number[ + row_length - block_start - block_length - 1 + ] + + ways_number[row_length] += 1 + + return ways_number[length] + + +if __name__ == "__main__": + print(f"{solution() = }")