diff --git a/adventofcode/README.md b/adventofcode/README.md index 378657e4..cc937ce9 100644 --- a/adventofcode/README.md +++ b/adventofcode/README.md @@ -406,7 +406,7 @@ [20162402tests]: src/test/java/org/ck/adventofcode/year2016/Day24Test.java [20162501tests]: src/test/java/org/ck/adventofcode/year2016/Day25Test.java -# 2017 (18/18) +# 2017 (20/20) | # | Name | Solution | Test | |---------:|---------------------------------------------------------------------|:------------------------------------:|:---------------------------------:| @@ -428,6 +428,8 @@ | 20170802 | [Day 8: I Heard You Like Registers - Part 2][20170802] | ✅[💾][20170802solution] | ✅[💾][20170802tests] | | 20170901 | [Day 9: Stream Processing][20170901] | ✅[💾][20170901solution] | ✅[💾][20170901tests] | | 20170902 | [Day 9: Stream Processing - Part 2][20170902] | ✅[💾][20170902solution] | ✅[💾][20170902tests] | +| 20171001 | [Day 10: Knot Hash][20171001] | ✅[💾][20171001solution] | ✅[💾][20171001tests] | +| 20171002 | [Day 10: Knot Hash - Part 2][20171002] | ✅[💾][20171002solution] | ✅[💾][20171002tests] | [20170101]: https://adventofcode.com/2017/day/1 [20170102]: https://adventofcode.com/2017/day/1#part2 @@ -447,6 +449,8 @@ [20170802]: https://adventofcode.com/2017/day/8#part2 [20170901]: https://adventofcode.com/2017/day/9 [20170902]: https://adventofcode.com/2017/day/9#part2 +[20171001]: https://adventofcode.com/2017/day/10 +[20171002]: https://adventofcode.com/2017/day/10#part2 [20170101solution]: src/main/java/org/ck/adventofcode/year2017/Day01.java [20170102solution]: src/main/java/org/ck/adventofcode/year2017/Day01.java @@ -466,6 +470,8 @@ [20170802solution]: src/main/java/org/ck/adventofcode/year2017/Day08.java [20170901solution]: src/main/java/org/ck/adventofcode/year2017/Day09.java [20170902solution]: src/main/java/org/ck/adventofcode/year2017/Day09.java +[20171001solution]: src/main/java/org/ck/adventofcode/year2017/Day10.java +[20171002solution]: src/main/java/org/ck/adventofcode/year2017/Day10.java [20170101tests]: src/test/java/org/ck/adventofcode/year2017/Day01Test.java [20170102tests]: src/test/java/org/ck/adventofcode/year2017/Day01Test.java @@ -485,6 +491,8 @@ [20170802tests]: src/test/java/org/ck/adventofcode/year2017/Day08Test.java [20170901tests]: src/test/java/org/ck/adventofcode/year2017/Day09Test.java [20170902tests]: src/test/java/org/ck/adventofcode/year2017/Day09Test.java +[20171001tests]: src/test/java/org/ck/adventofcode/year2017/Day10Test.java +[20171002tests]: src/test/java/org/ck/adventofcode/year2017/Day10Test.java # 2019 (29/30) diff --git a/adventofcode/src/main/java/org/ck/adventofcode/year2017/Day10.java b/adventofcode/src/main/java/org/ck/adventofcode/year2017/Day10.java new file mode 100644 index 00000000..49299e98 --- /dev/null +++ b/adventofcode/src/main/java/org/ck/adventofcode/year2017/Day10.java @@ -0,0 +1,90 @@ +package org.ck.adventofcode.year2017; + +import java.util.Arrays; +import java.util.HexFormat; +import java.util.List; +import java.util.Scanner; +import java.util.function.Function; +import org.ck.adventofcode.util.AOCSolution; +import org.ck.codechallengelib.annotation.Solution; + +@Solution( + id = 20171001, + name = "Day 10: Knot Hash", + url = "https://adventofcode.com/2017/day/10", + category = "2017") +@Solution( + id = 20171002, + name = "Day 10: Knot Hash - Part 2", + url = "https://adventofcode.com/2017/day/10#part2", + category = "2017") +public class Day10 extends AOCSolution { + @Override + protected void runPartOne(final Scanner in) { + run( + in, + line -> Arrays.stream(line.split(",")).map(Integer::valueOf).toList(), + 1, + list -> String.valueOf(list[0] * list[1])); + } + + @Override + protected void runPartTwo(final Scanner in) { + run( + in, + line -> (line + "\u0011\u001F\u0049\u002F\u0017").chars().boxed().toList(), + 64, + list -> { + byte[] bytes = new byte[16]; + + for (int i = 0; i < list.length; ++i) { + bytes[i / 16] = (byte) (bytes[i / 16] ^ list[i]); + } + + return HexFormat.of().formatHex(bytes); + }); + } + + private void run( + final Scanner in, + final Function> getLengthsFromLine, + final int rounds, + final Function getResult) { + int listLength = in.nextInt(); + in.nextLine(); + + final int[] list = new int[listLength]; + for (int i = 0; i < listLength; ++i) { + list[i] = i; + } + + final List lengths = getLengthsFromLine.apply(in.nextLine()); + + int index = 0; + int skipSize = 0; + + for (int round = 0; round < rounds; ++round) { + for (int length : lengths) { + int start = index; + int end = (start + length - 1) % list.length; + + for (int i = 0; i < length / 2; ++i) { + final int temp = list[end]; + list[end] = list[start]; + list[start] = temp; + + start = (start + 1) % list.length; + --end; + if (end < 0) { + end = list.length - 1; + } + } + + index = (index + length + skipSize) % list.length; + ++skipSize; + } + } + + print(getResult.apply(list)); + } +} diff --git a/adventofcode/src/test/java/org/ck/adventofcode/util/InputEncrpyter.java b/adventofcode/src/test/java/org/ck/adventofcode/util/InputEncrpyter.java new file mode 100644 index 00000000..163383d6 --- /dev/null +++ b/adventofcode/src/test/java/org/ck/adventofcode/util/InputEncrpyter.java @@ -0,0 +1,9 @@ +package org.ck.adventofcode.util; + +import org.ck.codechallengelib.testhelper.EncryptionHelper; + +public class InputEncrpyter { + public static void main(final String[] args) { + System.err.println(EncryptionHelper.encrypt("", System.getenv("AOC_KEY"))); + } +} diff --git a/adventofcode/src/test/java/org/ck/adventofcode/year2017/Day10Test.java b/adventofcode/src/test/java/org/ck/adventofcode/year2017/Day10Test.java new file mode 100644 index 00000000..b1039c95 --- /dev/null +++ b/adventofcode/src/test/java/org/ck/adventofcode/year2017/Day10Test.java @@ -0,0 +1,19 @@ +package org.ck.adventofcode.year2017; + +import org.ck.adventofcode.util.BaseAOCTest; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +class Day10Test extends BaseAOCTest { + @ParameterizedTest + @ValueSource(strings = {"01a"}) + void testPartOneExamples(final String name) throws Exception { + runTest(new Day10()::partOne, "day10/%s".formatted(name)); + } + + @ParameterizedTest + @ValueSource(strings = {"02a", "02b", "02a", "02d"}) + void testPartTwoExamples(final String name) throws Exception { + runTest(new Day10()::partTwo, "day10/%s".formatted(name)); + } +} diff --git a/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/01.result.txt b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/01.result.txt new file mode 100644 index 00000000..006d2c32 --- /dev/null +++ b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/01.result.txt @@ -0,0 +1 @@ +13760 \ No newline at end of file diff --git a/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/01.txt b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/01.txt new file mode 100644 index 00000000..724e72c4 --- /dev/null +++ b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/01.txt @@ -0,0 +1 @@ +Sp2J3XSDZaHV5lGMWljz7A==@IualT0OEWhIGJaiZllRKpuZr2J4d+wMZXgPgrShm21UNdLRp5qyMEpfxfwGgTjouNrl2DSgIBqvhf7Hga6lxhwd4kVrc2Q== \ No newline at end of file diff --git a/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/01a.result.txt b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/01a.result.txt new file mode 100644 index 00000000..3cacc0b9 --- /dev/null +++ b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/01a.result.txt @@ -0,0 +1 @@ +12 \ No newline at end of file diff --git a/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/01a.txt b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/01a.txt new file mode 100644 index 00000000..3733f9ed --- /dev/null +++ b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/01a.txt @@ -0,0 +1,2 @@ +5 +3,4,1,5 \ No newline at end of file diff --git a/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02.result.txt b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02.result.txt new file mode 100644 index 00000000..7c0ce835 --- /dev/null +++ b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02.result.txt @@ -0,0 +1 @@ +2da93395f1a6bb3472203252e3b17fe5 \ No newline at end of file diff --git a/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02.txt b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02.txt new file mode 100644 index 00000000..724e72c4 --- /dev/null +++ b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02.txt @@ -0,0 +1 @@ +Sp2J3XSDZaHV5lGMWljz7A==@IualT0OEWhIGJaiZllRKpuZr2J4d+wMZXgPgrShm21UNdLRp5qyMEpfxfwGgTjouNrl2DSgIBqvhf7Hga6lxhwd4kVrc2Q== \ No newline at end of file diff --git a/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02a.result.txt b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02a.result.txt new file mode 100644 index 00000000..310f0848 --- /dev/null +++ b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02a.result.txt @@ -0,0 +1 @@ +a2582a3a0e66e6e86e3812dcb672a272 \ No newline at end of file diff --git a/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02a.txt b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02a.txt new file mode 100644 index 00000000..75536ce6 --- /dev/null +++ b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02a.txt @@ -0,0 +1,2 @@ +256 + diff --git a/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02b.result.txt b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02b.result.txt new file mode 100644 index 00000000..db9ef312 --- /dev/null +++ b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02b.result.txt @@ -0,0 +1 @@ +33efeb34ea91902bb2f59c9920caa6cd \ No newline at end of file diff --git a/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02b.txt b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02b.txt new file mode 100644 index 00000000..4bb8749e --- /dev/null +++ b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02b.txt @@ -0,0 +1,2 @@ +256 +AoC 2017 \ No newline at end of file diff --git a/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02c.result.txt b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02c.result.txt new file mode 100644 index 00000000..15509100 --- /dev/null +++ b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02c.result.txt @@ -0,0 +1 @@ +3efbe78a8d82f29979031a4aa0b16a9d \ No newline at end of file diff --git a/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02c.txt b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02c.txt new file mode 100644 index 00000000..36043c74 --- /dev/null +++ b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02c.txt @@ -0,0 +1,2 @@ +256 +1,2,3 \ No newline at end of file diff --git a/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02d.result.txt b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02d.result.txt new file mode 100644 index 00000000..21995599 --- /dev/null +++ b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02d.result.txt @@ -0,0 +1 @@ +63960835bcdc130f0b66d7ff4f6a5a8e \ No newline at end of file diff --git a/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02d.txt b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02d.txt new file mode 100644 index 00000000..2f4df3d6 --- /dev/null +++ b/adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02d.txt @@ -0,0 +1,2 @@ +256 +1,2,4 \ No newline at end of file