DEV Community

loading...
Cover image for Daily Challenge #275 - Casino Chips

Daily Challenge #275 - Casino Chips

thepracticaldev profile image dev.to staff ・1 min read

You are given three piles of casino chips: white, green and black chips in the form of an array. Each day you need exactly two chips of different colors to play at the casino. You can chose any color, but you are not allowed to use two chips of the same color in a single day.

You will be given an array representing the number of chips of each color and your task is to return the maximum number of days you can play.

Examples:
solve([1,1,1]) = 1, because after you pick on day one, there will be only one chip left
solve([1,2,1] = 2, you can pick twice; you pick two chips on day one then on day two
solve([4,1,1]) = 2

Brute force is not the way to go here. Look for a simplifying mathematical approach.

Tests:
solve([8,1,4])
solve([7,4,10])
solve([12,12,12])
solve([1,23,2])

Good luck!


This challenge comes from KenKamau on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Discussion (35)

pic
Editor guide
Collapse
masterroshan profile image
TJ Johnson • Edited

This should do it

def solve(chips):
  max_chips = max(chips)
  chips.remove(max_chips)
  remaining_count = sum(chips)
  return min(max_chips, remaining_count)
Enter fullscreen mode Exit fullscreen mode

EDIT: first solution does not work for all possible test cases, this should

def solve(chips):
    chips.sort(reverse=True)
    return min(sum(chips) >> 1, sum(chips[1:]))
Enter fullscreen mode Exit fullscreen mode
Collapse
lopert profile image
lopert

Doesn't this fail the [12,12,12] input?

Collapse
masterroshan profile image
TJ Johnson

The output of solve([12,12,12]) is 12. But you don't have to take my word for it, you can always throw it in the shell and see for yourself.

Collapse
masterroshan profile image
TJ Johnson

ah I see the correct output should be 18

Thread Thread
lopert profile image
lopert

Yes exactly! I just stumbled upon your solution while looking through the comments for what values I should be outputting, and comparing answers to my code.

Your updated solution works great! I'm not sure I get the "math" side of it, as my solution is definitely brute force (recursively remove tokens until we can't anymore).

I get that your code sorts, then returns either the total number of chips / 2 OR the total number of chips in the smaller stacks, whichever is smaller.

Math-wise, I'll take a stab at understanding it.

If we have 1 larger / infinite stack, then we are limited by the other two. We pair a chip from the smaller stacks with one from the large stack everyday, and that's our total. That's the second half of the return statement.

However, if we have stacks that are somewhat even, we can rotate through and deplete them evenly. We code this by taking the total number of chips, and dividing them by the daily chip requirement (we can use the bitwise operator >> here since the daily chip requirement is 2).

Thread Thread
masterroshan profile image
TJ Johnson • Edited

I approached it like this: There's a case where it's better to split the highest stack in half to divvy between the lower 2 stacks, this only happens when the lower 2 stacks add up to be greater than the highest stack, then there are these "leftovers" in the stacks that you can combine.
I ended up with something looking like
high/2 + (mid + low)/2
which can be reduced mathematically to
(high + mid + low)/2
There was a point where it stopped making sense and I was just reducing the math and logic. This was a solution that helped me dev.to/juanrodriguezarc/comment/12o94

Collapse
arnoldarn profile image
ArnoldArn

Interesting topic! I like gambling, especially online casino games without any deposits. It's always interesting to know how these games are made and some of their secrets. For the second goal I go to this site and check different informational articles. I think that the Casinospieles platform is the best review platform. This challenge is really interesting!

Collapse
dora63366916 profile image
Dora • Edited

Personally, I prefer to play different free slots online. I'm not a pro gambler, so I usually play just for fun when I want to relax. My bro shared with me the lit Queen of Hearts slot. The free Queen of Hearts slot with real money option is one of the most popular online casino games, but on this site, you can try it in demo mode. This slot machine, provided by the Novomatic, is not only simple to play; but is a colorful one indeed. On this site, you can check different casino games and their features overview.

Collapse
3642066 profile image
David Augustus

worked on mobile

Collapse
ava41452156 profile image
Ava

I agree with you. Gambling is very interesting but I personally prefer depositpp.com they have great welcome offer and and they support ma favorite pay pal;)

Collapse
qm3ster profile image
Mihail Malo

"constant time" in Javascript

const solve = chips => {
  const [low, mid, high] = [...chips].sort()
  const extra = low & 1
  const rings = low - extra
  const tower = mid - rings
  const last = (high !== mid) & extra
  return rings * 3 + tower + last
}
  1. first we take whole rings, (0-1, 1-2, 2-0)
  2. then we take a the stack of lone pairs ("tower")
  3. finally we see if remainder from rings that's not in the "tower" can last us one more day with remainder of tower.
Collapse
bubbler4 profile image
Bubbler

APL (using Dyalog APL):

      Solve←(⌊2÷⍨+/)⌊(+/-⌈/)
      Solve 1 1 1
1
      Solve 1 2 1
2
      Solve 4 1 1
2
      Solve 12 12 12
18
      Solve 7 4 10
10

(Use the bookmarklet on this post to see the code with APL font and syntax highlighting.)

Explanation: Two possible answers are the sum of two lower values, and half of the entire sum (floored). We need to take the lower of the two.

(⌊2÷⍨+/)⌊(+/-⌈/)  ⍝ Input: an array of 3 numbers
        ⌊         ⍝ Take minimum of two sides...
                  ⍝  Choice 1:
          +/      ⍝   Sum (reduction / by addition +)
            -     ⍝   Minus
             ⌈/   ⍝   Max (reduction / by max-of-two ⌈)
                  ⍝  Choice 2:
     +/           ⍝   Sum
  2÷⍨             ⍝   Divided by 2
 ⌊                ⍝   Floor of the above
Collapse
nocnica profile image
Nočnica Fee

Nice explanation!

Collapse
saviourcode profile image
Sourabh Choure

In C with O(1):

#include <stdio.h>

int solve(int a,int b,int c)
{
    int Days;
    (a>=b)? (b>=c? (Days=c,c-=c):(Days=b,b-=b)):(a>=c? (Days=c,c-=c):(Days=a,a-=a));

    Days+=(a==0) ? (b>=c? c:b):((b==0) ? (a>=c? c:a):((a>=b) ? b:a));

    return Days;
}

int main(void)
{
    printf("Days: %d \n",solve(4,1,1));
    printf("Days: %d \n",solve(8,1,4));
    printf("Days: %d \n",solve(7,4,10));
    printf("Days: %d \n",solve(12,12,12));
    printf("Days: %d \n",solve(1,23,2));
    return 0;
}
Collapse
qm3ster profile image
Mihail Malo

Rust

fn solve(mut chips: [u64; 3]) -> u64 {
    chips.sort_unstable();
    let [low, mid, high] = chips;
    let extra = low & 1;
    let rings = low - extra;
    let tower = mid - rings;
    let last = (high != mid) as u64 & extra;
    rings * 3 + tower + last
}

fn main() {
    assert_eq!(solve([1, 1, 1]), 1);
    assert_eq!(solve([1, 2, 1]), 2);
    assert_eq!(solve([4, 1, 1]), 2);
    dbg!(solve([8, 1, 4]));
    dbg!(solve([7, 4, 10]));
    dbg!(solve([12, 12, 12]));
    dbg!(solve([1, 23, 2]));
}

Look at it go!

Explaination in dev.to/qm3ster/comment/1303g

Collapse
harrisgeo88 profile image
Harris Geo 👨🏻‍💻

My Javascript solution

const solve = (chips) => {
  chips.sort((a,b) => a - b)
  let days = 0

  while(days >= 0) {
    if (chips[0] > 0) {
      days++
      chips[0]--
      chips[2]--
    } else if (chips[1] > 0 && chips[2] > 0) {
      days++
      chips[1]--
      chips[2]--
    } else {
      break; // no chips left! exit the loop
    }
  }


  return days
}
Collapse
sommilee profile image
Lee Seng

You can find very good bonuses at the casino-x. The design of the site is simple and easy to understand, and the fact that there are plenty of bonuses and events is also attractive, and its ease of use is perfect!

Collapse
fusleor profile image
Fusleo Rigaton

Very nice site here! I admit the design is nice but a much better site for casino bonuses I've found was GamblersArea. Their casino section offers anything from cashback to welcome deposits. Probably the best social casino I've played on yet too.

Collapse
nicoray86 profile image
NicoRay86 • Edited

Bonuses are always good news, and not only for casinos. But for a casino, the availability of bonuses is especially important, because thanks to this, there are more winning opportunities. I got dr.bet bonus uk not only for registration and first deposit but also permanently there are other kinds of bonuses. This suits me, so I have been using this source lately.

Collapse
tomwhite1122 profile image
TomWhite1122

Good one, nothing difficult, but interesting. I have website about casino with bonus code bonuscode-casinos.com/ you can check it. All these casino algorithms were quite helpful for me. It's kinda basic staff, but I'm using it a lot. PM me if you need any help with it.

Collapse
lucifarjamse00 profile image
lucifarjamse00

The daily Casino Challenge on this website seems quite interesting, if you are looking for more such challenges then you should try Malaysia online casino free credit if you can afford.

Collapse
savagepixie profile image
SavagePixie

JavaScript

const solve = xs => {
  if (xs[0] === xs[1] && xs[0] === xs[2]) return Math.floor(xs[0] * 1.5)
  xs.sort()
  return Math.min(xs[2], xs[1] + xs[0])
}
Collapse
casinomentor profile image
Tom

I’m a fairly busy person with office work and barely have the time to visit land-based casinos. Therefore, online gambling has always been my top choice. Especially, I'm not a pro gambler, so gambling online for free is always a fun way to relax. My favorite slot so far is probably NetEnt's Starburst slot, especially at this site, I can try it out in demo mode. What makes me love Casinomentor is that the free slots allow me to practice and have fun without worrying about losing my hard-earned money. Players can also check out various casino games and feature an overview of each gambling game at this site.

Collapse
a3 profile image
Ajay

Go #GoLang

package main

import (
    "fmt"
    "sort"
    "math"
)

func main() {
    fmt.Println("Hello, playground");
    conins := []int{12,2,1}
    res := play(conins)
    fmt.Println(res)
}

func play(c []int) int {
    if(c[0] == c[1] && c[1]==c[2] && c[0] == c[2]) {
        return int(math.Floor(float64(c[0])*1.5))
    }
    sort.Ints(c)
    return int(math.Min(float64(c[2]), float64(c[0]+c[1])));
}
Collapse
juanrodriguezarc profile image
Juan Rodríguez
function solve(arr){
    const [a,b,c] = arr.sort((a,b)=> b - a);
    const [x,y] = [a, b+c];
    const z = y > x ? Math.floor((x-y)/2): 0;
    return y + z;
}```

Collapse
minhtn12706743 profile image
MinhTN

code php

function solve(array $arr)
{
$maxDay = 0;
sort($arr);
$checkDuplicateValue = array_unique($arr);
if($arr !== $checkDuplicateValue) {
$maxDay = $arr[0];
return $maxDay;
}
$maxDay = $arr[0] + 1;
return $maxDay;
}

Collapse
kaus2595 profile image
3642066 profile image
David Augustus

its all flash
it sucks

Collapse
shiaz profile image
shiaz

I think that many people like to play and win real money at the same time, especially when there are very real and transparent chances for this, because casinosanalyzer.com/free-slots-onl... provide not only a guarantee of complete security of the game in the online casino, but also unique methods and approaches to each player, play and you will see that it is possible.