Code-Golf
Verfasst: Mittwoch 16. Oktober 2024, 18:26
Hallo,
falls jemand Langeweile hat, hier eine Funktion (`find_max_sum`) mit der man das vielleicht spielen könnte:
Ich überlege schon eine Weile, bin aber mal wieder fest gefahren.
Viel Spass
Dennis
falls jemand Langeweile hat, hier eine Funktion (`find_max_sum`) mit der man das vielleicht spielen könnte:
Code: Alles auswählen
import requests
from bs4 import BeautifulSoup
import re
URL = "https://projecteuler.net/problem=18"
def get_puzzle_input(url):
html_parser = BeautifulSoup(requests.get(url).text, "html.parser")
return [
content.text
for content in html_parser.find_all("p", {"class": "monospace center"})[1:]
]
def parse_input(content):
numbers = []
for line in content[0].splitlines():
matches = [int(match) for match in re.findall(r"\d+", line)]
if matches:
numbers.append(matches)
return list(reversed(numbers))
def find_max_sum(pyramid):
for row in range(len(pyramid)):
for column in range(len(pyramid[row]) - 1):
pyramid[row + 1][column] += max(
pyramid[row][column], pyramid[row][column + 1]
)
print(pyramid[-1][-1])
def main():
pyramid = parse_input(get_puzzle_input(URL))
find_max_sum(pyramid)
if __name__ == "__main__":
main()
Viel Spass
Dennis