From 5c59b960c3a8571543a75ef1e0c73d65e70dc4cf Mon Sep 17 00:00:00 2001
From: Christopher Bohn <bohn@unl.edu>
Date: Fri, 2 Dec 2022 08:01:30 -0600
Subject: [PATCH] Finished Year 2022 Day 2
---
2022/README.md | 10 ++
.../java/edu/unl/cse/bohn/year2022/Day2.java | 157 ++++++++++++++++++
2 files changed, 167 insertions(+)
create mode 100644 2022/src/main/java/edu/unl/cse/bohn/year2022/Day2.java
diff --git a/2022/README.md b/2022/README.md
index 5f1626b..e88fb8b 100644
--- a/2022/README.md
+++ b/2022/README.md
@@ -31,5 +31,15 @@ Which means we can use the same solution for both parts, parameterized by the to
## 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)
diff --git a/2022/src/main/java/edu/unl/cse/bohn/year2022/Day2.java b/2022/src/main/java/edu/unl/cse/bohn/year2022/Day2.java
new file mode 100644
index 0000000..9646d89
--- /dev/null
+++ b/2022/src/main/java/edu/unl/cse/bohn/year2022/Day2.java
@@ -0,0 +1,157 @@
+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;
+ }
+}
--
GitLab