-
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
18 changed files
with
146 additions
and
1 deletion.
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
90 changes: 90 additions & 0 deletions
90
adventofcode/src/main/java/org/ck/adventofcode/year2017/Day10.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 |
---|---|---|
@@ -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)); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
adventofcode/src/test/java/org/ck/adventofcode/util/InputEncrpyter.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 |
---|---|---|
@@ -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"))); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
adventofcode/src/test/java/org/ck/adventofcode/year2017/Day10Test.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 |
---|---|---|
@@ -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)); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/01.result.txt
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
13760 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/01.txt
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Sp2J3XSDZaHV5lGMWljz7A==@IualT0OEWhIGJaiZllRKpuZr2J4d+wMZXgPgrShm21UNdLRp5qyMEpfxfwGgTjouNrl2DSgIBqvhf7Hga6lxhwd4kVrc2Q== |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/01a.result.txt
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
12 |
2 changes: 2 additions & 0 deletions
2
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/01a.txt
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
5 | ||
3,4,1,5 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02.result.txt
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
2da93395f1a6bb3472203252e3b17fe5 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02.txt
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Sp2J3XSDZaHV5lGMWljz7A==@IualT0OEWhIGJaiZllRKpuZr2J4d+wMZXgPgrShm21UNdLRp5qyMEpfxfwGgTjouNrl2DSgIBqvhf7Hga6lxhwd4kVrc2Q== |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02a.result.txt
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
a2582a3a0e66e6e86e3812dcb672a272 |
2 changes: 2 additions & 0 deletions
2
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02a.txt
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
256 | ||
|
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02b.result.txt
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
33efeb34ea91902bb2f59c9920caa6cd |
2 changes: 2 additions & 0 deletions
2
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02b.txt
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
256 | ||
AoC 2017 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02c.result.txt
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
3efbe78a8d82f29979031a4aa0b16a9d |
2 changes: 2 additions & 0 deletions
2
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02c.txt
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
256 | ||
1,2,3 |
1 change: 1 addition & 0 deletions
1
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02d.result.txt
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
63960835bcdc130f0b66d7ff4f6a5a8e |
2 changes: 2 additions & 0 deletions
2
adventofcode/src/test/resources/org/ck/adventofcode/year2017/day10/02d.txt
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
256 | ||
1,2,4 |