Table of Contents

IronForgeCyber

Courses

Resources

Blog

Log In



IronForgeCyber

AboutPrivacyTerms

© 2026 IronForgeCyber. All rights reserved.

  1. courses
  2. c-programming
  3. rock-paper-scissors
13 min read

Rock Paper Scissors

From Advent of Code
This challenge is based on Day 2: Rock Paper Scissors from Advent of Code 2022, an annual collection of programming puzzles by Eric Wastl.

Problem Description

You are provided a list of data input.txt that contains a list of Rock Paper Scissors games. Each line includes the enemy's move (A for Rock, B for Paper, and C for Scissors) and our move (X for Rock, Y for Paper, and Z for Scissors).

This game comes in two parts:

  1. The score for a round is the score for the shape you selected (1 for Rock, 2 for Paper, and 3 for Scissors) plus the score of the outcome round (0 if you lose, 3 if you draw, and 6 if you win).
  2. The setup of the game changes. The second column indicates how the round needs to end. X means a loss, Y means a draw, and Z means a win.

If everything follows the guidelines, each part's answer will be the total score. Submit your final answer in the form flag{Part1_Part2}, where Part1 and Part2 are the answers for each part.

Warning
If you're still confused about what's happening here, check the full problem description. It runs some sample testing cases for the challenge so you understand how to compute the totals.

Stuck? Open one hint at a time before reading the walkthrough.

Hint 1
You don't need to keep every round. A running total for each part is enough.
Hint 2
Turn the letters into numbers from 0 to 2: the offset from A or X. Your shape scores 1 + that offset.
Hint 3
Subtract the enemy's move from yours for every pair and look for a pattern: each outcome (win, draw, loss) comes from the same differences, wrapping around in threes.
Hint 4
In part 2 you know the outcome and work backwards to the shape that gives it, using the same pattern.

Walk-through

For each problem, we'll follow the same template. We'll break it down into the following steps:

  1. Choose our data structure based on our end goal for the challenge.
  2. Open the file and initialize our necessary structures.
  3. Read each file line and process the data into the data structure.
  4. Process the data structure to get our answer.

We'll work on each of these in turn.

Choosing a Data Structure

For this challenge, our end goal is to get the total score across the round. Since each round is independent, we don't need to store each round's score. All we need is a running total score for each round. We can do this with a single (unsigned) integer for each round.

We'll find that it's easier to complete these parts independently when we do this challenge. There is not much comparable information other than reading the file, so we'll handle each part separately.

Initialization

In this stage, we'll prepare to read the data. We'll do this the same way we do this in Calorie Counting.

c
1// open file
2FILE *fp = fopen("input.txt", "r");
3if (fp == NULL)

Note that this time, we don't need the ssize_t read variable. We know the size of the data we're reading, so we don't need to store it.

Then, we need to initialize the total score to 0.

c
uint32_t total_score = 0;

Reading the Data

We'll handle this one part at a time.

Part 1

For the first part, we are told the second value is the value the opponent plays. When we compare the two values, it will be easier to convert them to integers first. We can use the offset from A or X as the value for each player (meaning every move corresponds to a 0, 1, or 2).

Why do we think to do this?
Numeric comparisons are typically easier to wrap our head around. Rather than making an if statement with many checks, we can check for patterns with integers.

We can do the same thing for the user score as well. Since the user's move can be seen as 1 + offset, we can add this offset to the total score.

We'll use the same logic as the previous challenge to loop through each line:

c
1while (getline(&line, &len, fp) != -1)
2{
3    ..

In each line, we can get the self and enemy scores. We'll convert them to integers

c
1int enemy = (int)line[0] - 'A';
2int self = (int)line[2]

We'll add 1 + offset for our move's portion of the total score.

c
total_score += (self + 1);

Then, we need to handle the three cases. Let's think about what these cases are. Let's make a table subtracting the enemy score from self.

Enemy / Self012
00 [D]1 [W]2 [L]
1-1 [L]0 [D]1 [W]
2-2 [W]-1 [L]0 [D]

In this case, we notice the following:

  • Wins happen when the result is 1 or -2.
  • Losses happen when the result is -1 or 2.
  • Draws happen when the result is 0.

We can simplify this into the following checks:

c
1int difference = self - enemy;
2
3// draw case
4if (difference == 0)

As a sanity check, we should never use the code after all three checks. I put a printf("Bad\n"); statement at the end to ensure this doesn't get printed.

Once all the lines are iterated for this part, we can return the total score for that round.

Part 2

For the second part, we are told the second value is the game's outcome. X is a loss, Y is a draw, and Z is a win.

There are many ways to approach this challenge. Differentiating the win, loss, and draw cases made the most sense to me. This time, converting them into integers makes less sense because we're not comparing the two values.

We'll use the same logic as the previous challenge to loop through each line:

c
1while (getline(&line, &len, fp) != -1)
2{
3    ..

In each line, we can get the self and enemy scores. We'll leave them as characters.

c
1char enemy = line[0];
2char self = line[2];

Since self represents the score this time, we can add the score results to the total score. The score is three times its value (since the score is 0, 1, or 2).

c
total_score += 3 * ((int)self - 'X');

Now, we can handle the three cases.

  • If the result is a loss (X), and the enemy puts A, we put C (score of 2). For B, we put A; for C, we put B.

    c
    1if (self == 'X')
    2{
    3    if (enemy == 'B' 

Once we've iterated the lines, we can return the total score.

Putting it Together

I chose to make a function for each part. We'll define the main function to call each part's function and print the result.

c
1int main(void)
2{
3    uint32_t first = part01();
4    uint32_t

If we run this, we'll get the following output:

$ gcc -o solve solve.c && ./solveflag{12453_12320}

Full Solution

The full solution is below:

solve.c
1#
4 exit(EXIT_FAILURE);
5
6// initialize reader variables
7char *line = NULL;
8size_t len = 0;
.
4}
-
'X'
;
5{
6 total_score += 3;
7 continue;
8}
9
10// win case
11if (difference == 1 || difference == -2)
12{
13 total_score += 6;
14 continue;
15}
16
17// loss case
18if (difference == -1 || difference == 2)
19{
20 total_score += 0;
21 continue;
22}
.
4}
||
enemy
==
'C'
)
4 total_score += ((int)enemy - 'A');
5 else if (enemy == 'A')
6 total_score += 3;
7 else
8 printf("Bad 1\n");
9}
  • We can use their score for ours if the result is a draw.

    c
    1else if (self == 'Y')
    2{ // total score is what enemy put
    3    total_score += (((int)enemy - 'A') + 1);
    4}
  • If the result is a win, we'll follow a similar strategy to the loss case.

    c
    1else if (self == 'Z')
    2{
    3    if (enemy == 'A' || enemy == 'B')
    4        total_score += (((int)enemy - 'A') + 2);
    5    else if (enemy == 'C')
    6        total_score += 1;
    7    else
    8        printf("Bad 2\n");
    9}
  • We should never hit the else case, but we'll put one just in case.

    c
    1else
    2{
    3    printf("Bad 3\n");
    4}
  • second
    =
    part02
    (
    )
    ;
    5
    6 // print the total score
    7 printf("flag{%d_%d}\n", first, second);
    8
    9 return 0;
    10}
    include
    <stdint.h>
    2#include <stdio.h>
    3#include <stdlib.h>
    4
    5uint32_t part01()
    6{
    7 // open file
    8 FILE *fp = fopen("input.txt", "r");
    9 if (fp == NULL)
    10 exit(EXIT_FAILURE);
    11
    12 // initialize reader variables
    13 char *line = NULL;
    14 size_t len = 0;
    15
    16 // read the file
    17 uint32_t total_score = 0;
    18 while (getline(&line, &len, fp) != -1)
    19 {
    20 int enemy = (int)line[0] - 'A';
    21 int self = (int)line[2] - 'X';
    22
    23 total_score += (self + 1); // score for self move
    24 int difference = self - enemy;
    25
    26 // draw case
    27 if (difference == 0)
    28 {
    29 total_score += 3;
    30 continue;
    31 }
    32
    33 // win case
    34 if (difference == 1 || difference == -2)
    35 {
    36 total_score += 6;
    37 continue;
    38 }
    39
    40 // loss case
    41 if (difference == -1 || difference == 2)
    42 {
    43 total_score += 0;
    44 continue;
    45 }
    46
    47 printf("Bad\n");
    48 }
    49
    50 // print the total score
    51 return total_score;
    52}
    53
    54uint32_t part02()
    55{
    56 // open file
    57 FILE *fp = fopen("input.txt", "r");
    58 if (fp == NULL)
    59 exit(EXIT_FAILURE);
    60
    61 // initialize reader variables
    62 char *line = NULL;
    63 size_t len = 0;
    64
    65 // read the file
    66 uint32_t total_score = 0;
    67 while (getline(&line, &len, fp) != -1)
    68 {
    69 char enemy = line[0];
    70 char self = line[2];
    71
    72 total_score += 3 * ((int)self - 'X'); // score for match result
    73
    74 if (self == 'X')
    75 {
    76 if (enemy == 'B' || enemy == 'C')
    77 total_score += ((int)enemy - 'A');
    78 else if (enemy == 'A')
    79 total_score += 3;
    80 else
    81 printf("Bad 1\n");
    82 }
    83 else if (self == 'Y')
    84 { // total score is what enemy put
    85 total_score += (((int)enemy - 'A') + 1);
    86 }
    87 else if (self == 'Z')
    88 {
    89 if (enemy == 'A' || enemy == 'B')
    90 total_score += (((int)enemy - 'A') + 2);
    91 else if (enemy == 'C')
    92 total_score += 1;
    93 else
    94 printf("Bad 2\n");
    95 }
    96 else
    97 {
    98 printf("Bad 3\n");
    99 }
    100 }
    101
    102 return total_score;
    103}
    104
    105int main(void)
    106{
    107 uint32_t first = part01();
    108 uint32_t second = part02();
    109
    110 // print the total score
    111 printf("flag{%d_%d}\n", first, second);
    112
    113 return 0;
    114}