diff --git a/src/main/java/sugui/Day05.java b/src/main/java/sugui/Day05.java index 55388ed..fad5bd3 100644 --- a/src/main/java/sugui/Day05.java +++ b/src/main/java/sugui/Day05.java @@ -24,13 +24,13 @@ public class Day05 { private record ProductionKitRanged(List> seedPairs, List> maps) { } - private record MapInfo(long destination, long source, long range) { + public record MapInfo(long destination, long source, long range) { } - private static long getLocation(List mapper, long origin) { + public static long getLocation(List 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); diff --git a/src/main/java/sugui/util/Interval.java b/src/main/java/sugui/util/Interval.java index f757480..7375a6e 100644 --- a/src/main/java/sugui/util/Interval.java +++ b/src/main/java/sugui/util/Interval.java @@ -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 { private record Node(long value, boolean canBeLess) { } + LinkedList 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 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 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 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 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(); + } } diff --git a/src/main/java/sugui/util/IntervalIterator.java b/src/main/java/sugui/util/IntervalIterator.java new file mode 100644 index 0000000..d199168 --- /dev/null +++ b/src/main/java/sugui/util/IntervalIterator.java @@ -0,0 +1,22 @@ +package sugui.util; + +import java.util.Iterator; + +public class IntervalIterator implements Iterator { + 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; + } +} diff --git a/src/test/java/sugui/Day05Test.java b/src/test/java/sugui/Day05Test.java index 2040ec1..4de412c 100644 --- a/src/test/java/sugui/Day05Test.java +++ b/src/test/java/sugui/Day05Test.java @@ -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); diff --git a/src/test/java/sugui/IntervalTest.java b/src/test/java/sugui/IntervalTest.java index 7db3d67..0be5ec1 100644 --- a/src/test/java/sugui/IntervalTest.java +++ b/src/test/java/sugui/IntervalTest.java @@ -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)); + } }