Other posts from this series:
DataWeave programming challenge #1: Add numbers separated by paragraphs and get the max number
DataWeave programming challenge #2: Rock Paper Scissors game score system
DataWeave programming challenge #3: Count palindrome phrases using the Strings module
DataWeave programming challenge #4: Solve the Tower of Hanoi mathematical puzzle
DataWeave programming challenge #5: Reverse a phrase's words, but keep the punctuation
DataWeave programming challenge #6: Using tail-recursion to get the factorial of a number
DataWeave programming challenge #7: Modify certain values from a JSON structure
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:
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!
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.
Feel free to comment your code below for others to see! 😄
Subscribe to receive notifications as soon as new content is published ✨
%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 ))
%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)
%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(" ") ))
%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 " "))
%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 r…