bro
This commit is contained in:
1 parent
de41a02749
commit
9af09cebd3
764 files changed
+54218
-78859
No files matched your search
@@ -0,0 +1,35 @@
|
||||
from functools import reduce
|
||||
|
||||
|
||||
def read_input(path):
|
||||
# Einzige Funktion mit Seiteneffekt (Dateizugriff)
|
||||
with open(path) as f:
|
||||
return f.read()
|
||||
|
||||
|
||||
def parse(data):
|
||||
# Text -> Liste von Gruppen -> Liste von Zahlenlisten
|
||||
# strip() entfernt den Zeilenumbruch am Dateiende
|
||||
groups = data.strip().split("\n\n")
|
||||
return list(map(lambda g: list(map(int, g.split("\n"))), groups))
|
||||
|
||||
|
||||
def group_sums(data):
|
||||
# Pro Gruppe die Summe bilden
|
||||
return list(map(sum, parse(data)))
|
||||
|
||||
|
||||
def max_calories(data):
|
||||
# Maximum mit reduce: immer den größeren von zwei Werten behalten
|
||||
return reduce(lambda a, b: a if a > b else b, group_sums(data))
|
||||
|
||||
|
||||
def top_three(data):
|
||||
# sorted erzeugt eine neue Liste, die Originalliste bleibt unverändert
|
||||
return sum(sorted(group_sums(data), reverse=True)[:3])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
data = read_input("beispiel.txt")
|
||||
print(max_calories(data)) # Teil 1
|
||||
print(top_three(data)) # Teil 2
|
||||
Reference in new issue
Block a user