day five in the books
This commit is contained in:
100
src/Day05.java
Normal file
100
src/Day05.java
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
package src;
|
||||||
|
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Day05 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Instant start = Instant.now();
|
||||||
|
part1();
|
||||||
|
Instant middle = Instant.now();
|
||||||
|
part2();
|
||||||
|
Instant end = Instant.now();
|
||||||
|
IO.println("part 1: " + Duration.between(start, middle).toMillis() + " ms");
|
||||||
|
IO.println("part 2: " + Duration.between(middle, end).toMillis() + " ms");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void part1() {
|
||||||
|
try (Scanner scanner = new Scanner(new FileReader("resources/inputs/05/input"))) {
|
||||||
|
List<Pair<Long, Long>> ranges = new ArrayList<>();
|
||||||
|
List<Long> ingredients = new ArrayList<>();
|
||||||
|
|
||||||
|
while (scanner.hasNextLine()) {
|
||||||
|
String line = scanner.nextLine();
|
||||||
|
if (line.contains("-")) {
|
||||||
|
String[] parts = line.split("-");
|
||||||
|
long first = Long.parseLong(parts[0]);
|
||||||
|
long second = Long.parseLong(parts[1]);
|
||||||
|
ranges.add(new Pair<>(first, second));
|
||||||
|
} else if (!line.isBlank()) {
|
||||||
|
ingredients.add(Long.parseLong(line));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int count = 0;
|
||||||
|
for (Long ingredient : ingredients) {
|
||||||
|
for (Pair<Long, Long> range : ranges) {
|
||||||
|
if (ingredient >= range.head && ingredient <= range.tail) {
|
||||||
|
count++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
IO.println(count);
|
||||||
|
} catch (Exception e) {
|
||||||
|
IO.println(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void part2() {
|
||||||
|
try (Scanner scanner = new Scanner(new FileReader("resources/inputs/05/input"))) {
|
||||||
|
List<Pair<Long, Long>> ranges = new ArrayList<>();
|
||||||
|
|
||||||
|
while (scanner.hasNextLine()) {
|
||||||
|
String line = scanner.nextLine();
|
||||||
|
if (line.contains("-")) {
|
||||||
|
String[] parts = line.split("-");
|
||||||
|
long first = Long.parseLong(parts[0]);
|
||||||
|
long second = Long.parseLong(parts[1]);
|
||||||
|
ranges.add(new Pair<>(first, second));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Pair<Long, Long> result = ranges.stream()
|
||||||
|
.sorted((a, b) -> Long.compare(a.head, b.head))
|
||||||
|
.reduce(
|
||||||
|
// first is count, second is end of range covered so far
|
||||||
|
new Pair<>(0L, 0L),
|
||||||
|
(total, curr) -> {
|
||||||
|
if (total.tail >= curr.tail) {
|
||||||
|
// this range was subsumed by a previous range
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
if (total.tail >= curr.head) {
|
||||||
|
// this range was partially included
|
||||||
|
return new Pair<>(total.head + (curr.tail - total.tail), curr.tail);
|
||||||
|
}
|
||||||
|
// this range was not previously included at all
|
||||||
|
// +1 to include both endpoints
|
||||||
|
return new Pair<>(total.head + (curr.tail - curr.head) + 1, curr.tail);
|
||||||
|
},
|
||||||
|
// merge function doesn't actually get used here but might as well write it
|
||||||
|
(a, b) -> new Pair<>(a.head + b.head, Long.max(a.tail, b.tail)));
|
||||||
|
IO.println(result.head);
|
||||||
|
} catch (Exception e) {
|
||||||
|
IO.println(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class Pair<T, U> {
|
||||||
|
public final T head;
|
||||||
|
public final U tail;
|
||||||
|
|
||||||
|
public Pair(T first, U second) {
|
||||||
|
this.head = first;
|
||||||
|
this.tail = second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user