ProstDev ProstDev
Challenges Feb 28, 2023 · 4 min read

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

Create a script to keep your score on a series of Rock Paper Scissors games. 0 if you lose, 3 if it's a draw, and 6 if you win.

By Alex Martinez
Thumbnail: DataWeave programming challenge #2: Rock Paper Scissors game score system Read & copy the full video transcript

Note

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!

Solve on the Playground

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:

  • 3
  • 6
  • 0
  • 0
  • 3
  • 6
  • 6
  • 0
  • 3
  • 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

Using splitBy "\n" is the same as using the lines() function from the Strings module

Clue #2

You can use reduce() to keep the score in the accumulator

Clue #3

If you don’t want to use reduce, you can use map() to get the points of each round and add the numbers at the end

Clue #4

You can use splitBy() to get your opponent’s moves and yours to compare

Clue #5

You can create a variable with the pointing system

Clue #6

The rules can be made of objects in order to extract the points/moves

Clue #7

If you used map() to get the points, you can use sum() to add all the numbers

Clue #8

You can use the do operator to create localized variables

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

%dw 2.0
output application/json
import lines from dw::core::Strings
var defeats = {
    R: "S",
    P: "R",
    S: "P"
}
fun getScore(opponent:String, me:String): Number =
    if (opponent == me) 3
    else if (defeats[me] == opponent) 6
    else 0
---
sum(lines(payload) map getScore($[0],$[-1]))

Solution #2

%dw 2.0
output application/json
var rules = {
    R: {
        R: 3,
        P: 6,
        S: 0
    },
    P: {
        R: 0,
        P: 3,
        S: 6
    },
    S: {
        R: 6,
        P: 0,
        S: 3
    }
}
---
payload splitBy "\n"
reduce ((round, score=0) -> do {
    var arr = round splitBy " "
    var opponent = arr[0]
    var me = arr[-1]
    ---
    score + rules[opponent][me]
})

Solution #3

%dw 2.0
output application/json
import lines from dw::core::Strings
var rules = {
    R: {
        R: 3,
        P: 6,
        S: 0
    },
    P: {
        R: 0,
        P: 3,
        S: 6
    },
    S: {
        R: 6,
        P: 0,
        S: 3
    }
}
---
lines(payload) map do {
    var arr = $ splitBy " "
    ---
    rules[arr[0]][arr[1]]
}
then sum($)

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

Reader notes

Helpful comments preserved from the original post.

  • 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 ))
  • 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)
  • zainab zaidi · Nov 7, 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(" ") ))
  • Kelvin Fernandes · Oct 19, 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 " "))
  • sai teja nallamallli · Jul 13, 2023
    %dw 2.0
    output application/json
    type Rank="S"|"R"|"P"|'s'|'p'|'r'
    fun GetRank(Data : Array<Rank>):Array<Number> = do {
        var Res = Data
        var P1=upper(Res[0]) as Rank
        var P2=upper(Res[1]) as Rank
        ---
        if((P1 == "P") and (P2 == "R")) [6,0] else
        if((P1 == "S") and (P2 == "P")) [6,0] else
        if((P1 == "R") and (P2 == "P")) [6,0] else
        if((P1 == "R") and (P2 == "S")) [0,6] else
        if((P1 == "P") and (P2 == "S")) [0,6] else
        if((P1 == "S") and (P2 == "R")) [0,6] else
        [3,3]
    }
    fun GetWinner(Data : Array<Number>) = if(Data[0] > Data[1]) "Player 1 is The winner Score is $(Data[0])"
    else if(Data[0] > Data[1]) "The Game is Draw With The Score is $(Data[0])"
    else "Player 2 is The winner Score is $(Data[1])"
    ---
    GetWinner(
    (payload splitBy "\n") reduce (item,acc=[0,0]) ->
    do {
        var res = GetRank(item splitBy " ")
        ---
        [acc[0] + res[0] , acc[1] + res[1]]
    }
    )
  • tafkamw · May 2, 2023

    I took the old-fashioned way out 😎

    %dw 2.0
    import * from dw::core::Strings
    
    output application/json
    ---
    lines(payload remove " ") map (
        if ($=="RR" or $=="PP" or $=="SS") 3
        else if ($=="RP" or $=="PS" or $=="SR") 6
        else if ($=="RS" or $=="SP" or $=="PR") 0
        else "what?!"
    )
    reduce ($$ + $)
  • Victor Manuel Rodriguez · Jul 11, 2023
    %dw 2.0
    import * from dw::core::Strings
    var points = {
        "R R": 3,
        "S S": 3,
        "P P": 3,
        "R S": 6,
        "P R": 6,
        "S P": 6
    }
    fun RPSComparer(value) = points[value] default 0
    output application/json
    ---
    lines(payload) reduce ((item, acc=0) -> acc + RPSComparer(item))
  • shwetabh singh · Apr 30, 2023

    sharing my approach below:

    %dw 2.0
    fun getScore(result)= calcScore(((result splitBy " ")[0]),((result splitBy " ")[1]))
    fun calcScore(bot,me)= if(bot == me) 3
    else if (bot == "R" and me == "S")  0
    else if (bot == "R" and me == "P") 6
    else if (bot == "P" and me == "R") 0
    else if (bot == "P" and me == "S") 6
    else if (bot == "S" and me == "P") 0
    else if (bot == "S" and me == "R") 6
    else 0
    output application/json
    ---
    sum(valuesOf(((payload splitBy "\n") map ((item, index) ->{
        score: getScore(item)
    } ))reduce ($$ ++ $ ))
  • mysg147 · Apr 22, 2023
    %dw 2.0
    output application/json
    var x = payload splitBy('\n')
    var y =x map ((val,in) -> if(val == "R R" or val == "S S" or val == "P P") 3
    else if(val == "R P" or val == "P S" or val == "S R") 6 else 0
    )
    ---
    sum(y)
  • prasadrnithin · Mar 2, 2023
    %dw 2.0
    output application/json
    import * from dw::core::Strings
    fun rps(inValue) =
        if(['R P','P S','S R'] contains inValue)
            6
        else if(['P R','S P','R S'] contains inValue)
            0
        else
            3
    ---
    sum(lines(payload) map (rps($)))
  • BESIDE serious · Mar 2, 2023
    %dw 2.0
    import * from dw::core::Strings
    output application/json
    fun myScore(moves :Any)= do{
        var steps =  moves splitBy " "
        ---
        log(if(steps[0]==steps[1]) 3 else if (["S R","R P","P S"] contains(moves) ) 6 else 0)
    }
    ---
    sum(valuesOf(((lines(payload) map (() -> {
        ($): myScore($)
    } )))))
  • sksalmanhaider · Mar 1, 2023
    %dw 2.0
    fun calculateMyScore(inData) =
      (inData splitBy "\n") reduce (stringOfChoices, myTotalScore = 0) -> do {
        var opponentChoice = stringOfChoices[0]
        var myChoice = stringOfChoices[2]
        var myWinningCombos = ["SR", "RP", "PS"]
        ---
        if (opponentChoice == myChoice)
          myTotalScore + 3
        else if (myWinningCombos contains (opponentChoice ++ myChoice))
          myTotalScore + 6
        else
          myTotalScore
      }
    output json
    ---
    calculateMyScore(payload)
  • Vikash · Feb 28, 2023

    Just a kind of static & probability...!!

    %dw 2.0
    var opponent = payload splitBy  "\n" map ($ splitBy  " ")[0]
    var you = payload splitBy  "\n" map ($ splitBy  " ")[1]
    var result = payload splitBy  "\n"  map(v,i) ->
    {
        result: if(opponent[i] == you[i]) 3 else if((opponent[i] == "R") and (you[i] == "S")) 0 else if((opponent[i] == "R") and (you[i] == "P")) 6 else if((opponent[i] == "S") and (you[i] == "P")) 0 else if((opponent[i] == "S") and (you[i] == "R")) 6 else if((opponent[i] == "P") and (you[i] == "S")) 6 else if((opponent[i] == "P") and (you[i] == "R")) 0 else 0
    }
    output application/json
    ---
    sum(result.result)
  • Shyam Raj Prasad · Feb 28, 2023

    Just sharing my approach:

    %dw 2.0
    output application/json
    var scoring ={
        "R R" : 3,
        "R P" : 6,
        "R S" : 0,
        "S R" : 6,
        "S S" : 3,
        "S P" : 0,
        "P R" : 0,
        "P P" : 3,
        "P S" : 6
    }
    ---
    sum(payload splitBy  "\n" map (
        scoring[$] default 0
    ))

FAQs

Frequently asked questions about this post.

  • What is the goal of this DataWeave challenge?

    The goal is to create a DataWeave script that keeps your score across a series of Rock Paper Scissors games. The input has two columns where the first column is what your opponent chose and the second column is what you chose, and the script adds up your points over all 10 rounds.

  • How are points scored in this challenge?

    You earn 0 points if you lost the round, 3 points if the round was a draw, and 6 points if you won the round. The game rules are that Rock defeats Scissors, Paper defeats Rock, and Scissors defeat Paper, with R meaning Rock, P meaning Paper, and S meaning Scissors.

  • What is the expected output for the sample input?

    For the given input the expected output is 30, which is the total of the 10 rounds scoring 3, 6, 0, 0, 3, 6, 6, 0, 3, and 3.

  • How do I split the input into individual rounds and moves?

    You can use splitBy with the newline character to break the payload into rounds, which is the same as using the lines() function from the Strings module, and then use splitBy() again on each round to separate your opponent's move and yours so you can compare them.

  • How can I keep a running total of the score?

    You can use reduce() to keep the score in the accumulator, or if you prefer you can use map() to get the points of each round and then use sum() to add all the numbers together at the end.

  • Where can I solve this challenge and read the reference?

    You can solve it on the DataWeave Playground at https://dataweave.mulesoft.com/learn/playground?projectMethod=GHRepo&repo=alexandramartinez%2Fdataweave-challenges&path=challenges%2F2 and refer only to the DataWeave documentation at https://docs.mulesoft.com/dataweave/latest/ as you work through it. The challenge is based on Advent of Code 2022 day 2 at https://adventofcode.com/2022/day/2

More from this series

DataWeave Programming Challenges· Part 2 of 8
  1. 1.DataWeave programming challenge #1: Add numbers separated by paragraphs and get the max number
  2. 2.DataWeave programming challenge #2: Rock Paper Scissors game score system
  3. 3.DataWeave programming challenge #3: Count palindrome phrases using the Strings module
  4. 4.DataWeave programming challenge #4: Solve the Tower of Hanoi mathematical puzzle
  5. 5.DataWeave programming challenge #5: Reverse a phrase's words, but keep the punctuation
  6. 6.DataWeave programming challenge #6: Using tail-recursion to get the factorial of a number
  7. 7.DataWeave programming challenge #7: Modify certain values from a JSON structure
  8. 8.DataWeave programming challenge #8: Sum all digits to get a 1-digit number
Search

Loading search…