top of page

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

Writer's picture: Alex MartinezAlex Martinez


 

Other posts from this series:

 

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 ✨








1,106 views29 comments

29 Comments


Adam Haynes
Adam Haynes
Dec 25, 2024

It is all about balancing skill development, enjoyment, and focus. Each game requires unique strategies, but understanding mechanics and pacing yourself are universal tips for improvement. Want more insights? So basant club login is a resource to explore expert advice and discover gaming apps that help refine your playstyle. The right app can guide you in mastering techniques while making the process fun and engaging. Start leveling up your gaming journey today with a well-chosen app.

Like

Rahul Dusaje
Rahul Dusaje
Aug 17, 2024

%dw 2.0

import * from dw::core::Strings

output application/json


fun checkPoints(myScore, opponentScore) =


if(opponentScore == "R" and myScore == "S")

total: 6


else if(opponentScore == "R" and myScore == "P")

total: 0


else if(opponentScore == "R" and myScore == "R")

total: 3


else if(opponentScore == "P" and myScore == "P")

total: 3


else if(opponentScore == "P" and myScore == "S")

total: 0


else if(opponentScore == "P" and myScore == "R")

total: 6


else if(opponentScore == "S" and myScore == "S")

total: 3


else if(opponentScore == "S" and myScore == "P")

total: 6


else if(opponentScore == "S" and myScore == "R")

total: 0


else

total: -1


---

sum(((payload splitBy "\n") map ((item, index) -> (item splitBy " "))) map ((item, index) -> checkPoints((item)[0], item[1]).total ))

Like

Kevin Medina
Kevin Medina
Feb 23, 2024

%dw 2.0

import * from dw::core::Arrays

output application/json

fun getPoints(games) =

games splitBy "\n" map ((hands, index) -> 

    do{

        var points = 0

    ---

        hands match {

            case "R R" -> points + 3

            case "R P" -> points + 0

            case "R S" -> points + 6

            case "P P" -> points + 3

            case "P S" -> points + 0

            case "P R" -> points + 6

            case "S S" -> points + 3

            case "S R" -> points + 0

            case "S P" -> points + 6

            else -> 0

        }


    }

    

) sumBy $

---

getPoints(payload)

Like

zainab zaidi
zainab zaidi
Nov 07, 2023

%dw 2.0 output application/json fun calculate( item) = if(item [0] == item [1]) 3 else if((item[0]=='R' and item[1]== 'P') or (item[0]=='S' and item[1]=='R') or item[0]=='P' and item[1]=='S') 6 else 0 --- /* Expected output: 30 */ sum(payload splitBy ("\n") map (item,index) -> calculate(item splitBy(" ") ))

Like

Kelvin Fernandes
Kelvin Fernandes
Oct 20, 2023

%dw 2.0 var ROCK = "R" var PAPER = "P" var SCISSOR = "S" fun getScore(play: Array) = ( do { var opponent = play[0] var me = play[1] --- if((me == ROCK and opponent == SCISSOR) or (me == PAPER and opponent == ROCK) or (me == SCISSOR and opponent == PAPER)) 6 else if(opponent == me) 3 else 0 } ) output application/json --- sum((payload splitBy "\n") map getScore($ splitBy " "))

Like
Alex Martinez
Alex Martinez
Oct 20, 2023
Replying to

woohoo!! thanks for sharing your script!

Like

Join our mailing list

Thanks for subscribing!

  • Youtube
  • GitHub
  • LinkedIn
bottom of page