-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDay20.java
89 lines (69 loc) · 2.2 KB
/
Day20.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package aoc16;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import myutils16.Pair;
import myutils16.StaticUtils;
public class Day20 {
private List<Pair<Long, Long>> ranges;
public Day20(File input) {
List<String> rawData = StaticUtils.inputToList(input);
ranges = getRanges(rawData);
}
public long run1() {
Collections.sort(ranges, Comparator.comparing(n -> n.k()));
boolean targetFound = false;
long high = ranges.get(0).v();
while (!targetFound) {
long max = high;
Optional<Pair<Long, Long>> updatedMax = ranges.stream().filter(n -> n.k() <= max)
.max(Comparator.comparing(num -> num.v()));
if (updatedMax.isPresent() && updatedMax.get().v() != high) {
high = updatedMax.get().v();
} else {
targetFound = true;
}
}
return high + 1;
}
public long run2() {
Collections.sort(ranges, Comparator.comparing(n -> n.k()));
long count = 0;
long high = ranges.get(0).v();
boolean targetFound = false;
while (!targetFound) {
long max = high;
Optional<Pair<Long, Long>> updatedMax = ranges.stream().filter(n -> n.k() <= max)
.max(Comparator.comparing(num -> num.v()));
if (updatedMax.isPresent() && updatedMax.get().v() != high) {
high = updatedMax.get().v();
} else {
Optional<Pair<Long, Long>> nextLow = ranges.stream().filter(n -> n.k() > max)
.min(Comparator.comparing(n -> n.k()));
if(nextLow.isPresent()) {
count += nextLow.get().k() - high - 1;
high = nextLow.get().v();
} else {
targetFound = true;
}
}
}
return count;
}
private List<Pair<Long, Long>> getRanges(List<String> rawData) {
List<Pair<Long, Long>> ranges = new ArrayList<>();
for (String line : rawData) {
long low = Long.parseLong(line.substring(0, line.indexOf('-')));
long high = Long.parseLong(line.substring(line.indexOf('-') + 1));
ranges.add(new Pair<>(low, high));
}
return ranges;
}
public static void main(String[] args) {
Day20 test = new Day20(new File("\\C:\\Users\\Timucin\\Desktop\\Advent of code 2016\\Day 20\\InputFile.txt"));
System.out.println(test.run2());
}
}