Introduction
The Day 7 problem can be solved using bruteforce solution and searching in all possible numbers to find the minimal amount of fuel needed. However, we would like to present some known properties from statistics that can make our solution faster 😎.
Solution
We’re given a list of positions $X = \lbrace x_1, x_2, \ldots, x_n \rbrace$ for which we have to find some position $y_c$ according to the specified cost function $c$ that will minimize
$$\sum_{i=1}^{n} c(x_i, y_c)$$
so we can write that
$$y_c = \underset{x}{\textnormal{argmin}} \sum_{i=1}^{n} c(x_i, x)$$
In the first part we have to find the position $y_m$ that minimizes
$$\sum_{i=1}^{n} |x_i - y_m|$$
which is known to be minimized by median value from $X$ 😮.
That’s it - we don’t need to check all values from range $[ 0, \textnormal{max }X]$. It’s enough if your remember that property as it can be useful, while proving it cannot be presented easily.
In the second part we have to find the position $y_a$ that minimizes
$$\sum_{i=1}^{n} \sum_{j=1}^{|x_i - y_a|} j = \sum_{i=1}^{n} \frac{|x_i - y_a| (|x_i - y_a| + 1)}{2}$$
I don’t know how to minimize that sum directly, but for the purpose of the task I tried to minimize just the sum
$$\sum_{i=1}^{n} \frac{|x_i - y_a|^2}{2}=\sum_{i=1}^{n} \frac{(x_i - y_a)^2}{2}$$
for which we can calculate the derivative directly and compare it to $0$ to notice that this function have its minimum for $y_m = \frac{1}{n} \sum_{i=1}^{n} x_i$ i.e. the average of numbers from $X$.
It’s enough in case of this problem to find such approximation of the solution as we deal with natural numbers so changing value some number by $1$ is pretty small change so it shouldn’t change the final result - that’s only my intuition, but it seems to work in case of this problem as I have tested the solution, and I haven’t found bad cases yet.
I hope these properties of numbers may be useful for you at some day 🤞.
Day7.kt
|
|
Extra notes
We introduced pretty new function for dealing with input data when solving this day problem.
|
|
It is intended to use with named parameter - that’s the convention partially introduced to Kotlin that is quite popular in Swift and allows us to read code directly, e.g.
|
|
is simply seen as “List
of Int
separated by ","
” - keep this in mind when designing your api
in Kotlin to make it more readable in many cases 😉.