Do day 1 and 2.
This commit is contained in:
64
d1.py
Normal file
64
d1.py
Normal file
@@ -0,0 +1,64 @@
|
||||
import re
|
||||
|
||||
EXAMPLE = """
|
||||
1000
|
||||
2000
|
||||
3000
|
||||
|
||||
4000
|
||||
|
||||
5000
|
||||
6000
|
||||
|
||||
7000
|
||||
8000
|
||||
9000
|
||||
|
||||
10000
|
||||
"""
|
||||
|
||||
def solve(lines: list[str]):
|
||||
counter = 0
|
||||
count_max = 0
|
||||
for line in lines.splitlines():
|
||||
if line.strip() == "":
|
||||
if counter >= count_max:
|
||||
count_max = counter
|
||||
counter = 0
|
||||
else:
|
||||
counter += int(line)
|
||||
return count_max
|
||||
|
||||
def solve2(lines: list[str]):
|
||||
counter = 0
|
||||
count_max = 0
|
||||
counts = []
|
||||
for line in lines.splitlines():
|
||||
if line.strip() == "":
|
||||
if counter >= count_max:
|
||||
count_max = counter
|
||||
counts.append(counter)
|
||||
counter = 0
|
||||
else:
|
||||
counter += int(line)
|
||||
counts.append(counter)
|
||||
|
||||
counts.sort(reverse=True)
|
||||
return sum(counts[:3])
|
||||
|
||||
def main():
|
||||
example = str(EXAMPLE)
|
||||
print("Example 1:", solve(example))
|
||||
|
||||
data = open("i1.txt").read()
|
||||
print("Solution 1:", solve(data))
|
||||
|
||||
example = str(EXAMPLE)
|
||||
print("Example 2:", solve2(example))
|
||||
|
||||
data = open("i1.txt").read()
|
||||
print("Solution 2:", solve2(data))
|
||||
return
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user