Skip to content

Commit

Permalink
Day 5 part 2 - at least running through
Browse files Browse the repository at this point in the history
  • Loading branch information
leibi committed Dec 16, 2023
1 parent 419f1cf commit 541b515
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 37 deletions.
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
65 changes: 30 additions & 35 deletions src/main/java/net/leibi/adventofcode2023/day5/Day5.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,56 @@

import com.google.common.collect.Lists;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.LongStream;

public class Day5 {

List<BigInteger> seedList = new ArrayList<>();
List<Long> seedList = new ArrayList<>();
LookUpMap seed2SOILLookUpMap = new LookUpMap();
LookUpMap soil2FertilizerMap = new LookUpMap();
LookUpMap fertilizor2WaterMap = new LookUpMap();
LookUpMap water2LightMap = new LookUpMap();
LookUpMap light2TempMap = new LookUpMap();

LookUpMap temp2HumidMap = new LookUpMap();

LookUpMap humid2LocationMap = new LookUpMap();
List<BigInteger> tmpList = new ArrayList<>();
List<Long> tmpList = new ArrayList<>();

BigInteger getLowestLocationNumber(String input) {
fillInputs(input);
Long getLowestLocationNumber(String input) {
fillMaps(input);
fillSeedList(input);

return seedList.stream()
.parallel()
.map(seed2SOILLookUpMap::getMapping)
.map(soil2FertilizerMap::getMapping)
.map(fertilizor2WaterMap::getMapping)
.map(water2LightMap::getMapping)
.map(light2TempMap::getMapping)
.map(temp2HumidMap::getMapping)
.map(humid2LocationMap::getMapping)
.min(BigInteger::compareTo).orElseThrow();
.min(Long::compareTo).orElseThrow();

}

BigInteger getLowestLocationNumberFromSeedList(String input) {
fillInputs(input);

List<BigInteger> completeSeedList = getCompleteSeedList();
Long getLowestLocationNumberFromSeedList(String input) {
fillMaps(input);
fillSeedList(input);

return completeSeedList.stream()
return getCompleteSeedList()
.parallel()
.map(seed2SOILLookUpMap::getMapping)
.map(soil2FertilizerMap::getMapping)
.map(fertilizor2WaterMap::getMapping)
.map(water2LightMap::getMapping)
.map(light2TempMap::getMapping)
.map(temp2HumidMap::getMapping)
.map(humid2LocationMap::getMapping)
.min(BigInteger::compareTo).orElseThrow();
.boxed()
.min(Long::compareTo).orElseThrow();

}

Expand All @@ -74,28 +75,23 @@ void fillSeedList(String input) {
.stream(first.get().split(":")[1]
.split(" "))
.filter(s -> !s.isEmpty())
.map(myString -> new BigInteger(myString, (int) 10))
.map(Long::valueOf)
.toList());
}

private List<BigInteger> getCompleteSeedList() {
private LongStream getCompleteSeedList() {

var partition = Lists.partition(seedList, 2);
return partition.stream().flatMap(subList -> generateSeeds(subList).stream()).toList();
return partition.stream()
.flatMapToLong(x -> generateSeedsAsStream(x.getFirst(), x.getFirst() + x.getLast()));
}

private List<BigInteger> generateSeeds(List<BigInteger> a) {
// need to get all BigIntegers from
//a.get(0) to a.get(0)+a.get(1)
tmpList.clear();
for (BigInteger i = a.getFirst(); i.compareTo(a.getFirst().add(a.getLast())) < 0; i = i.add(BigInteger.ONE)) {
tmpList.add(i);
}
return List.copyOf(tmpList);

private LongStream generateSeedsAsStream(Long start, Long end) {
return LongStream.range(start, end + 1);
}

private void fillInputs(String input) {
fillSeedList(input);
private void fillMaps(String input) {

fillMap(input, seed2SOILLookUpMap, "seed-to-soil map:");
fillMap(input, soil2FertilizerMap, "soil-to-fertilizer map:");
fillMap(input, fertilizor2WaterMap, "fertilizer-to-water map:");
Expand All @@ -114,24 +110,23 @@ public void fill(String inputstring) {
add(m);
}

BigInteger getMapping(BigInteger input) {
Long getMapping(Long input) {
// if input is a BigDecimal between any sourceStart and sourceStart+range
// we return destStart + distance
var destination = this.stream().map(myMapEntry -> myMapEntry.getDestination(input)).filter(Optional::isPresent).findFirst().orElse(Optional.empty());
return destination.orElse(input);
}

record MyMapEntry(BigInteger destStart, BigInteger sourceStart, BigInteger range) {
record MyMapEntry(Long destStart, Long sourceStart, Long range) {
public MyMapEntry(List<String> l) {
this(new BigInteger(l.get(0)), new BigInteger(l.get(1)), new BigInteger(l.get(2)));
this(Long.valueOf(l.get(0)), Long.valueOf(l.get(1)), Long.valueOf(l.get(2)));
}

public Optional<BigInteger> getDestination(BigInteger input) {
if (input.compareTo(sourceStart) < 0 || input.compareTo(sourceStart.add(range)) > 0)
public Optional<Long> getDestination(Long input) {
if (input.compareTo(sourceStart) < 0 || input.compareTo(sourceStart + range) > 0)
return Optional.empty();

return Optional.of(destStart.add(input.subtract(sourceStart)));

return Optional.of(destStart + (input - sourceStart));
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/test/java/net/leibi/adventofcode2023/day5/Day5Test.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.leibi.adventofcode2023.day5;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -21,7 +22,7 @@ void getCompleteSeedList_Small() {

@Test
void getCompleteSeedList_Big() {
assertThat(day5.getLowestLocationNumberFromSeedList(Input.BIG)).isEqualTo(46);
assertThat(day5.getLowestLocationNumberFromSeedList(Input.BIG)).isEqualTo(28580590L);
}

@Test
Expand Down

0 comments on commit 541b515

Please sign in to comment.