Add 2021-07 in Python
Signed-off-by: AKU <tom@tdpain.net>
This commit is contained in:
parent
fa6be480d8
commit
371ee4bc3e
4 changed files with 92 additions and 0 deletions
2
challenges/2021/07-theTreacheryOfWhales/README.md
Normal file
2
challenges/2021/07-theTreacheryOfWhales/README.md
Normal file
|
@ -0,0 +1,2 @@
|
|||
# [Day <n>: <Day Title>](https://adventofcode.com/<year>/day/<n>)
|
||||
|
15
challenges/2021/07-theTreacheryOfWhales/benchmark.json
Normal file
15
challenges/2021/07-theTreacheryOfWhales/benchmark.json
Normal file
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"day": 7,
|
||||
"dir": "challenges/2021/07-theTreacheryOfWhales",
|
||||
"implementations": {
|
||||
"Python": {
|
||||
"part.1.avg": 0.0003524906635284424,
|
||||
"part.1.max": 0.0011348724365234375,
|
||||
"part.1.min": 0.00024008750915527344,
|
||||
"part.2.avg": 0.033716694355010984,
|
||||
"part.2.max": 0.08556342124938965,
|
||||
"part.2.min": 0.025210857391357422
|
||||
}
|
||||
},
|
||||
"numRuns": 1000
|
||||
}
|
17
challenges/2021/07-theTreacheryOfWhales/info.json
Normal file
17
challenges/2021/07-theTreacheryOfWhales/info.json
Normal file
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"inputFile": "input.txt",
|
||||
"testCases": {
|
||||
"one": [
|
||||
{
|
||||
"input": "16,1,2,0,4,2,7,1,2,14",
|
||||
"expected": "37"
|
||||
}
|
||||
],
|
||||
"two": [
|
||||
{
|
||||
"input": "16,1,2,0,4,2,7,1,2,14",
|
||||
"expected": "168"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
58
challenges/2021/07-theTreacheryOfWhales/py/__init__.py
Normal file
58
challenges/2021/07-theTreacheryOfWhales/py/__init__.py
Normal file
|
@ -0,0 +1,58 @@
|
|||
from typing import List
|
||||
from aocpy import BaseChallenge
|
||||
import math
|
||||
|
||||
|
||||
def parse(instr: str) -> List[int]:
|
||||
return list(map(int, instr.strip().split(",")))
|
||||
|
||||
|
||||
def increasing_fuel_sum_to_position(crabs: List[int], position: int) -> int:
|
||||
sigma = 0
|
||||
for crab in crabs:
|
||||
dist = abs(position - crab)
|
||||
sigma += sum(i + 1 for i in range(dist))
|
||||
return sigma
|
||||
|
||||
|
||||
def linear_fuel_sum_to_position(crabs: List[int], position: int) -> int:
|
||||
sigma = 0
|
||||
for crab in crabs:
|
||||
sigma += abs(position - crab)
|
||||
return sigma
|
||||
|
||||
|
||||
def median_distance(crabs: List[int]) -> int:
|
||||
crabs = list(sorted(crabs))
|
||||
num_crabs = len(crabs)
|
||||
n = None
|
||||
if num_crabs % 2 == 0: # is even, therefore interpolate :(
|
||||
high = crabs[int(num_crabs / 2)]
|
||||
low = crabs[int(num_crabs / 2) - 1]
|
||||
delta = high - low
|
||||
n = low + int(delta / 2)
|
||||
else: # odd, take raw middle value
|
||||
n = crabs[math.floor(num_crabs / 2)]
|
||||
return n
|
||||
|
||||
|
||||
def mean_distance(crabs: List[int]) -> float:
|
||||
return sum(crabs) / len(crabs)
|
||||
|
||||
|
||||
class Challenge(BaseChallenge):
|
||||
@staticmethod
|
||||
def one(instr: str) -> int:
|
||||
crab_distances = parse(instr)
|
||||
n = median_distance(crab_distances)
|
||||
return linear_fuel_sum_to_position(crab_distances, n)
|
||||
|
||||
@staticmethod
|
||||
def two(instr: str) -> int:
|
||||
crabs = parse(instr)
|
||||
n = mean_distance(crabs)
|
||||
|
||||
low = increasing_fuel_sum_to_position(crabs, math.floor(n))
|
||||
high = increasing_fuel_sum_to_position(crabs, math.ceil(n))
|
||||
|
||||
return min(low, high)
|
Loading…
Add table
Add a link
Reference in a new issue