-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
54 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 50 additions & 4 deletions
54
adventofcode/src/main/java/org/ck/adventofcode/year2016/Day20.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,64 @@ | ||
package org.ck.adventofcode.year2016; | ||
|
||
import java.util.Scanner; | ||
import java.util.*; | ||
import org.ck.adventofcode.util.AOCSolution; | ||
import org.ck.codechallengelib.annotation.Solution; | ||
|
||
@Solution( | ||
id = 20162001, | ||
name = "Day 20: Firewall Rules", | ||
url = "https://adventofcode.com/2016/day/20", | ||
category = "2016") | ||
@Solution( | ||
id = 20162002, | ||
name = "Day 20: Firewall Rules - Part 2", | ||
url = "https://adventofcode.com/2016/day/20", | ||
category = "2016") | ||
public class Day20 extends AOCSolution { | ||
@Override | ||
protected void runPartOne(final Scanner in) { | ||
run(in); | ||
run(in, true); | ||
} | ||
|
||
@Override | ||
protected void runPartTwo(final Scanner in) { | ||
run(in); | ||
run(in, false); | ||
} | ||
|
||
private void run(final Scanner in) {} | ||
private void run(final Scanner in, final boolean findFirstOnly) { | ||
final List<Range> blocked = new ArrayList<>(); | ||
|
||
while (in.hasNextLine()) { | ||
final String[] input = in.nextLine().split("-"); | ||
|
||
blocked.add(new Range(Long.parseLong(input[0]), Long.parseLong(input[1]))); | ||
} | ||
|
||
long count = 0; | ||
long ip = 0; | ||
|
||
while (ip <= 4294967295L) { | ||
final long currentIp = ip; | ||
|
||
final Optional<Range> max = | ||
blocked.stream() | ||
.filter(block -> block.start() <= currentIp && currentIp <= block.end()) | ||
.max(Comparator.comparingLong(Range::end)); | ||
|
||
if (max.isPresent()) { | ||
ip = max.get().end() + 1; | ||
} else { | ||
if (findFirstOnly) { | ||
break; | ||
} | ||
|
||
++count; | ||
++ip; | ||
} | ||
} | ||
|
||
print(findFirstOnly ? ip : count); | ||
} | ||
|
||
private record Range(long start, long end) {} | ||
} |
44 changes: 0 additions & 44 deletions
44
adventofcode/src/main/java/org/ck/adventofcode/year2016/day20/Part1.java
This file was deleted.
Oops, something went wrong.
46 changes: 0 additions & 46 deletions
46
adventofcode/src/main/java/org/ck/adventofcode/year2016/day20/Part2.java
This file was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
adventofcode/src/test/java/org/ck/adventofcode/year2016/Day20Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
adventofcode/src/test/java/org/ck/adventofcode/year2016/day20/Part1Test.java
This file was deleted.
Oops, something went wrong.
13 changes: 0 additions & 13 deletions
13
adventofcode/src/test/java/org/ck/adventofcode/year2016/day20/Part2Test.java
This file was deleted.
Oops, something went wrong.