mirror of
https://github.com/Crocmagnon/advent-of-code.git
synced 2024-12-22 05:41:51 +01:00
Solve 2022 day 1
This commit is contained in:
parent
a1782c4cae
commit
cd3e1ecbc5
5 changed files with 2334 additions and 0 deletions
37
2022/day01_calories.py
Normal file
37
2022/day01_calories.py
Normal file
|
@ -0,0 +1,37 @@
|
|||
def main(filename: str, expected_part_1: int = None, expected_part_2: int = None):
|
||||
print(f"\n+ Running on {filename}")
|
||||
with open(filename) as f:
|
||||
data = f.read().strip().split("\n\n")
|
||||
|
||||
data = parse_data(data)
|
||||
solution_part_1 = solve_part_1(data)
|
||||
|
||||
print(f"1. Found {solution_part_1}")
|
||||
if expected_part_1:
|
||||
assert expected_part_1 == solution_part_1
|
||||
|
||||
solution_part_2 = solve_part_2(data)
|
||||
print(f"2. Found {solution_part_2}")
|
||||
if expected_part_2:
|
||||
assert expected_part_2 == solution_part_2
|
||||
|
||||
|
||||
def parse_data(data: list[str]) -> list[int]:
|
||||
split_lines = []
|
||||
for group in data:
|
||||
split_lines.append(list(map(int, group.split("\n"))))
|
||||
sums = [sum(group) for group in split_lines]
|
||||
return sums
|
||||
|
||||
|
||||
def solve_part_1(data: list[int]) -> int:
|
||||
return max(data)
|
||||
|
||||
|
||||
def solve_part_2(data: list[int]) -> int:
|
||||
return sum(sorted(data, reverse=True)[:3])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main("inputs/day01-test1", expected_part_1=24000, expected_part_2=45000)
|
||||
main("inputs/day01", expected_part_1=70296)
|
2236
2022/inputs/day01
Normal file
2236
2022/inputs/day01
Normal file
File diff suppressed because it is too large
Load diff
14
2022/inputs/day01-test1
Normal file
14
2022/inputs/day01-test1
Normal file
|
@ -0,0 +1,14 @@
|
|||
1000
|
||||
2000
|
||||
3000
|
||||
|
||||
4000
|
||||
|
||||
5000
|
||||
6000
|
||||
|
||||
7000
|
||||
8000
|
||||
9000
|
||||
|
||||
10000
|
14
2022/new_day
Executable file
14
2022/new_day
Executable file
|
@ -0,0 +1,14 @@
|
|||
#!/bin/bash
|
||||
set -euxo pipefail
|
||||
|
||||
if [[ $# -ne 2 ]]; then
|
||||
>&2 echo "Usage: $0 <number> <python_module_name>"
|
||||
exit 2
|
||||
fi
|
||||
|
||||
filename=day$1_$2.py
|
||||
cp template.py $filename
|
||||
sed -e "s/dayXX/day$1/g" -i "" ./$filename
|
||||
mkdir -p inputs
|
||||
touch inputs/day$1
|
||||
touch inputs/day$1-test1
|
33
2022/template.py
Normal file
33
2022/template.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
def main(filename: str, expected_part_1: int = None, expected_part_2: int = None):
|
||||
print(f"\n+ Running on {filename}")
|
||||
with open(filename) as f:
|
||||
data = f.read().strip().split("\n")
|
||||
|
||||
data = parse_data(data)
|
||||
solution_part_1 = solve_part_1(data)
|
||||
|
||||
print(f"1. Found {solution_part_1}")
|
||||
if expected_part_1:
|
||||
assert expected_part_1 == solution_part_1
|
||||
|
||||
solution_part_2 = solve_part_2(data)
|
||||
print(f"2. Found {solution_part_2}")
|
||||
if expected_part_2:
|
||||
assert expected_part_2 == solution_part_2
|
||||
|
||||
|
||||
def parse_data(data: list[str]):
|
||||
return data
|
||||
|
||||
|
||||
def solve_part_1(data) -> int:
|
||||
return 0
|
||||
|
||||
|
||||
def solve_part_2(data) -> int:
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main("inputs/dayXX-test1")
|
||||
main("inputs/dayXX")
|
Loading…
Reference in a new issue