diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-02-13 10:00:31 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-02-13 10:00:31 +0000 |
| commit | 670b384d26ab4fe7e38b4b264642a56efa9fe9f1 (patch) | |
| tree | 9b340a2ffa58a63e98d31abd7b33a487f58d3dae /challenge-151/sgreen/python | |
| parent | 0fc74df4cb758def1acbae228e15c66d0cc28232 (diff) | |
| parent | 93424d2f86a103e895912599f00b2e4049555708 (diff) | |
| download | perlweeklychallenge-club-670b384d26ab4fe7e38b4b264642a56efa9fe9f1.tar.gz perlweeklychallenge-club-670b384d26ab4fe7e38b4b264642a56efa9fe9f1.tar.bz2 perlweeklychallenge-club-670b384d26ab4fe7e38b4b264642a56efa9fe9f1.zip | |
Merge pull request #5645 from simongreen-net/master
sgreen solutions to challenge 151
Diffstat (limited to 'challenge-151/sgreen/python')
| -rwxr-xr-x | challenge-151/sgreen/python/ch-1.py | 31 | ||||
| -rwxr-xr-x | challenge-151/sgreen/python/ch-2.py | 28 |
2 files changed, 59 insertions, 0 deletions
diff --git a/challenge-151/sgreen/python/ch-1.py b/challenge-151/sgreen/python/ch-1.py new file mode 100755 index 0000000000..7c14e1f791 --- /dev/null +++ b/challenge-151/sgreen/python/ch-1.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +import sys + + +def main(s): + # Split the input on pipes + rows = s.split('|') + min_depth = 1 + + for row in rows[1:]: + # Split the row, and expand if missing values + v = row.strip().split() + while len(v) < 2 ** min_depth: + v.append('*') + + # Go through each pair, and exit if we find two '*' + for i in range(0, len(v), 2): + if v[i] == '*' and v[i+1] == '*': + break + else: + min_depth += 1 + continue + + break + + print(min_depth) + + +if __name__ == '__main__': + main(sys.argv[1]) diff --git a/challenge-151/sgreen/python/ch-2.py b/challenge-151/sgreen/python/ch-2.py new file mode 100755 index 0000000000..094f071037 --- /dev/null +++ b/challenge-151/sgreen/python/ch-2.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 + +import sys + + +def rob(haul, valuables): + if len(valuables) <= 2: + # We rob the remaining house, and take off with the haul! + return haul + valuables[0] + + # Call the function recursively skipping either one or two houses + hauls = [] + hauls.append(rob(haul+valuables[0], valuables[2:])) + if len(valuables) >= 4: + hauls.append(rob(haul+valuables[0], valuables[3:])) + + # Return the largest haul + return max(hauls) + + +def main(inputs): + valuables = list(map(int, inputs)) + largest_haul = rob(0, valuables) + print(largest_haul) + + +if __name__ == '__main__': + main(sys.argv[1:]) |
