Working on my custom Interval

This commit is contained in:
Sugui 2023-12-05 19:02:28 +01:00
parent 6961310292
commit 6ae549ea78
5 changed files with 118 additions and 14 deletions

View File

@ -24,13 +24,13 @@ public class Day05 {
private record ProductionKitRanged(List<Pair<Long, Long>> seedPairs, List<List<MapInfo>> maps) {
}
private record MapInfo(long destination, long source, long range) {
public record MapInfo(long destination, long source, long range) {
}
private static long getLocation(List<MapInfo> mapper, long origin) {
public static long getLocation(List<MapInfo> mapper, long origin) {
for (var mapInfo : mapper) {
var originSourceDiff = origin - mapInfo.source;
if (originSourceDiff >= 0 && originSourceDiff <= mapInfo.range)
if (originSourceDiff >= 0 && originSourceDiff < mapInfo.range)
return mapInfo.destination + originSourceDiff;
}
return origin;
@ -103,7 +103,9 @@ public class Day05 {
ProductionKitRanged prodKit = parseSecond(input);
long minLocation = Long.MAX_VALUE;
for (var seedPair : prodKit.seedPairs) {
for (long seed = seedPair._1(); seed < seedPair._1() + seedPair._2(); seed++) {
long initSeed = seedPair._1();
long maxSeed = initSeed + seedPair._2();
for (long seed = initSeed; seed < maxSeed; seed++) {
long currLocation = seed;
for (var map : prodKit.maps) {
long newLocation = getLocation(map, currLocation);

View File

@ -2,15 +2,19 @@ package sugui.util;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
/**
* Class for intervals. From is inclusive, to is exclusive.
*/
public class Interval {
public class Interval implements Iterable<Long> {
private record Node(long value, boolean canBeLess) {
}
LinkedList<Node> interval = new LinkedList<>();
private Interval() {}
public Interval(long from, long to) {
Node fromNode = new Node(from, false);
Node toNode = new Node(to, true);
@ -19,13 +23,68 @@ public class Interval {
}
public boolean contains(long x) {
Iterator<Node> iterator = interval.iterator();
while(iterator.hasNext()) {
Node currNode = iterator.next();
if (x < currNode.value) {
return currNode.canBeLess;
for (Node node : interval) {
if (x < node.value) {
return node.canBeLess;
}
}
return false;
}
private void addNode(Node node) {
ListIterator<Node> iterator = interval.listIterator();
while (iterator.hasNext()) {
Node currNode = iterator.next();
if (node.value < currNode.value) {
iterator.previous();
iterator.add(node);
return;
}
}
iterator.add(node);
}
/**
* Adds the external interval to the current interval, modifying it.
* @param extInterval the inverval to add to the current interval
*/
public Interval union(Interval extInterval) {
Interval toret = new Interval();
for (Node node : interval) {
toret.addNode(node);
}
for (Node node : extInterval.interval) {
toret.addNode(node);
}
toret.normalize();
return toret;
}
public void normalize() {
ListIterator<Node> iterator = interval.listIterator();
while (iterator.hasNext()) {
Node currNode = iterator.next();
if (iterator.hasNext()) {
Node nextNode = iterator.next();
iterator.previous();
if (currNode.value == nextNode.value) {
iterator.remove();
}
}
}
}
@Override
public Iterator<Long> iterator() {
return new IntervalIterator(this);
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
for (Node node : interval) {
stringBuilder.append(node.canBeLess + " < " + node.value + "\n");
}
return stringBuilder.toString();
}
}

View File

@ -0,0 +1,22 @@
package sugui.util;
import java.util.Iterator;
public class IntervalIterator implements Iterator<Long> {
private Interval interval;
public IntervalIterator(Interval interval) {
this.interval = interval;
}
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return false;
}
@Override
public Long next() {
// TODO Auto-generated method stub
return null;
}
}

View File

@ -3,6 +3,7 @@ package sugui;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.util.List;
import org.junit.Test;
@ -56,6 +57,13 @@ public class Day05Test {
assertEquals(firstBasicResult, actualResult);
}
@Test
public void isGetLocationExclusive() {
assertEquals(Day05.getLocation(List.of(
new Day05.MapInfo(30, 0, 2)), 2),
2);
}
@Test
public void secondBasicCase() throws IOException {
String actualResult = Day05.getSecondPuzzleResult(secondBasicInput);

View File

@ -15,15 +15,28 @@ import sugui.util.Interval;
*/
public class IntervalTest {
public static Interval interval1 = new Interval(2, 8);
public static Interval interval1 = new Interval(2, 4);
public static Interval interval2 = new Interval(7, 9);
public static Interval union = interval1.union(interval2);
@Test
public void containsTest() throws IOException {
public void containsTest() {
assertTrue(interval1.contains(2));
assertTrue(interval1.contains(5));
assertTrue(interval1.contains(7));
assertTrue(interval1.contains(3));
assertFalse(interval1.contains(1));
assertFalse(interval1.contains(8));
assertFalse(interval1.contains(10));
}
@Test
public void unionTest() {
assertTrue(union.contains(2));
assertTrue(union.contains(3));
assertFalse(union.contains(4));
assertFalse(union.contains(5));
assertFalse(union.contains(6));
assertTrue(union.contains(7));
assertTrue(union.contains(8));
assertFalse(union.contains(9));
}
}