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

Finished Year 2022 Day 2

parent 92b65f0f
Branches
No related tags found
No related merge requests found
...@@ -31,5 +31,15 @@ Which means we can use the same solution for both parts, parameterized by the to ...@@ -31,5 +31,15 @@ Which means we can use the same solution for both parts, parameterized by the to
## Day 2 ## Day 2
The subproblems for both parts are
- Score a single round
- Determine which hand is played by each player; adjust score based on "my" hand
- Determine the outcome of the round; adjust score based on the outcome
- Compute the total score
Seems pretty straight-forward.
## Day 3
(coming soon) (coming soon)
package edu.unl.cse.bohn.year2022;
import edu.unl.cse.bohn.Puzzle;
import java.util.List;
@SuppressWarnings("unused")
public class Day2 extends Puzzle {
public enum Hand {ROCK, PAPER, SCISSORS, UNKNOWN}
public enum Outcome {WIN, LOSE, DRAW, UNKNOWN}
public Day2(boolean isProductionReady) {
super(isProductionReady);
sampleData = """
A Y
B X
C Z""";
}
@Override
public long computePart1(List<String> data) {
long totalScore = 0;
for (String datum : data) {
totalScore += scoreOneRoundForPart1(datum);
}
return totalScore;
}
@Override
public long computePart2(List<String> data) {
long totalScore = 0;
for (String datum : data) {
totalScore += scoreOneRoundForPart2(datum);
}
return totalScore;
}
private long scoreOneRoundForPart1(String choices) {
long score;
Outcome outcome = Outcome.UNKNOWN;
//noinspection DuplicatedCode
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;
switch (choices.charAt(2)) {
case 'X' -> {
me = Hand.ROCK;
score = 1;
outcome = switch (opponent) {
case SCISSORS -> Outcome.WIN;
case PAPER -> Outcome.LOSE;
default -> Outcome.DRAW;
};
}
case 'Y' -> {
me = Hand.PAPER;
score = 2;
outcome = switch (opponent) {
case ROCK -> Outcome.WIN;
case SCISSORS -> Outcome.LOSE;
default -> Outcome.DRAW;
};
}
case 'Z' -> {
me = Hand.SCISSORS;
score = 3;
outcome = switch (opponent) {
case PAPER -> Outcome.WIN;
case ROCK -> Outcome.LOSE;
default -> Outcome.DRAW;
};
}
default -> {
me = Hand.UNKNOWN;
score = 0;
}
}
if (me == Hand.UNKNOWN) {
throw new IllegalStateException("My hand determined as \"UNKNOWN\". Choices: { " + choices + " }");
}
switch (outcome) {
case WIN -> score += 6;
case DRAW -> score += 3;
// no change for LOSE
}
return score;
}
private long scoreOneRoundForPart2(String choices) {
long score;
// Outcome outcome = Outcome.UNKNOWN;
//noinspection DuplicatedCode
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;
switch (choices.charAt(2)) {
case 'X' -> {
// outcome = Outcome.LOSE;
score = 0;
me = switch (opponent) {
case SCISSORS -> Hand.PAPER;
case PAPER -> Hand.ROCK;
case ROCK -> Hand.SCISSORS;
default -> Hand.UNKNOWN;
};
}
case 'Y' -> {
// outcome = Outcome.DRAW;
score = 3;
me = switch (opponent) {
case SCISSORS -> Hand.SCISSORS;
case PAPER -> Hand.PAPER;
case ROCK -> Hand.ROCK;
default -> Hand.UNKNOWN;
};
}
case 'Z' -> {
// outcome = Outcome.WIN;
score = 6;
me = switch (opponent) {
case SCISSORS -> Hand.ROCK;
case PAPER -> Hand.SCISSORS;
case ROCK -> Hand.PAPER;
default -> Hand.UNKNOWN;
};
}
default -> {
me = Hand.UNKNOWN;
score = 0;
}
}
switch (me) {
case ROCK -> score += 1;
case PAPER -> score += 2;
case SCISSORS -> score += 3;
default ->
throw new IllegalStateException("My hand determined as \"UNKNOWN\". Choices: { " + choices + " }");
}
return score;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment