top of page
lp.jpeg

Blog

Tags:

DataWeave programming challenge #2: Rock Paper Scissors game score system



 

Other posts from this series:

  1. DataWeave programming challenge #1: Add numbers separated by paragraphs and get the max number

  2. DataWeave programming challenge #2: Rock Paper Scissors game score system

  3. DataWeave programming challenge #3: Count palindrome phrases using the Strings module

  4. DataWeave programming challenge #4: Solve the Tower of Hanoi mathematical puzzle

  5. DataWeave programming challenge #5: Reverse a phrase's words, but keep the punctuation

  6. DataWeave programming challenge #6: Using tail-recursion to get the factorial of a number

  7. DataWeave programming challenge #7: Modify certain values from a JSON structure

  8. DataWeave programming challenge #8: Sum all digits to get a 1-digit number

 

In this post:

 


This challenge is based on Advent of Code 2022 day 2

Try to solve this challenge on your own to maximize learning. We recommend you refer to the DataWeave documentation only. Try to avoid using Google or asking others so you can learn on your own and become a DataWeave expert!




Input


Consider the following input payload (can be of txt format):


R R
R P
R S
P R
P P
P S
S R
S P
S S
R R


Explanation of the problem


Create a DataWeave script to keep your score on a series of Rock Paper Scissors games. The first column is what your opponent chose and the second column is what you chose. Each letter is a representation of the decision made:

  • R = Rock

  • P = Paper

  • S = Scissors

The rules of the game are simple:

  • Rock defeats Scissors

  • Paper defeats Rock

  • Scissors defeat Paper

As per the scoring system, you will have to keep track of whether you won, lost, or if it was a draw. Remember the second column is your choice. Here's how you will be counting the points:

  • 0 points if you lost the round

  • 3 points if the round was a draw

  • 6 points if you won the round


For example,

  • The first round was R R, which means both your opponent and you chose Rock. This round results in a draw, so 3 points are added.

  • The second round was R P, which means your opponent chose Rock and you chose Paper. Paper defeats Rock, which means you win this round. 6 points are added.

  • The third round was R S, which means your opponent chose Rock and you chose Scissors. Rock defeats Scissors, which means you lose this round. 0 points are added.


The final result will be the number of total points you earned in the 10 rounds.



Expected output


In this case, the expected output would be:


30

The result for each of the 10 rounds should be:

  1. 3

  2. 6

  3. 0

  4. 0

  5. 3

  6. 6

  7. 6

  8. 0

  9. 3

  10. 3




Clues


If you're stuck with your solution, feel free to check out some of these clues to give you ideas on how to solve it!


Clue #1

Clue #2

Clue #3

Clue #4

Clue #5

Clue #6

Clue #7

Clue #8



Answer


If you haven't solved this challenge yet, we encourage you to keep trying! It's ok if it's taking longer than you thought. We all have to start somewhere ✨ Check out the clues and read the docs before giving up. You got this!! 💙


There are many ways to solve this challenge, but you can find here some solutions we are providing so you can compare your result with us.


Solution #1 - based on Felix Schnabel's solution

Solution #2

Solution #3


Feel free to comment your code below for others to see! 😄


Subscribe to receive notifications as soon as new content is published ✨








691 views

Categories:

bottom of page