-
Notifications
You must be signed in to change notification settings - Fork 0
/
day7.py
28 lines (21 loc) · 925 Bytes
/
day7.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import numpy as np
import pandas as pd
def determine_alignment_constant(_input):
positions = pd.Series(_input)
alignment = int(positions.median())
fuel = (positions - alignment).abs().sum()
return alignment, fuel
def fuel_func(alignment, positions):
return (positions - alignment).abs().apply(lambda x: int(x*(x+1)/2)).sum()
def determine_alignment(_input):
positions = pd.Series(_input)
domain = range(min(positions), max(positions)+1)
costs = [fuel_func(alignment, positions) for alignment in domain]
min_cost = min(costs)
alignment = costs.index(min_cost) + domain[0]
return alignment, min_cost
def run(_input):
alignment, fuel = determine_alignment_constant(_input)
print('Day 7, Part 1: Alignment {}, Fuel: {}'.format(alignment, fuel))
alignment, fuel = determine_alignment(_input)
print('Day 7, Part 2: Alignment {}, Fuel: {}'.format(alignment, fuel))