From 1863408f6e542a88374f811c1cded13c04c41980 Mon Sep 17 00:00:00 2001 From: TheCK Date: Wed, 27 Nov 2024 19:34:29 +0100 Subject: [PATCH] aoc 2016 refactor --- adventofcode/README.md | 8 +-- .../org/ck/adventofcode/year2016/Day20.java | 54 +++++++++++++++++-- .../ck/adventofcode/year2016/day20/Part1.java | 44 --------------- .../ck/adventofcode/year2016/day20/Part2.java | 46 ---------------- .../ck/adventofcode/year2016/Day20Test.java | 3 -- .../year2016/day20/Part1Test.java | 13 ----- .../year2016/day20/Part2Test.java | 13 ----- 7 files changed, 54 insertions(+), 127 deletions(-) delete mode 100644 adventofcode/src/main/java/org/ck/adventofcode/year2016/day20/Part1.java delete mode 100644 adventofcode/src/main/java/org/ck/adventofcode/year2016/day20/Part2.java delete mode 100644 adventofcode/src/test/java/org/ck/adventofcode/year2016/day20/Part1Test.java delete mode 100644 adventofcode/src/test/java/org/ck/adventofcode/year2016/day20/Part2Test.java diff --git a/adventofcode/README.md b/adventofcode/README.md index 7a219123..60a188f7 100644 --- a/adventofcode/README.md +++ b/adventofcode/README.md @@ -344,8 +344,8 @@ [20161802solution]: src/main/java/org/ck/adventofcode/year2016/Day18.java [20161901solution]: src/main/java/org/ck/adventofcode/year2016/Day19.java [20161902solution]: src/main/java/org/ck/adventofcode/year2016/Day19.java -[20162001solution]: src/main/java/org/ck/adventofcode/year2016/day20/Part1.java -[20162002solution]: src/main/java/org/ck/adventofcode/year2016/day20/Part2.java +[20162001solution]: src/main/java/org/ck/adventofcode/year2016/Day20.java +[20162002solution]: src/main/java/org/ck/adventofcode/year2016/Day20.java [20162101solution]: src/main/java/org/ck/adventofcode/year2016/day21/Part1.java [20162102solution]: src/main/java/org/ck/adventofcode/year2016/day21/Part2.java [20162201solution]: src/main/java/org/ck/adventofcode/year2016/day22/Part1.java @@ -394,8 +394,8 @@ [20161802tests]: src/test/java/org/ck/adventofcode/year2016/Day18Test.java [20161901tests]: src/test/java/org/ck/adventofcode/year2016/Day19Test.java [20161902tests]: src/test/java/org/ck/adventofcode/year2016/Day19Test.java -[20162001tests]: src/test/java/org/ck/adventofcode/year2016/day20/Part1Test.java -[20162002tests]: src/test/java/org/ck/adventofcode/year2016/day20/Part2Test.java +[20162001tests]: src/test/java/org/ck/adventofcode/year2016/Day20Test.java +[20162002tests]: src/test/java/org/ck/adventofcode/year2016/Day20Test.java [20162101tests]: src/test/java/org/ck/adventofcode/year2016/day21/Part1Test.java [20162102tests]: src/test/java/org/ck/adventofcode/year2016/day21/Part2Test.java [20162201tests]: src/test/java/org/ck/adventofcode/year2016/day22/Part1Test.java diff --git a/adventofcode/src/main/java/org/ck/adventofcode/year2016/Day20.java b/adventofcode/src/main/java/org/ck/adventofcode/year2016/Day20.java index f0d2b6f2..72290356 100644 --- a/adventofcode/src/main/java/org/ck/adventofcode/year2016/Day20.java +++ b/adventofcode/src/main/java/org/ck/adventofcode/year2016/Day20.java @@ -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 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 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) {} } diff --git a/adventofcode/src/main/java/org/ck/adventofcode/year2016/day20/Part1.java b/adventofcode/src/main/java/org/ck/adventofcode/year2016/day20/Part1.java deleted file mode 100644 index 7513cdcd..00000000 --- a/adventofcode/src/main/java/org/ck/adventofcode/year2016/day20/Part1.java +++ /dev/null @@ -1,44 +0,0 @@ -package org.ck.adventofcode.year2016.day20; - -import java.util.*; -import org.ck.codechallengelib.annotation.Solution; - -@Solution( - id = 20162001, - name = "Day 20: Firewall Rules", - url = "https://adventofcode.com/2016/day/20", - category = "2016") -public class Part1 { - public static void main(String[] args) { - List blocked = new ArrayList<>(); - - try (Scanner in = new Scanner(System.in)) { - while (in.hasNextLine()) { - String[] input = in.nextLine().split("-"); - - blocked.add(new Range(Long.parseLong(input[0]), Long.parseLong(input[1]))); - } - } - - long ip = 0; - - while (true) { - long currentIp = ip; - - final Optional 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 { - break; - } - } - - System.out.println(ip); - } - - private record Range(long start, long end) {} -} diff --git a/adventofcode/src/main/java/org/ck/adventofcode/year2016/day20/Part2.java b/adventofcode/src/main/java/org/ck/adventofcode/year2016/day20/Part2.java deleted file mode 100644 index 2e2132dc..00000000 --- a/adventofcode/src/main/java/org/ck/adventofcode/year2016/day20/Part2.java +++ /dev/null @@ -1,46 +0,0 @@ -package org.ck.adventofcode.year2016.day20; - -import java.util.*; -import org.ck.codechallengelib.annotation.Solution; - -@Solution( - id = 20162002, - name = "Day 20: Firewall Rules - Part 2", - url = "https://adventofcode.com/2016/day/20", - category = "2016") -public class Part2 { - public static void main(String[] args) { - List blocked = new ArrayList<>(); - - try (Scanner in = new Scanner(System.in)) { - while (in.hasNextLine()) { - 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) { - long currentIp = ip; - - final Optional 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 { - ++count; - ++ip; - } - } - - System.out.println(count); - } - - private record Range(long start, long end) {} -} diff --git a/adventofcode/src/test/java/org/ck/adventofcode/year2016/Day20Test.java b/adventofcode/src/test/java/org/ck/adventofcode/year2016/Day20Test.java index 67ceaee1..fc910246 100644 --- a/adventofcode/src/test/java/org/ck/adventofcode/year2016/Day20Test.java +++ b/adventofcode/src/test/java/org/ck/adventofcode/year2016/Day20Test.java @@ -1,21 +1,18 @@ package org.ck.adventofcode.year2016; import org.ck.adventofcode.util.BaseAOCTest; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; class Day20Test extends BaseAOCTest { @ParameterizedTest @ValueSource(strings = {"01"}) - @Disabled void testOne(String name) throws Exception { runTest(new Day20()::partOne, "day20/%s".formatted(name)); } @ParameterizedTest @ValueSource(strings = {"02"}) - @Disabled void testTwo(String name) throws Exception { runTest(new Day20()::partTwo, "day20/%s".formatted(name)); } diff --git a/adventofcode/src/test/java/org/ck/adventofcode/year2016/day20/Part1Test.java b/adventofcode/src/test/java/org/ck/adventofcode/year2016/day20/Part1Test.java deleted file mode 100644 index 7a024ebb..00000000 --- a/adventofcode/src/test/java/org/ck/adventofcode/year2016/day20/Part1Test.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.ck.adventofcode.year2016.day20; - -import org.ck.codechallengelib.testhelper.BaseTest; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -public class Part1Test extends BaseTest { - @ParameterizedTest - @ValueSource(strings = {"01"}) - public void test(String name) throws Exception { - runFileAsStdIn(Part1.class, name); - } -} diff --git a/adventofcode/src/test/java/org/ck/adventofcode/year2016/day20/Part2Test.java b/adventofcode/src/test/java/org/ck/adventofcode/year2016/day20/Part2Test.java deleted file mode 100644 index f7acfe67..00000000 --- a/adventofcode/src/test/java/org/ck/adventofcode/year2016/day20/Part2Test.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.ck.adventofcode.year2016.day20; - -import org.ck.codechallengelib.testhelper.BaseTest; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -public class Part2Test extends BaseTest { - @ParameterizedTest - @ValueSource(strings = {"02"}) - public void test(String name) throws Exception { - runFileAsStdIn(Part2.class, name); - } -}