Coding since 11yo, that makes it over 30 years now ~~~
Have a PhD in Comp Sci ~~~
Love to go on bike tours ~~~
I try to stay as generalist as I can in this crazy wide place coding is at now.
functionscore(dice){// Business rules of the game:// v: the dice value// c: the number of dice required to trigger the rule// p: the points added each time the rule is triggeredconstscoringRules=[{v:1,c:3,p:1000},{v:6,c:3,p:600},{v:5,c:3,p:500},{v:4,c:3,p:400},{v:3,c:3,p:300},{v:2,c:3,p:200},{v:1,c:1,p:100},{v:5,c:1,p:50}]// returns the score given a histogram mapping a dice value to the number of dice with that valueconstscoreForHistogram=counts=>{letscore=0for(const{v,c,p}ofscoringRules){construleCount=Math.floor((counts[v]||0)/c);counts[v]-=ruleCount*c;score+=ruleCount*p;}returnscore;};// Generate the histogram required by scoreForHistogramconstcounts=[...Array(7)].map(v=>0);// note that the rest of the logic uses 1-indexed arrays, so we need 7 entries to get a valid counts[6] entryfor(constvofdice)counts[v]++;returnscoreForHistogram(counts)}
I am a software engineer currently working @ShopPad, previously @HPE. I like to build websites and web application in PHP, JavaScript, and Golang. I have an unhealthy obsession with Mexican food (🌯)
P.S. Hoogle is really useful. I didn't know about the uncurry function, but I knew I needed a function to do what uncurry does, so I could search for (a -> b -> c) -> (a, b) -> c in hoogle!
I love functional programming and feel the whole day like it's Friday
// create random rollsconstroll=()=>Array(5).fill().map(()=>Math.floor(Math.random()*6)+1)// score a rollconstscore=roll=>roll.reduce((a,v)=>a+Math.pow(10,v-1),0).toString().padStart(6,"0").split("").reduce((a,v,i)=>a+[v>2?600:0,(v>2?500:0)+v%3*50,v>2?400:0,v>2?300:0,v>2?200:0,(v>2?1000:0)+v%3*100][i],0)// example scoringtest=(iRoll,iScore)=>`${iRoll.join('')} = ${score(iRoll)} (${score(iRoll)==iScore?'success':'fail'})`console.log(test([5,1,3,4,1],250),test([1,1,1,3,1],1100),test([2,4,4,5,4],450))
its pretty? no... but it works
Very understandable and faster! 👍
Thanks Peter 😃
JS implementation keeping the rules as data
Keep the rules as data is the way to go. 🔥🔥🔥
ruby <3
Perl
JavaScript
Live demo on CodePen.
Easy peasy, lemon squeezy
(language: Elixir)
A super gross unreadable solution but fun none the less 😂
C#
Repl.it
my attempt using Python:
Haskell:
P.S. Hoogle is really useful. I didn't know about the
uncurryfunction, but I knew I needed a function to do whatuncurrydoes, so I could search for(a -> b -> c) -> (a, b) -> cin hoogle!Python
Here is my simple solution with Python:
I love functional programming and feel the whole day like it's Friday
My python sol :
Ruby.