Skip to content
Snippets Groups Projects
Commit a20bb720 authored by LUDMANN Pierre's avatar LUDMANN Pierre
Browse files

fix build and usage

parent 9cc429a6
Branches
No related tags found
No related merge requests found
Pipeline #16094 failed
package CieDuVaseDesNoces;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static java.lang.Thread.currentThread;
public class Day5 extends Day {
List<Long> seeds;
List<String> categories = new LinkedList<>();
Map<String, RangeMap> mapsByDest = new HashMap<>(7);
public Day5(String inputName) {
try (InputStream inputStream = currentThread().getContextClassLoader()
.getResourceAsStream(inputName)) {
assert inputStream != null;
String[] seedsAndMaps = new String(inputStream.readAllBytes())
.split("\n\n", 2);
seeds = Arrays.stream(
seedsAndMaps[0].substring("seeds: ".length()).split(
" +"))
.map(Long::parseUnsignedLong).toList();
Map<String, String> nextCategory = new HashMap<>(7);
for (String mapString : seedsAndMaps[1].split("\n\n")) {
String[] categoriesAndRanges = mapString.split(" +map:\n", 2);
String[] sourceAndDestination =
categoriesAndRanges[0].split("-to-", 2);
nextCategory.put(sourceAndDestination[0],
sourceAndDestination[1]);
parseRanges(sourceAndDestination[1], categoriesAndRanges[1]);
}
String category = nextCategory.get("seed");
while (category != null) {
categories.add(category);
category = nextCategory.get(category);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
void parseRanges(String dest, String ranges) {
RangeMap ans = new RangeMap();
ans.put(0L, 0L);
for (String rangeLine : ranges.split("\n")) {
ans.put(rangeLine);
}
mapsByDest.put(dest, ans);
}
String part1() {
Long ans = categories.stream().map(mapsByDest::get)
.reduce(seeds.stream().mapToLong(x -> x),
(numbers, rangeMap) -> numbers.map(rangeMap::apply)
, (x, y) -> y
).min().orElseThrow();
return String.valueOf(ans);
}
String part2() {
OptionalLong ans = categories.stream().map(mapsByDest::get)
.reduce(IntStream.range(0, seeds.size() / 2).mapToObj(
i -> new Range(seeds.get(2 * i), seeds.get(2 * i + 1))),
(numberRanges, mapRanges) -> numberRanges.flatMap(
mapRanges::apply), (x, y) -> y)
.mapToLong(Range::start).min();
return String.valueOf(ans.orElseThrow());
}
}
record Range(Long start, Long length) {
}
class RangeMap extends TreeMap<Long, Long> {
void put(String rangeLine) {
long[] rangeInfo =
Arrays.stream(rangeLine.split(" +"))
.mapToLong(Long::parseUnsignedLong).toArray();
Long destinationRangeStart = rangeInfo[0];
Long sourceRangeStart = rangeInfo[1];
Long rangeLength = rangeInfo[2];
put(sourceRangeStart, destinationRangeStart - sourceRangeStart);
Long sourceRangeEnd = sourceRangeStart + rangeLength;
if (!containsKey(sourceRangeEnd)) put(sourceRangeEnd, 0L);
}
Long apply(Long n) {
return n + floorEntry(n).getValue();
}
Stream<Range> apply(Range sourceRange) {
Long fromKey = sourceRange.start();
Long toKey = sourceRange.start() + sourceRange.length();
ArrayList<Long> sourceStarts = new ArrayList<>(
subMap(fromKey,
toKey).keySet().stream().
sorted().toList());
sourceStarts.addFirst(sourceRange.start());
List<Long> sourceEnds = new ArrayList<>(sourceStarts);
Collections.rotate(sourceEnds, -1);
sourceEnds.set(sourceEnds.size() - 1,
sourceRange.start() + sourceRange.length());
return IntStream.range(0, sourceStarts.size()).mapToObj(
i -> new Range(apply(sourceStarts.get(i)),
sourceEnds.get(i) - sourceStarts.get(i)));
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment