Skip to content
Snippets Groups Projects
Commit 3048a067 authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Day 7 complete

parent 2610bc51
No related branches found
No related tags found
No related merge requests found
......@@ -43,3 +43,15 @@ For example, in the problem statement practically *screams* that the
obvious solution grows exponentially. Normally, death would keep the population
under control, but I guess Death Takes a Holiday. One little extra gotcha:
26984457539 > Integer.MAX_INT, so I need to use a long integer.
## Day 7
Naively trying each position is reasonable: O(num_crabs * max_distance). Or,
we could follow a similar model of Day 6, in which we track the number of crabs
in each position, which would be O(max_distance^2). These are both very
manageable, so I'll go with tracking each crab since that requires less upfront
work.
Part 2 looks a bit more interesting. We *could* go for an
O(num_crabs * max_distance^2) solution, or we could recognize what Euler
recognized: ∑_{i=1}^{n}i = n(n+1)/2.
package edu.unl.cse.bohn.year2021;
import edu.unl.cse.bohn.Puzzle;
import java.util.Arrays;
import java.util.List;
@SuppressWarnings("unused")
public class Day7 extends Puzzle {
long[] crabs;
long maximum_position;
public Day7(boolean isProductionReady) {
super(isProductionReady);
sampleData = "16,1,2,0,4,2,7,1,2,14";
}
@Override
public long computePart1(List<String> data) {
crabs = Arrays.stream(data.get(0).split(",")).mapToLong(Long::parseLong).toArray();
maximum_position = Arrays.stream(crabs).max().orElseThrow();
long minimumCost = Long.MAX_VALUE;
for (long position = 0; position <= maximum_position; position++) {
long fuelCost = 0;
for (long crab : crabs) {
fuelCost += Math.abs(crab - position);
}
minimumCost = Long.min(minimumCost, fuelCost);
}
return minimumCost;
}
@Override
public long computePart2(List<String> data) {
crabs = Arrays.stream(data.get(0).split(",")).mapToLong(Long::parseLong).toArray();
maximum_position = Arrays.stream(crabs).max().orElseThrow();
long minimumCost = Long.MAX_VALUE;
for (long position = 0; position <= maximum_position; position++) {
long fuelCost = 0;
for (long crab : crabs) {
long displacement = Math.abs(crab - position);
fuelCost += displacement * (displacement + 1) / 2;
}
minimumCost = Long.min(minimumCost, fuelCost);
}
return minimumCost;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment