Skip to content
AoC 2022, Day two - Part One
This image is generated using Dall-E
Category: Advent of Code

AoC 2022, Day two - Part One

Get the outcome of each rock-paper-scissors match.

Bart Kessels 5 min read

Part of the series

Advent of Code 2022

Preface

Now that we have the following data structure from our previous post, we can start by implementing the business logic to calculate the outcome of each match. Before we can do that, we need to setup two conversion tables. One for converting each strategy and another one to convert each match outcome to points.

[
  { "A", "Y" }, // Match 1
  { "B", "X" }, // Match 2
  { "C", "Z" }  // Match 3
]

Conversion table for strategies

A for Rock, B for Paper, and C for Scissors […] X for Rock, Y for Paper, and Z for Scissors

RockPaperScissors
ABC
XYZ

Conversion table for the match outcome

score for the outcome of the round […] 0 if you lost, 3 if the round was a draw, and 6 if you won

0 points3 points6 points
Rock - PaperRock - RockRock - Scissors
Paper - ScissorsPaper - PaperPaper - Rock
Scissors - RockScissors - ScissorsScissors - Paper

Design

Now we know how our data is structured and we gave it a meaning. We can start to think about our business logic. The steps we need to take is that we need to get the points for each match and combine them with the points for each strategy we played.

the score for the shape you selected […] 1 for Rock, 2 for Paper, and 3 for Scissors

So for the sample input we get the following diagram.

Match 3

Add 3 strategy points

Play match

Strategy

opponent: Scissors

you: Scissors
3 strategy points

3 points

Total 6 points

Match 2

Add 1 strategy points

Play match

Strategy

opponent: Paper

you: Rock
1 strategy points

0 points

Total 1 points

Match 1

Add 2 strategy point

Play match

Strategy

opponent: Rock

you: Paper
2 strategy points

6 points

Total 8 point

Sum()

Total: 15 points

Once all the matches have been played, and the points calculated we can add all outcomes togheter for our end result.

Implementation

Business logic

Now we know what we want our code to do, let’s start implementing it in our PartOne class.

aoc-2022/day2/src/main/kotlin/aoc/PartOne.kt
class PartOne(
    private val sanitizer: Sanitizer
) {
    fun getResult(): Int {
        val data = sanitizer.getItems()
        val points = data?.map {
            val strategyPoints = when(it.second) { // 1
                "X" -> 1    // Rock
                "Y" -> 2    // Paper
                else -> 3   // Scissors
            }
            val roundOutcome = when(it) { // 2
                // Lost
                Pair("A", "Z"),
                Pair("B", "X"),
                Pair("C", "Y") -> 0

                // Draw
                Pair("A", "X"),
                Pair("B", "Y"),
                Pair("C", "Z") -> 3

                // Won
                else -> 6
            }

            strategyPoints + roundOutcome
        }

        val totalRoundsOutcome = points?.sum()

        return totalRoundsOutcome ?: -1;
    }
}

What our code does, is, it turns our matches list into a list of round outcomes. At step 1 the strategy points are calculated based on the information we’ve gotten from the assignment. We only check for our own moves, because we don’t get points for the move the opponent made.

Once we’ve gotten the score from our strategy, we calculate the points based on the round outcome. At step 2 we only check for winnings and a draw. This can be cleaned up by splitting it into a separate method or normalizing the data into Rock, Paper and Scissors instead of the current A, B, X etc.

So this will give us the following data structure.

[
  8,
  1,
  6
]

This list is finally summed up and return as the assignment outcome.

Test case

Because we know that we have a list of all round outcomes, we know that we can sum each item in the list to get the total score. As you can see in our previous diagram, the total score of the sample input will be 15.

So we can write a test case that validates our test input to the outcome of 15. Right now we can update the PartOneTest class with the following contents.

aoc-2022/day2/src/test/kotlin/aoc/PartOne.kt
class PartOneTest {
    @Test
    fun testGetResult() {
        // Arrange
        val resource = {}::class.java.getResource("/input.txt")
        val sanitizer = Sanitizer(resource)
        val sut = PartOne(sanitizer)
        val expectedNumberOfPoints = 15

        // Act
        val result = sut.getResult()

        // Assert
        assertEquals(expectedNumberOfPoints, result)
    }
}