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