Skip to content

Commit a1a0283

Browse files
committed
java/2016/21: add solution for part 2
1 parent 6ef74a0 commit a1a0283

File tree

2 files changed

+50
-29
lines changed
  • java/src
    • main/java/com/github/saser/adventofcode/year2016/day21
    • test/java/com/github/saser/adventofcode/year2016/day21

2 files changed

+50
-29
lines changed

java/src/main/java/com/github/saser/adventofcode/year2016/day21/Day21.java

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.BufferedReader;
44
import java.io.Reader;
55
import java.util.Arrays;
6+
import java.util.Collections;
67
import java.util.List;
78
import java.util.regex.Matcher;
89
import java.util.regex.Pattern;
@@ -20,52 +21,77 @@ public static Result part2(Reader r) {
2021
}
2122

2223
private static Result solve(Reader r, int part) {
23-
var chars = "abcdefgh".toCharArray();
24+
var chars = (part == 1 ? "abcdefgh" : "fbgdceah").toCharArray();
2425
var instructions = new BufferedReader(r)
2526
.lines()
26-
.collect(Collectors.toUnmodifiableList());
27-
apply(chars, instructions);
27+
.collect(Collectors.toList());
28+
apply(chars, instructions, part == 2);
2829
return Result.ok(new String(chars));
2930
}
3031

31-
private static void apply(char[] chars, List<String> instructions) {
32+
private static void apply(char[] chars, List<String> instructions, boolean reverse) {
3233
var swapPositionRE = Pattern.compile("swap position (\\d+) with position (\\d+)");
3334
var swapLetterRE = Pattern.compile("swap letter (\\w) with letter (\\w)");
3435
var rotateStepsRE = Pattern.compile("rotate (left|right) (\\d+) steps?");
3536
var rotateLetterRE = Pattern.compile("rotate based on position of letter (\\w)");
3637
var reverseRE = Pattern.compile("reverse positions (\\d+) through (\\d+)");
3738
var moveRE = Pattern.compile("move position (\\d+) to position (\\d+)");
39+
if (reverse) {
40+
Collections.reverse(instructions);
41+
}
3842
for (var instruction : instructions) {
3943
Matcher matcher;
4044
matcher = swapPositionRE.matcher(instruction);
4145
if (matcher.matches()) {
4246
var x = Integer.parseInt(matcher.group(1));
4347
var y = Integer.parseInt(matcher.group(2));
44-
swap(chars, x, y);
48+
if (reverse) {
49+
swap(chars, y, x);
50+
} else {
51+
swap(chars, x, y);
52+
}
4553
continue;
4654
}
4755
matcher = swapLetterRE.matcher(instruction);
4856
if (matcher.matches()) {
4957
var x = find(chars, matcher.group(1).charAt(0));
5058
var y = find(chars, matcher.group(2).charAt(0));
51-
swap(chars, x, y);
59+
if (reverse) {
60+
swap(chars, y, x);
61+
} else {
62+
swap(chars, x, y);
63+
}
5264
continue;
5365
}
5466
matcher = rotateStepsRE.matcher(instruction);
5567
if (matcher.matches()) {
5668
var right = matcher.group(1).equals("right");
5769
var steps = Integer.parseInt(matcher.group(2));
70+
if (reverse) {
71+
right = !right;
72+
}
5873
rotate(chars, right, steps);
5974
continue;
6075
}
6176
matcher = rotateLetterRE.matcher(instruction);
6277
if (matcher.matches()) {
6378
var index = find(chars, matcher.group(1).charAt(0));
64-
var steps = 1 + index;
65-
if (index >= 4) {
66-
steps++;
79+
int steps;
80+
if (reverse) {
81+
if (index == 0) {
82+
steps = 1;
83+
} else if (index % 2 == 1) {
84+
steps = index / 2 + 1;
85+
} else {
86+
steps = 5 + index / 2;
87+
}
88+
} else {
89+
steps = 1 + index;
90+
if (index >= 4) {
91+
steps++;
92+
}
6793
}
68-
rotate(chars, true, steps);
94+
rotate(chars, !reverse, steps);
6995
continue;
7096
}
7197
matcher = reverseRE.matcher(instruction);
@@ -79,7 +105,11 @@ private static void apply(char[] chars, List<String> instructions) {
79105
if (matcher.matches()) {
80106
var x = Integer.parseInt(matcher.group(1));
81107
var y = Integer.parseInt(matcher.group(2));
82-
move(chars, x, y);
108+
if (reverse) {
109+
move(chars, y, x);
110+
} else {
111+
move(chars, x, y);
112+
}
83113
continue;
84114
}
85115
throw new IllegalArgumentException(String.format("invalid instruction: %s", instruction));

java/src/test/java/com/github/saser/adventofcode/year2016/day21/Day21Test.java

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,13 @@ public void part1Actual() throws IOException {
1818
}
1919
}
2020

21-
// @Test
22-
// public void part2Example() {
23-
// var input = new StringReader("");
24-
// var output = "";
25-
// var result = Day21.part2(input);
26-
// Assert.assertEquals("no error", "", result.error);
27-
// Assert.assertEquals("correct output", output, result.answer);
28-
// }
29-
30-
// @Test
31-
// public void part2Actual() throws IOException {
32-
// try (var input = new FileReader("inputs/2016/21")) {
33-
// var output = "";
34-
// var result = Day21.part2(input);
35-
// Assert.assertEquals("no error", "", result.error);
36-
// Assert.assertEquals("correct output", output, result.answer);
37-
// }
38-
// }
21+
@Test
22+
public void part2Actual() throws IOException {
23+
try (var input = new FileReader("inputs/2016/21")) {
24+
var output = "gcehdbfa";
25+
var result = Day21.part2(input);
26+
Assert.assertEquals("no error", "", result.error);
27+
Assert.assertEquals("correct output", output, result.answer);
28+
}
29+
}
3930
}

0 commit comments

Comments
 (0)