add day 1
This commit is contained in:
commit
df0349791c
16 changed files with 2791 additions and 0 deletions
0
2022/src/__init__.py
Normal file
0
2022/src/__init__.py
Normal file
24
2022/src/day1/first.py
Normal file
24
2022/src/day1/first.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
def process_input(lines: list[str]) -> list[list[int]]:
|
||||
output = []
|
||||
block = []
|
||||
for line in lines:
|
||||
stripped = line.strip()
|
||||
if stripped.isnumeric():
|
||||
block.append(int(stripped))
|
||||
elif block:
|
||||
output.append(block)
|
||||
block = []
|
||||
return output
|
||||
|
||||
|
||||
def main(lines: list[str]) -> int:
|
||||
blocks = process_input(lines)
|
||||
block_sums = [sum(block) for block in blocks]
|
||||
return max(block_sums)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
with open("input.txt") as file:
|
||||
contents = file.readlines()
|
||||
solution = main(contents)
|
||||
print(f"Highest block sum: {solution}")
|
||||
2237
2022/src/day1/input.txt
Normal file
2237
2022/src/day1/input.txt
Normal file
File diff suppressed because it is too large
Load diff
27
2022/src/day1/second.py
Normal file
27
2022/src/day1/second.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
from first import process_input
|
||||
|
||||
|
||||
def maximum(input_list: list[int], top=3) -> list[int]:
|
||||
if top <= 0:
|
||||
raise RuntimeError(f"Argument top has to be >= 1! Got {top}.")
|
||||
top_elements = input_list[:top]
|
||||
for i in input_list[top:]:
|
||||
if any(i > element for element in top_elements):
|
||||
top_elements.append(i)
|
||||
if len(top_elements) > top:
|
||||
top_elements.sort(reverse=True)
|
||||
top_elements.pop()
|
||||
return top_elements
|
||||
|
||||
|
||||
def main(lines: list[str]) -> int:
|
||||
blocks = process_input(lines)
|
||||
block_sums = [sum(block) for block in blocks]
|
||||
return sum(maximum(block_sums))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
with open("input.txt") as file:
|
||||
contents = file.readlines()
|
||||
solution = main(contents)
|
||||
print(f"Highest block sum: {solution}")
|
||||
35
2022/src/day1/test_first.py
Normal file
35
2022/src/day1/test_first.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import pytest
|
||||
from first import main, process_input
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def example():
|
||||
return """
|
||||
1000
|
||||
2000
|
||||
3000
|
||||
|
||||
4000
|
||||
|
||||
5000
|
||||
6000
|
||||
|
||||
7000
|
||||
8000
|
||||
9000
|
||||
|
||||
10000
|
||||
""".splitlines()
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def blocks():
|
||||
return [[1000, 2000, 3000], [4000], [5000, 6000], [7000, 8000, 9000], [10000]]
|
||||
|
||||
|
||||
def test_process_input(example, blocks):
|
||||
assert process_input(example) == blocks
|
||||
|
||||
|
||||
def test_main(example):
|
||||
assert main(example) == 24000
|
||||
11
2022/src/day1/test_second.py
Normal file
11
2022/src/day1/test_second.py
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
from second import main, maximum
|
||||
from test_first import blocks, example
|
||||
|
||||
|
||||
def test_maximum(blocks):
|
||||
block_sums = [sum(block) for block in blocks]
|
||||
assert sorted(maximum(block_sums), reverse=True) == [24000, 11000, 10000]
|
||||
|
||||
|
||||
def test_main(example):
|
||||
assert main(example) == 45000
|
||||
Loading…
Add table
Add a link
Reference in a new issue