Skip to content
Snippets Groups Projects
Commit 15c83405 authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Refactored Year 2022 Day 2

parent 5c59b960
No related branches found
No related tags found
No related merge requests found
...@@ -39,6 +39,11 @@ The subproblems for both parts are ...@@ -39,6 +39,11 @@ The subproblems for both parts are
Seems pretty straight-forward. Seems pretty straight-forward.
### Refactoring opportunity
Parts 1 & 2 differ in whether we determine the outcome based off of "my" hand, or whether we determine "my" hand based off of the outcome.
Where we parameterized the commonality on Day 1, today we'll simply extract the commonality into a helper method.
## Day 3 ## Day 3
(coming soon) (coming soon)
......
...@@ -37,10 +37,7 @@ public class Day2 extends Puzzle { ...@@ -37,10 +37,7 @@ public class Day2 extends Puzzle {
return totalScore; return totalScore;
} }
private long scoreOneRoundForPart1(String choices) { private static Hand getOpponentHand(String choices) {
long score;
Outcome outcome = Outcome.UNKNOWN;
//noinspection DuplicatedCode
Hand opponent = switch (choices.charAt(0)) { Hand opponent = switch (choices.charAt(0)) {
case 'A' -> Hand.ROCK; case 'A' -> Hand.ROCK;
case 'B' -> Hand.PAPER; case 'B' -> Hand.PAPER;
...@@ -50,6 +47,13 @@ public class Day2 extends Puzzle { ...@@ -50,6 +47,13 @@ public class Day2 extends Puzzle {
if (opponent == Hand.UNKNOWN) { if (opponent == Hand.UNKNOWN) {
throw new IllegalStateException("Opponent's hand determined as \"UNKNOWN\". Choices: { " + choices + " }"); throw new IllegalStateException("Opponent's hand determined as \"UNKNOWN\". Choices: { " + choices + " }");
} }
return opponent;
}
private long scoreOneRoundForPart1(String choices) {
long score;
Outcome outcome = Outcome.UNKNOWN;
Hand opponent = getOpponentHand(choices);
Hand me; Hand me;
switch (choices.charAt(2)) { switch (choices.charAt(2)) {
case 'X' -> { case 'X' -> {
...@@ -98,16 +102,7 @@ public class Day2 extends Puzzle { ...@@ -98,16 +102,7 @@ public class Day2 extends Puzzle {
private long scoreOneRoundForPart2(String choices) { private long scoreOneRoundForPart2(String choices) {
long score; long score;
// Outcome outcome = Outcome.UNKNOWN; // Outcome outcome = Outcome.UNKNOWN;
//noinspection DuplicatedCode Hand opponent = getOpponentHand(choices);
Hand opponent = switch (choices.charAt(0)) {
case 'A' -> Hand.ROCK;
case 'B' -> Hand.PAPER;
case 'C' -> Hand.SCISSORS;
default -> Hand.UNKNOWN;
};
if (opponent == Hand.UNKNOWN) {
throw new IllegalStateException("Opponent's hand determined as \"UNKNOWN\". Choices: { " + choices + " }");
}
Hand me; Hand me;
switch (choices.charAt(2)) { switch (choices.charAt(2)) {
case 'X' -> { case 'X' -> {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment