day07: code beautification
This commit is contained in:
parent
4a45434585
commit
f4455f1c56
47
day07.py
47
day07.py
@ -1,24 +1,21 @@
|
||||
from functools import cache
|
||||
|
||||
from aoc import AOCDay
|
||||
import math
|
||||
from typing import Any
|
||||
from math import inf
|
||||
from tools.aoc import AOCDay
|
||||
from tools.int_seq import triangular
|
||||
from typing import Any, List
|
||||
|
||||
|
||||
@cache
|
||||
def getFuelForSteps(steps: int) -> int:
|
||||
return int(steps * (steps + 1) / 2)
|
||||
|
||||
|
||||
def getFuelUse(crabs, pos, exp=False):
|
||||
fuel = 0
|
||||
for c in crabs:
|
||||
if exp:
|
||||
fuel += getFuelForSteps(abs(pos - c))
|
||||
def getMinFuelUse(crabs: List[int], increased: bool = False) -> int:
|
||||
minFuel = inf
|
||||
for pos in range(min(crabs), max(crabs) + 1):
|
||||
if increased:
|
||||
fuel = sum(triangular(abs(pos - c)) for c in crabs)
|
||||
else:
|
||||
fuel += abs(pos - c)
|
||||
fuel = sum(abs(pos - c) for c in crabs)
|
||||
|
||||
return fuel
|
||||
if fuel < minFuel:
|
||||
minFuel = fuel
|
||||
|
||||
return minFuel
|
||||
|
||||
|
||||
class Day(AOCDay):
|
||||
@ -27,20 +24,8 @@ class Day(AOCDay):
|
||||
|
||||
def part1(self) -> Any:
|
||||
crabs = self.getInputAsArraySplit(",", int)
|
||||
minFuel = math.inf
|
||||
for pos in range(min(crabs), max(crabs) + 1):
|
||||
fuel = getFuelUse(crabs, pos)
|
||||
if fuel < minFuel:
|
||||
minFuel = fuel
|
||||
|
||||
return minFuel
|
||||
return getMinFuelUse(crabs)
|
||||
|
||||
def part2(self) -> Any:
|
||||
crabs = self.getInputAsArraySplit(",", int)
|
||||
minFuel = math.inf
|
||||
for pos in range(min(crabs), max(crabs) + 1):
|
||||
fuel = getFuelUse(crabs, pos, exp=True)
|
||||
if fuel < minFuel:
|
||||
minFuel = fuel
|
||||
|
||||
return minFuel
|
||||
return getMinFuelUse(crabs, True)
|
||||
|
||||
12
main.py
12
main.py
@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import aoc
|
||||
import tools.aoc
|
||||
import argparse
|
||||
import importlib
|
||||
import os
|
||||
@ -27,7 +27,7 @@ if flags.day:
|
||||
import_day = "%02d" % flags.day
|
||||
|
||||
imported = []
|
||||
for _, _, files in os.walk(aoc.BASE_PATH):
|
||||
for _, _, files in os.walk(tools.aoc.BASE_PATH):
|
||||
for f in files:
|
||||
if f.startswith('day' + import_day) and f.endswith('.py'):
|
||||
lib_name = f[:-3]
|
||||
@ -55,25 +55,25 @@ for lib in sorted(imported):
|
||||
if flags.test:
|
||||
day_class.test_part1()
|
||||
else:
|
||||
aoc.printSolution(day, 1, day_class.part1())
|
||||
tools.aoc.printSolution(day, 1, day_class.part1())
|
||||
else:
|
||||
exec_time = timeit.timeit(
|
||||
'day_class.part1()',
|
||||
globals=globals(),
|
||||
number=flags.timeit_number
|
||||
) / flags.timeit_number
|
||||
aoc.printSolution(day, 1, day_class.part1(), exec_time=exec_time)
|
||||
tools.aoc.printSolution(day, 1, day_class.part1(), exec_time=exec_time)
|
||||
|
||||
if not flags.part or flags.part == 2:
|
||||
if not flags.timeit:
|
||||
if flags.test:
|
||||
day_class.test_part2()
|
||||
else:
|
||||
aoc.printSolution(day, 2, day_class.part2())
|
||||
tools.aoc.printSolution(day, 2, day_class.part2())
|
||||
else:
|
||||
exec_time = timeit.timeit(
|
||||
'day_class.part2()',
|
||||
globals=globals(),
|
||||
number=flags.timeit_number
|
||||
) / flags.timeit_number
|
||||
aoc.printSolution(day, 2, day_class.part2(), exec_time=exec_time)
|
||||
tools.aoc.printSolution(day, 2, day_class.part2(), exec_time=exec_time)
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
from aoc import AOCDay
|
||||
from tools.aoc import AOCDay
|
||||
from typing import Any
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user