aoc2022/d1.py

65 lines
1.1 KiB
Python
Raw Normal View History

2023-12-04 00:05:09 +01:00
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()