Skip to content

Commit

Permalink
aoc 2017 day 10
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCK committed Dec 14, 2024
1 parent 9a399b2 commit ea004e8
Show file tree
Hide file tree
Showing 18 changed files with 146 additions and 1 deletion.
10 changes: 9 additions & 1 deletion adventofcode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
|---------:|---------------------------------------------------------------------|:------------------------------------:|:---------------------------------:|
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)

Expand Down
90 changes: 90 additions & 0 deletions adventofcode/src/main/java/org/ck/adventofcode/year2017/Day10.java
Original file line number Diff line number Diff line change
@@ -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<String, List<Integer>> getLengthsFromLine,
final int rounds,
final Function<int[], String> 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<Integer> 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));
}
}
Original file line number Diff line number Diff line change
@@ -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")));
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
13760
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sp2J3XSDZaHV5lGMWljz7A==@IualT0OEWhIGJaiZllRKpuZr2J4d+wMZXgPgrShm21UNdLRp5qyMEpfxfwGgTjouNrl2DSgIBqvhf7Hga6lxhwd4kVrc2Q==
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
5
3,4,1,5
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2da93395f1a6bb3472203252e3b17fe5
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sp2J3XSDZaHV5lGMWljz7A==@IualT0OEWhIGJaiZllRKpuZr2J4d+wMZXgPgrShm21UNdLRp5qyMEpfxfwGgTjouNrl2DSgIBqvhf7Hga6lxhwd4kVrc2Q==
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a2582a3a0e66e6e86e3812dcb672a272
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
256

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
33efeb34ea91902bb2f59c9920caa6cd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
256
AoC 2017
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3efbe78a8d82f29979031a4aa0b16a9d
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
256
1,2,3
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
63960835bcdc130f0b66d7ff4f6a5a8e
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
256
1,2,4

0 comments on commit ea004e8

Please sign in to comment.