mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Implement 2019 day 1 with tests in Python
This commit is contained in:
0
2019/aoc2019/__init__.py
Normal file
0
2019/aoc2019/__init__.py
Normal file
28
2019/aoc2019/__main__.py
Normal file
28
2019/aoc2019/__main__.py
Normal file
@@ -0,0 +1,28 @@
|
||||
import argparse
|
||||
import importlib
|
||||
import sys
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('day', type=int)
|
||||
parser.add_argument('input', type=argparse.FileType('rt'), nargs='?', default=sys.stdin)
|
||||
parser.add_argument('-2', '--part2', action='store_true')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
day = importlib.import_module(f'.day{args.day:02d}', __package__)
|
||||
|
||||
if args.part2:
|
||||
function = day.part2
|
||||
else:
|
||||
function = day.part1
|
||||
|
||||
print(function(args.input))
|
||||
|
||||
except ImportError:
|
||||
sys.exit(f'Invalid day: {args.day}')
|
||||
|
||||
|
||||
main()
|
||||
23
2019/aoc2019/day01.py
Normal file
23
2019/aoc2019/day01.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from typing import TextIO
|
||||
|
||||
|
||||
def fuel_required(weight: int) -> int:
|
||||
return max(0, weight // 3 - 2)
|
||||
|
||||
|
||||
def recursive_fuel_required(weight: int) -> int:
|
||||
total = 0
|
||||
|
||||
while weight > 0:
|
||||
weight = fuel_required(weight)
|
||||
total += weight
|
||||
|
||||
return total
|
||||
|
||||
|
||||
def part1(data: TextIO) -> int:
|
||||
return sum(fuel_required(int(line)) for line in data)
|
||||
|
||||
|
||||
def part2(data: TextIO) -> int:
|
||||
return sum(recursive_fuel_required(int(line)) for line in data)
|
||||
Reference in New Issue
Block a user