r/dailyprogrammer • 2 3 • Nov 11 '19

[2019-11-11] Challenge #381 [Easy] Yahtzee Upper Section Scoring

Description

The game of Yahtzee is played by rolling five 6-sided dice, and scoring the results in a number of ways. You are given a Yahtzee dice roll, represented as a sorted list of 5 integers, each of which is between 1 and 6 inclusive. Your task is to find the maximum possible score for this roll in the upper section of the Yahtzee score card. Here's what that means.

For the purpose of this challenge, the upper section of Yahtzee gives you six possible ways to score a roll. 1 times the number of 1's in the roll, 2 times the number of 2's, 3 times the number of 3's, and so on up to 6 times the number of 6's. For instance, consider the roll [2, 3, 5, 5, 6]. If you scored this as 1's, the score would be 0, since there are no 1's in the roll. If you scored it as 2's, the score would be 2, since there's one 2 in the roll. Scoring the roll in each of the six ways gives you the six possible scores:

0 2 3 0 10 6

The maximum here is 10 (2x5), so your result should be 10.

Examples

yahtzee_upper([2, 3, 5, 5, 6]) => 10
yahtzee_upper([1, 1, 1, 1, 3]) => 4
yahtzee_upper([1, 1, 1, 3, 3]) => 6
yahtzee_upper([1, 2, 3, 4, 5]) => 5
yahtzee_upper([6, 6, 6, 6, 6]) => 30

Optional Bonus

Efficiently handle inputs that are unsorted and much larger, both in the number of dice and in the number of sides per die. (For the purpose of this bonus challenge, you want the maximum value of some number k, times the number of times k appears in the input.)

yahtzee_upper([1654, 1654, 50995, 30864, 1654, 50995, 22747,
    1654, 1654, 1654, 1654, 1654, 30864, 4868, 1654, 4868, 1654,
    30864, 4868, 30864]) => 123456

There's no strict limit on how fast it needs to run. That depends on your language of choice. But for rough comparison, my Python solution on this challenge input, consisting of 100,000 values between 1 and 999,999,999 takes about 0.2 seconds (0.06 seconds not counting input parsing).

If you're preparing for a coding interview, this is a good opportunity to practice runtime complexity. Try to find a solution that's linear (O(N)) in both time and space requirements.

Thanks to u/Foenki for inspiring today's challenge in r/dailyprogrammer_ideas!

204 Upvotes

238 comments sorted by

12

u/meepmeep13 Nov 12 '19 edited Nov 12 '19

Python 3 one-liner:

def yahtzee_upper(roll): return max([roll.count(x+1)*(x+1) for x in range(max(roll))])

4

u/meepmeep13 Nov 12 '19

slightly more efficient (it only evaluates rolls that have actually happened) and briefer:

def yahtzee_upper(r): return max([r.count(x)*x for x in set(r)])

3

u/Gprime5 Nov 14 '19

You can make it more efficient and briefer.

def yahtzee_upper(r): return max(r.count(x)*x for x in set(r))

10

u/Gylergin Nov 12 '19 edited Nov 12 '19

TI-Basic: L₁ is the roll list and L₂ is the score list

Prompt L₁
SortA(L₁
{L₁(1→L₂
For(X,2,dim(L₁
If L₁(X)=L₁(X-1
Then
L₁(X)+L₂(dim(L₂→L₂(dim(L₂
Else
L₁(X→L₂(1+dim(L₂
End
End
Disp max(L₂
ClrList L₁,L₂

Edit: no need to sort L₂ when max( exists

→ More replies (1)

9

u/lukz 2 0 Nov 16 '19 edited Nov 16 '19

Z80 assembly

This is a program for Sharp MZ-800 computer. I don't do the user input and assume that the input data are stored in memory at addresses (2003h - 2007h). When run from address 2000h the program calculates the best score and prints it as two digits to the screen.

After loading the program we can go to monitor and enter the input using the M command. Then we can run the program from monitor using the G command.

Sample session:

The * is the monitor prompt

*M2003
2003 02 01
2004 03 01
2005 05 01
2006 10 01
2007 06 03
2008 21    <press shift+Break>
*G2000
04

Imgur

Code:

.org 2000h
  jr start
  .db 0           ; helper value
input:
  .db 2,3,5,5,6   ; dice rolls

start:
  ; do cummulative sum of the same numbers
  ld hl,input-1
  sub a
  ld b,6     ; list of 6 values
sum:
  cp (hl)
  jr z,same
  ld a,(hl)
  ex af,af'
  sub a
  ex af,af'
same:
  ex af,af'
  add a,(hl)
  daa
  ld (hl),a
  ex af,af'
  inc l
  djnz sum   ; loop

  ; find maximum
  ld hl,input
  sub a
  ld b,5     ; list of 5 values
max:
  cp (hl)
  jr nc,$+3
  ld a,(hl)
  inc l
  djnz max   ; loop

  ; print maximum and exit
  jp 3c3h    ; @bthex
             ; prints a hex byte

7

u/zmm0 Nov 15 '19

x86_64. Runs in O(N). Takes 50ms on the challenge input.

extern calloc
extern malloc
extern free
extern printf
extern fopen
extern fclose
extern fread
extern strtok
extern exit
extern strtol
extern GetTickCount

global main

%define NUM_ROLLS 100_000
%define HASH_SIZE (NUM_ROLLS * 10 + 1)
%define BUFFER_SIZE 100_000_000

; hash table element
%define ELEMENT_OFFSET 0
%define VALUE_OFFSET 8
%define HASH_ELEMENT_SIZE 16


section .text
main:
    push rbp
    push rbx
    push r12
    push r13
    push r14
    sub rsp, 0x30

    call GetTickCount
    mov r14d, eax

    mov rcx, HASH_SIZE
    call startHashTable
    mov rbp, rax

    ; open file
    mov rcx, FILE_NAME
    mov rdx, FILE_MODE
    call fopen
    cmp rax, 0
    jne .goodOpenFile
        mov rcx, BAD_OPEN_FILE
        call printf
        mov ecx, 1
        call exit
.goodOpenFile:
    mov rbx, rax

    ; read file
    mov ecx, BUFFER_SIZE
    call malloc
    cmp rax, 0
    jne .mallocGood
        mov rcx, BAD_ALLOC
        call printf
        mov ecx, 1
        call exit
.mallocGood:
    mov r13, rax

    mov rcx, r13
    mov edx, 1
    mov r8d, BUFFER_SIZE
    mov r9, rbx
    call fread
    cmp eax, BUFFER_SIZE
    jl .fullFileRead
        mov rcx, FILE_TOO_LARGE
        call printf
        mov ecx, 1
        call exit

.fullFileRead:

    xor r12d, r12d
    mov rcx, r13
    mov rdx, DELIMITER
    call strtok
.loop:
        cmp rax, 0
        je .endLoop

        mov rcx, rax
        lea rdx, [rsp + 0x20]
        mov r8d, 10
        call strtol

        mov rcx, rax
        mov rdx, r12
        mov r8, rbp
        call storeInHashTable
        mov r12, rax

        xor ecx, ecx
        mov rdx, DELIMITER
        call strtok
        jmp .loop

.endLoop:

    mov rcx, STRING_INT
    mov rdx, r12
    call printf

    call GetTickCount
    sub eax, r14d
    mov rcx, TIME
    mov edx, eax
    call printf

    mov rcx, rbx
    call fclose
    mov rcx, rbp
    call free
    mov rcx, r13
    call free
    xor eax, eax
    add rsp, 0x30
    pop r14
    pop r13
    pop r12
    pop rbx
    pop rbp
    ret
    

; rcx: hash table size
startHashTable:
    sub rsp, 0x28

    mov edx, HASH_ELEMENT_SIZE
    call calloc
    cmp rax, 0
    jne .goodCalloc
        mov rcx, BAD_ALLOC
        call printf
        mov ecx, 1
        call exit
.goodCalloc:
    add rsp, 0x28
    ret


; rcx: store value
; rdx: current max
; r8: hash table
; return rax: new max
storeInHashTable:
    push rsi
    push rdi
    sub rsp, 0x28

    ; hash the value
    mov rdi, rcx
    mov rsi, rdx
    xor edx, edx
    mov rax, rdi
    mov r9, HASH_SIZE
    div r9

    ; find table spot
    mov rcx, rdx
.loop:
        shl rcx, 4
        mov r10, [r8 + rcx]
        cmp r10, 0
        jne .notEmpty
            mov [r8 + rcx], rdi
            jmp .offsetFound
.notEmpty:
        cmp r10, rdi
        je .offsetFound



        ; get next offset to test
        shr rcx, 4
        inc rcx
        cmp rcx, HASH_SIZE
        jl .noCycleAround
            xor ecx, ecx

.noCycleAround:
        cmp rcx, rdx
        jne .loop
            mov rcx, HASH_TABLE_OUT_OF_SPACE
            call printf
            mov ecx, 1
            call exit

.offsetFound:
    mov r10, [r8 + rcx + VALUE_OFFSET]
    add r10, rdi
    mov [r8 + rcx + VALUE_OFFSET], r10

    cmp r10, rsi
    jle .oldMax
        mov rsi, r10
.oldMax:
    mov rax, rsi

    add rsp, 0x28
    pop rdi
    pop rsi
    ret


section .rdata
BAD_ALLOC: db "Not enough memory", 10, 0
BAD_OPEN_FILE: db "Could not open file", 10, 0
FILE_TOO_LARGE: db "File too large for buffer", 10, 0
FILE_NAME: db "yahtzeeUpper1.txt", 0
FILE_MODE: db "r", 0
STRING_INT: db "%lli", 10, 0
TIME: db "Time: %ims", 10, 0
HASH_TABLE_OUT_OF_SPACE: db "Hash table out of space", 10, 0
DELIMITER: db " ,", 10, 13, 0

6

u/[deleted] Nov 11 '19 edited Nov 11 '19

Clojure, github input takes about 50msec

(def test-input (->> (s/split-lines (slurp "./resources/yahtzee.txt"))
                     (map read-string)))

(defn score [[k v]] (* k (count v)))

(defn max-score [dice]
  (->> dice
       (group-by identity)
       (apply max-key score)
       ((comp (partial reduce +) second))))

3

u/2cow Nov 12 '19 edited Nov 12 '19

Just wanted to make sure you know about this function, since it's such a natural fit for this problem. Apologies if you already knew about it and decided to avoid it, but I was just very surprised to see a Clojure solution that didn't use it.

(reduce #(max %1 (apply * %2)) 0 (frequencies dice))

3

u/[deleted] Nov 12 '19

yup you're right just forgot about it :D

6

u/Gprime5 Nov 12 '19

Python 3 Runs in 20ms on the 100,000

def yahtzee_upper(roll):
    scores = {}
    
    for score in roll:
        scores[score] = scores.get(score, 0) + score

    return max(scores.values())

5

u/tomekanco Nov 12 '19

Python, 180 ms: dominated by read, 40 ms for counter+max.

from collections import Counter

def yahtzee_upper(inx): 
    return max(k*v for k,v in Counter(inx).items())   

def problem(file):
    with open(file) as f:
        return yahtzee_upper(int(x) for x in f.readlines())

5

u/sameer_sinha Nov 12 '19

Scala with bonus

import scala.io.Source
import scala.collection.SortedSet

val lines = Source.fromFile("yahtzee-upper-1.txt").getLines.toList.map(_.toLong)

def yahtzee_upper(a: List[Long]): Long = {
    val sa = a.sorted
    val aSet = SortedSet(sa: _*)
    aSet.map(x => sa.count(_ == x) * x).max
}

duration for the 100,000 values list = 0.8 seconds

0

u/[deleted] Nov 12 '19

[deleted] — view deleted comment

2

u/sameer_sinha Nov 13 '19 edited Nov 13 '19

Definitely a much better way of calculating the same thing but
does your bonus give the correct answer with the return type as Int?
Because the answer of 31415926535 is too big for an Int.

→ More replies (1)

5

u/[deleted] Nov 15 '19 edited Dec 01 '19

Shell (with gnu coreutils)

I downloaded the yahtzee-upper-1.txt file and put it into a file called input.txt, and the I/O, parsing of the numbers and then generating the output took 0.05s on my machine:

cat input.txt | sort -n | uniq -c | awk '{ print $1*$2 }' | sort -rn | head -n1

(Edit: shorter version: sort -n input.txt|uniq -c|awk '{print$1*$2}'|sort -n|tail -n1)

4

u/g00glen00b Nov 11 '19 edited Nov 12 '19

Java:

private static long yahtzee(Collection<Integer> values) {
    return values
        .stream()
        .collect(groupingBy(identity(), summingLong(Long::valueOf)))
        .values()
        .stream()
        .max(naturalOrder())
        .orElse(0L);
}

Also, could you tell us what the result would be of the large file? Currently I get 31,415,926,535 within 0.017 seconds (excluded I/O time of reading the text-file).

1

u/CoDBorn Nov 12 '19

I got the same result.

4

u/btingle Nov 16 '19

Python3 - O(n) straightforward solution

def yahtzee_upper(roll):
	n_table = {}
    for n in roll:
	    if not n_table.get(n):
		    n_table[n] = n
	    else:
		    n_table[n] += n
    return max(n_table.values())

2

u/McTeazy Nov 17 '19

Nice solution. Can you use .setdefault() instead of if/else? which is better? ex:

def yahtzee_upper(rolls: list):
    score = {}
    for roll in rolls:
        score.setdefault(roll, 0)
        score[roll] += roll
    return max(score.values())

2

u/btingle Nov 17 '19 edited Nov 17 '19

Alternatively, you could use a defaultdict. Like so:

from collections import defaultdict

def yahtzee_upper(rolls):
    score = defaultdict(int)
    for roll in rolls: score[roll] += roll
    return max(score.values())

Now it's only three lines of code!! Of course LOC doesn't really matter, this yahtzee_upper function and the previous one I posted do the exact same thing, in basically the exact same amount of time. But it is fun to bring the linecount down!

→ More replies (1)

4

u/Stickycover Nov 17 '19

Python - O(n)

There's a lot of good solutions but I wanted to put mine in there to highlight how a dict's setdefault(key, default_value) is useful for filling dictionaries like this.

# yahtzee_upper challenge


def yahtzee_upper(roll):
    basic_dict = {}
    for i in roll:
        # If the key doesn't already exist, this adds { i: 0 } to initialize. Avoids if-else.
        basic_dict.setdefault(i, 0)
        basic_dict[i] += i
    print(max(basic_dict.values()))


yahtzee_upper([2, 3, 5, 5, 6])  # 10
yahtzee_upper([1, 1, 1, 1, 3])  # 4
yahtzee_upper([1, 1, 1, 3, 3])  # 6
yahtzee_upper([1, 2, 3, 4, 5])  # 5
yahtzee_upper([6, 6, 6, 6, 6])  # 30
yahtzee_upper([1654, 1654, 50995,
               30864, 1654, 50995,
               22747, 1654, 1654,
               1654, 1654, 1654,
               30864, 4868, 1654,
               4868, 1654, 30864,
               4868, 30864])  # 123456

4

u/NemPlayer Nov 23 '19 edited Dec 18 '19

Python 3.8 (with bonus)

Time complexity: O(N^2 log n)

from collections import Counter

def yahtzee_upper(rolls):
    return max(map(lambda x: x[0]*x[1], Counter(rolls).most_common()))

Improved

Time complexity: O(N)

Instead of using map on the tuples generated, it's possible to just use a generator expression and instead of using the most_common method, it's possible to use the items method making it both faster and have a much better time complexity.

from collections import Counter

def yahtzee_upper(rolls):
    return max(x*y for x, y in Counter(rolls).items())

2

u/Little_Danson_Man Nov 25 '19

Hey this is a great solution thank you for showing me Counter

4

u/29a75HHH Nov 29 '19
import random as rt
import time as t

# Prompt user for number of rolls and the range of the die
n = int(input("Enter the number of rolls: \n"))
r = int(input("Enter the highest number on the die: \n"))

#Create the list
l = list(rt.randint(1, r) for i in range(n))
d = {}

# Key-value dictionary that essentially "tallies" each number's occurrence
t1 = t.time()
for i in range(n):
    if l[i] not in d:
        d[l[i]] = 1
    else:
        d[l[i]] += 1

# Find the max number of the number multiplied by its respective occurrences
yahtzee_upper = max(map(lambda x,y: x*y, d, list(d[l[i]] for i in range(n))))
t2 = t.time()

# Print the final score, and the time for calculation
print(yahtzee_upper)
print(t2 - t1)

This is done in Python. The complexity is O(N) and the time for 100,000 values between 1 and 999,999,999 has been practically 0.06 without parsing input. Feedback and compliments are both welcome and appreciated.

4

u/voidhawk42 Dec 06 '19

Dyalog APL:

{⌈/×/,∘≢⌸⍵}

Handles large, unsorted input. Runs in O(n).

3

u/ka-splam Dec 17 '19

I made:

⌈/{⍺×≢⍵}⌸    nums

but I think it can be golfed to:

⌈/×∘≢⌸     nums

3

u/twisted-teaspoon Nov 12 '19 edited Nov 12 '19

Python3

from collections import defaultdict

def yahtzee_upper(values):
    max_score = 0
    value_dict = defaultdict(int)
    for v in values:
        value_dict[v] += v
        max_score = max(max_score, value_dict[v])
    return max_score

with open('yahtzee-upper-1.txt', 'r') as f:
    values = [int(s) for s in f.read().strip().split('\n')]

print(yahtzee_upper(values))

3

u/lollordftw Nov 12 '19

Haskell

Unfortunately it's O(n*log(n)). Runs in about 190 ms.

Does anyone have an idea on how to solve this problem in O(n) without the use of Hash Maps?

module Main where

import qualified Data.Map.Strict as M
import           Data.List (foldl')

yahtzee_upper :: [Int] -> Int
yahtzee_upper = maxValue . foldl' (\m x -> M.insertWith (+) x x m) M.empty

maxValue :: (Ord a, Num a) => M.Map k a -> a
maxValue = M.foldl' max 0

testLargeInput :: IO Int
testLargeInput = readFile "inp-easy.txt" >>= return . yahtzee_upper . map read . lines

main :: IO ()
main = testLargeInput >>= putStrLn . show

3

u/qZeta Nov 12 '19

Some remarks:

  • putStrLn . show is print
  • x >>= return . f . g is f . g <$> x
→ More replies (1)
→ More replies (6)

3

u/olzd Nov 12 '19

(g)AWK:

gawk '{a[$0]+=$0} END {print a[asort(a)]}'

3

u/hyrulia Nov 12 '19

Kotlin

fun yahtzee(inputs: IntArray) = (1 .. 6).map { i -> inputs.count { it == i } * i }.max()

3

u/[deleted] Nov 12 '19

Python 3

def yahtzee_upper(r):
    return max(i * r.count(i) for i in set(r))


assert yahtzee_upper([2, 3, 5, 5, 6]) == 10
assert yahtzee_upper([1, 1, 1, 1, 3]) == 4
assert yahtzee_upper([1, 1, 1, 3, 3]) == 6
assert yahtzee_upper([1, 2, 3, 4, 5]) == 5
assert yahtzee_upper([6, 6, 6, 6, 6]) == 30


def yahtzee_upper_bonus(r):
    n = {}
    for i in r:
        n[i] = n.get(i, 0) + i

    return max(n.values())


if __name__ == "__main__":
    import sys
    import time

    rolls = [int(x) for x in sys.stdin.readlines()]
    s = time.time()
    yahtzee_upper_bonus(rolls)
    print(f"{time.time() - s:.5f} seconds")

Time for the bonus:

$ curl -s https://gist.githubusercontent.com/cosmologicon/beadf49c9fe50a5c2a07ab8d68093bd0/raw/fb5af1a744faf79d64e2a3bb10973e642dc6f7b0/yahtzee-upper-1.txt | python e381.py
0.02802 seconds

3

u/dontforget512 Nov 12 '19 edited Nov 13 '19

PowerShell!

function yahtzee_upper($Rolls)
{
    $Hash = @{}
    Foreach ($Roll in $Rolls)
    {$Hash[$Roll]++}
    
    $HighestScore = 0
    Foreach ($Key in $Hash.Keys)
    {
        $Score = $Hash[$Key] * $Key
        if ($Score -gt $HighestScore)
        {$HighestScore = $Score}
    }
    return $HighestScore
}

$100000 = Get-Content "C:\Users\$ENV:Username\Desktop\yahtzee-upper-1.txt" | ForEach-Object {[int]$_}

Measure-Command {yahtzee_upper $100000 | Out-Default}

31415926535

TotalMilliseconds : 22.4955

1

u/ka-splam Dec 17 '19

I set about rewriting this faster. I can't make it faster, which I am surprised and impressed at. Collections.Generic.Dictionary[long, long] obviously, it won't box the numbers into objects. Use ${d:\data.txt} file reading trick, instead of ++ use += to do the sum of each thing on an ongoing basis, sort-object and group-object from PSv7 that'll be some fast C# code .. 800ms!

What.

OK, scrap grouping even with -NoElement, scrap the sorting, too much overhead, move the file reading outside the timing like you did, now it will be rapid.. 150ms!

What.

Nothing I could do; the only changes which made it faster were more like your code, until your code is what I had for the same performance .. and that's it. The only possible improvement I can find is $Hash[$Key] -> $Hash.$Key and that's so small and intermittent I'm not sure it's not measurement error.

I can only make it shorter (but much slower):

(${d:\data.txt}|group|%{$_.count*$_.name}|sort)[-1]

3

u/jonnywoh Nov 13 '19 edited Nov 13 '19

C#, with bonus

using System.Linq;

int yahtzee_upper(IEnumerable<int> roll) => 
    roll.GroupBy(x => x)
        .Select(x => x.Key * x.Count())
        .Max();

Edit: a complete program with no error handling:

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static int YahtzeeUpper(IEnumerable<int> roll) =>
        roll.GroupBy(x => x)
            .Select(x => x.Key * x.Count())
            .Max();
    
    static void Main(string[] args)
    {
        if(args.Length == 0)
        {
            Console.WriteLine("Usage:");
            Console.WriteLine("\tyahtzeeupper.exe <int...>");
        }
        else
        {
            Console.WriteLine(YahtzeeUpper(args.Select(int.Parse)));
        }
    }
}

Edit: much faster version (but with similar complexity):

static int YahtzeeUpper2(IEnumerable<int> roll)
{
    var dict = new Dictionary<int, int>();

    foreach(int i in roll)
    {
        if(dict.ContainsKey(i))
        {
            dict[i]++;
        }
        else
        {
            dict[i] = 1;
        }
    }

    return dict.Values.Max();
}

3

u/raevnos Nov 23 '19 edited Nov 24 '19

GNU Prolog:

/* Compile with: gplc --no-top-level daily.pro  -*- prolog -*- */

ones(Lst, Ones) :- subtract(Lst, [2,3,4,5,6], Ones).
twos(Lst, Twos) :- subtract(Lst, [1,3,4,5,6], Twos).
threes(Lst, Threes) :- subtract(Lst, [1,2,4,5,6], Threes).
fours(Lst, Fours) :- subtract(Lst, [1,2,3,5,6], Fours).
fives(Lst, Fives) :- subtract(Lst, [1,2,3,4,6], Fives).
sixes(Lst, Sixes) :- subtract(Lst, [1,2,3,4,5], Sixes).

score(Dice, Val, Result) :-
    length(Dice, Len),
    Result = Val * Len.

yahtzee_upper(Dice, Result) :-
    ones(Dice, Ones),
    score(Ones, 1, SOne),
    twos(Dice, Twos),
    score(Twos, 2, STwo),
    threes(Dice, Threes),
    score(Threes, 3, SThree),
    fours(Dice, Fours),
    score(Fours, 4, SFour),
    fives(Dice, Fives),
    score(Fives, 5, SFive),
    sixes(Dice, Sixes),
    score(Sixes, 6, SSix),
    max_list([SOne, STwo, SThree, SFour, SFive, SSix], Result).

test(Dice, Expected) :-
    yahtzee_upper(Dice, Result),
    Result =:= Expected.

tests :-
    test([2, 3, 5, 5, 6], 10),
    test([1, 1, 1, 1, 3], 4),
    test([1, 1, 1, 3, 3], 6),
    test([1, 2, 3, 4, 5], 5),
    test([6, 6, 6, 6, 6], 30),
    write('All tests passed.'),
    nl.

:- initialization(tests).

3

u/ruincreep Nov 26 '19 edited Nov 29 '19

Raku (Perl 6)

words.Bag.kv.map(* * *).max.say

Takes ~0.7s for the bonus input including parsing.

Without parsing it's ~0.14s (there's very high I/O load on my server):

my @w = words;

{
  @w.Bag.kv.map(* * *).max.say;
  say now - ENTER now;
}

3

u/onesweetsheep Mar 11 '20 edited Mar 11 '20

This is my beginner Python attempt:

yahtzee_roll = [1, 1, 1, 4, 6]

def count_as(n, roll):
    sum_of_n = 0
    if n in roll:
        for number in roll:
            if number == n:
                sum_of_n += n
        return sum_of_n
    else:
        return sum_of_n

max_result = 0
all_results = [0, 0, 0, 0, 0, 0]

def yahtzee_upper(roll):
    global all_results
    dice_side = 1
    while dice_side <= 6:
          all_results[dice_side - 1] = count_as(dice_side, roll)
          dice_side += 1
    global max_result
    max_result = max(all_results)

yahtzee_upper(yahtzee_roll)
print(all_results)
print (max_result)

I'm sure it's not an elegant solution, but it seems to work. Happy for any feedback! :)

3

u/MrTyranius Apr 10 '20

Python that allows you to adjust the numbers on the dice and how many times you roll.

import random as rand
import time
start_time = time.time()
diceRolls = []
numRolls = 5
lowRoll = 1
highRoll = 6
for x in range(0,numRolls):
    diceNum = rand.randint(lowRoll, highRoll)
    diceRolls.append(diceNum)
diceRolls.sort()
# print(diceRolls)
def scoreRoll(list):
    numOfNums = []
    scores = []
for i in range(lowRoll,highRoll + 1):
        value = list.count(i)
        numOfNums.append(value)
#print(numOfNums)
    j = lowRoll
for k in range(0,len(numOfNums)):

        score = (j) * numOfNums[k] 
if score != 0: 
            scores.append(score)
        j += 1
# print(scores)
return max(scores)        
#print(scores)
maxScore = scoreRoll(diceRolls)
print(maxScore)
# print("------ %s seconds -------" % round(time.time() - start_time, 5))

2

u/skeeto -9 8 Nov 11 '19 edited Nov 12 '19

C with bonus, reading from stdin. It completes the bonus input in about 10ms.

#include <stdio.h>
#include <stdlib.h>

struct table {
    size_t len;
    size_t cap;
    struct {
        int value;
        int count;
    } slots[];
};

static struct table *
create(size_t cap)
{
    struct table *t = calloc(sizeof(*t) + sizeof(t->slots[0])*cap, 1);
    t->cap = cap;
    return t;
}

static void
tally(struct table *t, int value, int count)
{
    size_t i = value & (t->cap - 1);
    while (t->slots[i].value && t->slots[i].value != value)
        i = (i + 1) & (t->cap - 1);
    t->slots[i].value = value;
    t->len += !t->slots[i].count;
    t->slots[i].count += count;
}

static struct table *
grow(struct table *t)
{
    struct table *new = create(t->cap * 2);
    for (size_t i = 0; i < t->cap; i++)
        if (t->slots[i].value)
            tally(new, t->slots[i].value, t->slots[i].count);
    free(t);
    return new;
}

int
main(void)
{
    struct table *t = create(64);

    int value;
    while (scanf("%d", &value) == 1) {
        if (t->len > t->cap / 2)
            t = grow(t);
        tally(t, value, 1);
    }

    long best = 0;
    for (size_t i = 0; i < t->cap; i++) {
        long score = (long)t->slots[i].count * t->slots[i].value;
        if (score > best)
            best = score;
    }
    printf("%ld\n", best);

    free(t);
}

3

u/IROIVIVIAIV Nov 12 '19

I wanted to write this in C and I see your example and I'm like well fuck

3

u/skeeto -9 8 Nov 12 '19

My goal was to be as fast as possible (amortized O(n)) while also supporting arbitrarily large input. You could back off on these to arrive at a much simpler, shorter solution. For example, statically allocate a fixed-length (say 217) array of integers, read in the entire input, qsort() it (O(n log n)), then iterate over it finding the best score by looking at runs.

2

u/CoDBorn Nov 12 '19

Could you provide the result you've got on the 100 000 value list, for checking purposes?

3

u/tomekanco Nov 12 '19

It's pi, 3141...

2

u/CoDBorn Nov 12 '19

Java with bonus, got 31415926535 on the 100 000 value list in 85ms.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Hashtable;

public class UpperSection
{
    private static Hashtable<Long, Long> ocurrences = new Hashtable<>();

    public static void main(String[] args)
    {
        if(args.length == 0)
        {
            System.out.println("Usage: UpperSection <file> <number_of_values> || UpperSection <value_1> <value_2> ...");
            return;
        }

        try
        {
            long value0 = Integer.parseInt(args[0]);
            long[] values = new long[args.length];
            values[0] = value0;
            
            values = populateValues(values, args);
            System.out.println(calculateMaxScore(values, args));
        }
        catch(NumberFormatException e)
        {
            if(args.length == 2)
                System.out.println(calculateMaxScore(args[0], Long.parseLong(args[1])));
            else
                System.out.println("Usage: UpperSection <file> <number_of_values> || UpperSection <value_1> <value_2> ...");
        }
    }

    private static long[] populateValues(long[] values, String[] args)
    {
        for(int i = 1; i < args.length; i++)
            values[i] = Integer.parseInt(args[i]);

        return values;
    }

    private static long calculateMaxScore(String file, long valuesNo)
    {
        Long max = -1L, aux, sum, aux2, startTime, endTime;

        try
        {
            File numberList = new File(file);
            BufferedReader br = new BufferedReader(new FileReader(numberList));
            String number;

            startTime = System.currentTimeMillis();

            while((number = br.readLine()) != null)
            {
                aux2 = Long.parseLong(number);

                if((aux = ocurrences.get(aux2)) != null)
                {
                    sum = aux + aux2;
                    ocurrences.put(aux2, sum);

                    if(sum > max)
                        max = sum.longValue();
                }
                else {
                    ocurrences.put(aux2, aux2);

                    if(aux2 > max)
                        max = aux2.longValue();
                }
            }

            endTime = System.currentTimeMillis();

            System.out.println("Total Time: " + (endTime - startTime) + "ms");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        return max;
    }

    private static long calculateMaxScore(long[] values, String[] args)
    {
        Long max = -1L, aux, sum;

        for(int i = 0; i < values.length; i++)
        {
            if((aux = ocurrences.get(values[i])) != null)
            {
                sum = aux + values[i];
                ocurrences.put(values[i], sum);

                if(sum > max)
                    max = sum.longValue();
            }
            else {
                ocurrences.put(values[i], values[i]);

                if(values[i] > max)
                    max = values[i];
            }
                
        }

        return max;
    }
}

2

u/chunes 1 2 Nov 12 '19

Factor. Does the bonus in 7 milliseconds.

: yahtzee-upper ( seq -- n )
    histogram >alist [ product ] [ max ] map-reduce ;

2

u/ernesdp Nov 12 '19

Python.

from collections import Counter
def yahtzee_upper(dice):
    upper_score = 0
    elem_count  = Counter(dice)
    for key in elem_count.keys(): 
        if (elem_count[key] * key) > upper_score: 
            upper_score = elem_count[key] * key
return upper_score

This my solution although I guess this is not very fast.

2

u/[deleted] Nov 13 '19

C#

      class Program
  {
      static void Main(string[] args)
      {
        Random rnd = new Random();
        List<int> rolls = new List<int>();
        int newMax = 0;
        for (int i = 0; i < 5; i++)
        {
            int dice = rnd.Next(1, 7);
            rolls.Add(dice);
        }
        for (int i = 1; i < 7; i++)
        {
            int oldMax = 0;
            foreach (var roll in rolls)
            {
                if (roll == i)
                    oldMax += i;
            }
            if (oldMax > newMax)
                newMax = oldMax;
        }
        Console.WriteLine("Rolls:");
        foreach (var item in rolls)
        {
            Console.WriteLine(item);
        }
        Console.WriteLine("Maximum possible score:");
        Console.WriteLine(newMax);

    }
}

2

u/[deleted] Nov 13 '19

My code works fine for the first test but when trying the bonus i get the following value:

2144475295

Calculated from val = 944772471 repeated 25x in the list.

Anyone have a similar issue that knows what the issue is?

I took C in College (like 20 years ago) so I'm guessing I messed up some kind of memory allocation or something stupid..

code: https://pastebin.com/DYBFGaFW

I don't expect anytone to debug the code but if you see an obvious error/what im missing, it would be appreciated it!

3

u/gabyjunior 1 2 Nov 13 '19

You may have an issue with integer overflow, using a 32 bits variable type meanwhile the max score needs a 64 bits variable for the bonus. Using long long or uint64_t type should fix it.

2

u/[deleted] Nov 13 '19 edited Nov 13 '19

My sizeof(long) is 8bytes though? I thought the same initially ? At gym but I’ll switch when I get home and test it out!

Thank you

Yep! you were right.. I kinda knew this I remember specifically doing a sizeof(int) and sizeof(long) to check the sizes ... I think my issue though was I had the max variable defined as 32bit int which i didnt realize..

works now though! ty

2

u/[deleted] Nov 16 '19 edited Nov 18 '19

Python3 feedback appreciated (as always)

if __name__ == "__main__":
    with open("yahtzee-upper-1.txt", 'r') as f:
        fList = [int(n) for n in f.read().split('\n')]

    fDict = {n: 0 for n in fList}
    for n in fList: fDict[n] += n

    print(max(fDict.values()))

alternatively, here's a version with no intermediary stored list

if __name__ == "__main__":
    with open("yahtzee-upper-1.txt", 'r') as f
        fDict = {}
        for n in f.read().split('\n'):
            n = int(n)
#           if n in fDict:  fDict[n] += n
#           else:           fDict[n] = n
            fDict[n] = fDict.get(n, 0) + n

    print(max(fDict.values()))

2

u/errorseven Nov 16 '19 edited Nov 17 '19

AutoHotkey - O(n) untested on mobile

EDIT: Tested - Turns out Max() function does not support unpacking Associative Arrays / Objects, only Linear Arrays - I solved this by including a comparison for a Max Value at every iteration.

yahtzee_upper(array) {

   hash := {}, max := array[1]
   for e, v in array {
   
       if (hash[v])
            hash[v] += v, max := hash[v] > max ? hash[v] : max
       else
          hash[v] := v, max := v > max ? v : max
   }
   
   return max
}

2

u/kopo222 Nov 20 '19

Python 3

I'm not sure how to determine if it is O(n)

However it completes the large list supplied in roughly 0.018 seconds, excluding reading in the data. With this included it takes roghly 0.06 seconds

import time
def yahtzee_upper(numbers):
	count = {}
	for x in numbers:
		if not x in count:
			count[x] = x
		else:
			count[x] += x
			
	return maxList(count)
	
def maxList(count):

	maxKey = 0
	maxVal = 0
	for key, value in count.items():
		if value > maxVal:
			maxKey = key
			maxVal = value
	
	return (maxKey, maxVal)


nums = []
with open('yahtzee-upper-1.txt', 'r') as r:
	for num in r:
		nums.append(int(num))

start = time.time()	
print(yahtzee_upper(nums))
print(time.time() - start)

1

u/henshao Dec 22 '19 edited Dec 22 '19

This is O(N)

→ More replies (3)

2

u/LudBee Nov 21 '19

Why is the challenge input given without an expected output?

0

u/[deleted] Nov 22 '19

[deleted] — view deleted comment

3

u/LudBee Nov 22 '19

I understood the problem, I was wondering what is the point of providing an input, the one called challenge input ( this challenge input, consisting of 100,000 values between 1 and 999,999,999 ) without the respective expected output. I could run my code on this input but I wouldn't know if it gives the correct answer, so what is that input good for?

2

u/draegtun Nov 25 '19

Rebol

yahtzee-upper: function [rolls] [
    count: make map! 0
    foreach n rolls [
        any [
            attempt [count/:n: count/:n + n]
            append count reduce [n n]
        ]
    ]
    first maximum-of values-of count
] 

Example usage in Rebol 3 console:

>> yahtzee-upper [2 3 5 5 6]
== 10

>> yahtzee-upper bonus-data        
== 31415926535

>> delta-time [yahtzee-upper bonus-data]
== 0:00:00.074794

2

u/tcbenkhard Nov 26 '19

Java

public class Yahtzee {
    public static int upper(int[] dice) {
        Map<Integer, Integer> values = new HashMap<>();
        Arrays.stream(dice).forEach(d -> values.put(d, values.getOrDefault(d, 0) + d));
        return values.values().stream().max(Integer::compare).get();
    }
}

2

u/ephemient Nov 27 '19 edited Apr 24 '24

This space intentionally left blank.

2

u/Specter_Terrasbane Nov 27 '19 edited Nov 27 '19

Python

Tried multiple methods and timed them, just for shiggles ... A custom MaxDict class, that updates the current maximum value on any set value call, an itertools.groupby solution, and the pretty much expected solution, collections.Counter.

Spoiler alert: collections.Counter was the clear winner, by far, at an average time of 0.006s (not including input parsing).

Output

Testing 3 methods, 100 runs each:
(Input file parsing time: 0.384s)

Method:		MaxDict
Result:		31415926535
Min time:	0.047s
Avg time:	0.048s
Max time:	0.050s

Method:		groupby
Result:		31415926535
Min time:	0.015s
Avg time:	0.015s
Max time:	0.017s

Method:		Counter
Result:		31415926535
Min time:	0.006s
Avg time:	0.006s
Max time:	0.007s

Code

from collections import defaultdict, Counter
from statistics import mean
from time import perf_counter
from itertools import groupby


class MaxDict(defaultdict):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.max_value = None
        
    def __setitem__(self, key, value):
        super().__setitem__(key, value)
        if self.max_value is None or value > self.max_value:
            self.max_value = value

def yahtzee_upper_maxdict(values):
    md = MaxDict(int)
    for value in values:
        md[value] += value
    return md.max_value

def yahtzee_upper_sorted_groupby(values):
    return max(sum(g) for k, g in groupby(sorted(values)))

def yahtzee_upper_counter(values):
    return max(k * v for k, v in Counter(values).items())

def test(func, values, runs):
    times = []
    results = set()
    runs = 100
    for __ in range(runs):
        tf2 = perf_counter()
        result = func(values)
        tf3 = perf_counter()
        results.add(result)
        times.append(tf3 - tf2)

    assert(len(results) == 1)
    return results.pop(), min(times), mean(times), max(times)

if __name__ == '__main__':
    tf0 = perf_counter()
    with open('yahtzee-upper-1.txt', 'r') as inputfile:
        values = [int(line) for line in inputfile]
    tf1 = perf_counter()

    funcs = {
        'MaxDict': yahtzee_upper_maxdict,
        'groupby': yahtzee_upper_sorted_groupby,
        'Counter': yahtzee_upper_counter,
    }

    runs = 100
    print(f'''\
Testing {len(funcs)} methods, {runs} runs each:
(Input file parsing time: {tf1 - tf0:.03f}s)
''')
    for name, func in funcs.items():
        result, mn, avg, mx = test(func, values, runs)
        print(f'''\
Method:\t\t{name}
Result:\t\t{result}
Min time:\t{mn:.03f}s
Avg time:\t{avg:.03f}s
Max time:\t{mx:.03f}s
''')

2

u/Gian12315 Dec 20 '19

Here's my Rust implementation:

fn yahtzee_upper(rolls: Vec<usize>) -> usize {
    let mut map = HashMap::new();
    for roll in rolls {
        let entry = map.entry(roll).or_insert(0);
        *entry += 1;
    }
    map.iter().map(|(key, value)| key * value).max().unwrap()
}

It runs challenge input under 20ms

2

u/LowerConcentrate Dec 23 '19 edited Dec 23 '19

Python 3. One-liner.

def yahtzee_upper(arr: list) -> int:
    return max(arr.count(x)*x for x in arr)

C. (Without bonus, but could be with mapping)

int yahtzee_upper(int *arr, int arrLen) {
    int i, r, max = 0;
    int count[7] = { 0 };
    for (i = 0; i < arrLen; i++) {
        count[arr[i]]++;
    }
    for (i = 0; i < arrLen; i++) {
        r = arr[i] * count[arr[i]];
        if (r > max) {
            max = r;
        }
    }
    return max;
}

2

u/stevenbee95 Dec 24 '19

Javascript:

var yahtzee_upper = function (list) {
    let counted = {};
    let max = 0;
    list.forEach(number => {
        if (!counted[number]) {
            counted[number] = number;
        } else{
            counted[number] += number;
        }
        if(counted[number] > max){
            max = counted[number];
        }
    });
    return max;
};

2

u/mike_the_kangeroo Dec 28 '19

def yahtzee_upper(inp):
sides = set(inp)
highest = -1
for element in sides:
val = element * inp.count(element)
if val > highest:
highest = val
return highest

2

u/machinematrix Jan 02 '20

C++ with bonus.

#include <map>
#include <string>
#include <cstdint>
#include <iostream>

int main()
{
	std::uint64_t result = 0;
	std::string strDiceFace;
	std::map<decltype(result), decltype(result)> acumulator;
	
	while(std::cin >> strDiceFace)
	{
		decltype(result) diceFace = std::stoul(strDiceFace);
		auto acumulated = acumulator[diceFace] += diceFace;

		if(acumulated > result)
			result = acumulated;
	}

	std::cout << result << std::endl;

	return 0;
}
→ More replies (5)

2

u/thecleancoder Jan 14 '20 edited Jan 14 '20

Python 3

def yahtzee_upper(array):
    return max( [array.count(i)*i for i in range(1,7)] )
→ More replies (2)

2

u/TheRealGregorM Jan 14 '20

Python 3

def yahtzee_new(lst):
    diction = {
        }
    for i in lst:
        if i not in diction:
            diction[i] = 1
        elif i in diction:
            diction[i] += 1

    return max([diction[i] * i for i in diction])

1

u/MikeFratelli Jan 24 '20

Beautiful return statement!

2

u/neur0sys Jan 16 '20 edited Jan 16 '20

Z80 Assembly

Supports only 6-sided dice.

Run through BASIC: https://i.imgur.com/LeClRN5.gif

        org     &8000
        jp      start

buf:    ds      7               ; Work area to sum rolls
        db      -1
input:  db      2, 3, 5, 5, 6   ; Input yahtzee rolls
        db      -1

start:  call    solve
        ret

; returns largest in 'b
solve:  ld      hl, input
        exx
        ld      b, 0            ; max_sum
        exx
loop:   ld      de, buf
        ld      a, (hl)         ; a = *input
        cp      -1              ; if end, exit
        ret     z
        ex      de, hl
        ld      c, a
        ld      b, 0
        add     hl, bc
        ld      a, (hl)         ; a = *(buf + c)
        add     a, c
        exx
        cp      b               ; a > max_sum
        jr      c, solve2
        ld      b, a            ; max_sum = a
solve2: exx
        ld      (hl), a
        ex      de, hl
        inc     hl
        jr      loop

Together with the BASIC runner: https://gist.github.com/neuro-sys/375e6dc51a3099c99b9ef73fd73f8d96

2

u/Keone_ShyGuy Jan 17 '20 edited Jan 17 '20

New to javascript and codepen, so the idea of getting input for the 2nd challenge is a bit daunting at the moment, but managed to get the first challenge working.

Interactive version

2

u/DaVileKial6400 Jan 17 '20

Hey so checking you code and you run in to a problem when you have more multiples of low numbers compared to high numbers.

For instance an these arrays don't give the upper Yahtzee when ran through.

[1,2,1,2,1]  

supposed to output '2 * 2 = 4' instead outputs '1 * 3 = 3'

[2,5,2,5,2] 

intended '5 * 2 = 10' outputs '2 * 3 =6'

Since your code just looks for the number with the largest multiple you don't get the largest score in all situations.

→ More replies (1)

2

u/antonio-one Jan 21 '20 edited Jan 21 '20

Python 3

import random

class Dice():

"""Run the below to find the highest score of 20 dice with 100,000 sides each (I think)
import time

start_time = time.time()
dice = Dice(sides=100000, number=20)
dice.roll()
print(f'result = {dice.result}')
print(f'scores = {dice.scores}')
print(f'upper yahtzee = {dice.upper_score}')
print(f'duration = {time.time() - start_time}')
"""
def __init__(self, sides, number):
    self.sides = sides
    self.number = number
    self.result = []
    self.scores = {key: 0 for key in range(1, self.sides + 1)}
    self.upper_score = 0

def roll(self):
    self.result.clear()
    for i in range(0, self.number):
        self.result.append(random.randint(1, self.sides))
    self.result.sort()

    for i in self.result:
        self.scores[i] += i

    for i in self.scores.values():
        if self.upper_score < i:
            self.upper_score = i

2

u/Aware-Computer Feb 22 '20

Haskell:

yahtzee_upper y = maximum (map (\x -> (length x) * (head x)) (group y))

2

u/vorok21 Mar 10 '20

java

public class dice {

	public static void main(String[] args) {	
		int[] i = {1,1,1,1,7};
		yahtzee_upper(i);
	}
	
	public static void yahtzee_upper(int input[]) {
		int[] score = {0,0,0,0,0,0,0};
		for(int i=1;i<7;i++) {
			for(int j=0;j<5;j++) {
				if(input[j]==i) score[i]+=i;
			}
		}
		System.out.println(maxValue(score));
	}
	public static int maxValue(int array[]) {
		int max=0, i=0;	
		while(i<7) {
			if(max<array[i]) max=array[i];
			i++;
		}
		
		return max;
	}

}

2

u/Mahjongasaur Mar 11 '20

HTML/JS

<!DOCTYPE html>
<html>
    <head>
        <title>#381 Yahtzee Upper Section Scoring</title>

        <!-- Challenge
            The game of Yahtzee is played by rolling five 6-sided dice, and scoring the results in a number of ways. 
            You are given a Yahtzee dice roll, represented as a sorted list of 5 integers, each of which is between 1 and 6 inclusive. 
            Your task is to find the maximum possible score for this roll in the upper section of the Yahtzee score card. 
            Here's what that means.
            For the purpose of this challenge, the upper section of Yahtzee gives you six possible ways to score a roll. 
            1 times the number of 1's in the roll, 2 times the number of 2's, 3 times the number of 3's, and so on up to 
            6 times the number of 6's. For instance, consider the roll [2, 3, 5, 5, 6]. If you scored this as 1's, 
            the score would be 0, since there are no 1's in the roll. If you scored it as 2's, the score would be 2, 
            since there's one 2 in the roll. Scoring the roll in each of the six ways gives you the six possible scores:
            0 2 3 0 10 6
            The maximum here is 10 (2x5), so your result should be 10.
        -->
       
        <!-- Bonus
            Efficiently handle inputs that are unsorted and much larger, both in the number of dice and in the number of sides per die. 
            (For the purpose of this bonus challenge, you want the maximum value of some number k, 
            times the number of times k appears in the input.)
        -->
    </head>

    <body>
        <h1>Yahtzee Upper Section Scoring</h1>
        
        <!-- User enters dice rolls, separated by a comma -->
        <div>
            Enter dice rolls, separated by a comma <input type="text" id="inputDiceRolls">
        </div>

        <!-- Results displayed here -->
        <div id="resultDiv">
        </div>

        <!-- Run function to find highest possible score -->
        <button onclick="ScoreDice()">Score!</button>

        <script>
            var diceRolls = "";
            var diceRollsArray = [];

            var currentCount = 0;
            var currentCountTimesAppeared = 0;

            var highestValue = 0;
            var highestValueNumber = 0;
            var highestValueTimesAppeared = 0;

            function ScoreDice() {

                // Get input dice rolls, convert to array of integers, and sort numerically
                diceRolls = document.getElementById("inputDiceRolls").value;
                SortDiceRolls(diceRolls);

                // run through dice rolls and determine highest possible score
                for (var i = 0; i < diceRollsArray.length; i++) {
                    
                    // if the number appeared before, add the scores together and increase the count of number of times that number has appeared
                    if(diceRollsArray[i] == diceRollsArray[i-1]) {
                        currentCount += diceRollsArray[i];
                        currentCountTimesAppeared++;
                    } 
                    
                    // if that number hasn't appeared before, set the counter of both current scores and number of times appeared to 
                    // current number and 1, respectively
                    else {
                        currentCount = diceRollsArray[i];
                        currentCountTimesAppeared = 1;
                    }

                    // if the current values from above are higher, update the highest possible score variable, which number, and how frequent
                    if (currentCount > highestValue) {
                        highestValue = currentCount;
                        highestValueNumber = diceRollsArray[i];
                        highestValueTimesAppeared = currentCountTimesAppeared;
                    }
                }
                
                // update output div with results
                document.getElementById("resultDiv").innerHTML = "Highest Score Possible: " + highestValue + "<br /> Number used: " + 
                highestValueNumber + "<br /> Number of times appeared: " + highestValueTimesAppeared;
            }

            // pulls in diceRolls input, removes spaces, converts to array, sorts numerically, and converts to integers
            function SortDiceRolls(diceRolls) {
                diceRolls = diceRolls.split(" ").join("");
                diceRollsArray = diceRolls.split(',');
                diceRollsArray.sort(function(a, b){return a-b});
                diceRollsArray = diceRollsArray.map(Number);
            }

        </script>

    </body>
</html>

2

u/ArtemiyKra Jan 03 '23

The shortest Pyhton solution))

yahtzee_upper=lambda _:max([_.count(x)*x for x in _])

1

u/ASpueW Nov 12 '19 edited Nov 12 '19

Rust with bonus

trait Yahtzee<R>{
    fn yahtzee(self) -> R;    
}

impl<T> Yahtzee<u64> for T
where T:Iterator<Item=u64>
{
    fn yahtzee(self) -> u64 {
        self.fold(HashMap::new(), |mut map, item|{ *map.entry(item).or_insert(0u64) += item; map })
            .values()
            .cloned()
            .max()
            .unwrap_or(0)
    }
}

fn main() {
    for test in TESTS {
        println!("{:?} => {}", test, test.iter().cloned().yahtzee());
    }
}

Output:

[2, 3, 5, 5, 6] => 10
[1, 1, 1, 1, 3] => 4 
[1, 1, 1, 3, 3] => 6 
[1, 2, 3, 4, 5] => 5 
[6, 6, 6, 6, 6] => 30 
[1654, 1654, 50995, 30864, 1654, 50995, 22747, 1654, 1654, 1654, 1654, 1654, 30864, 4868, 1654, 4868, 1654, 30864, 4868, 30864] => 123456 
text data score=31415926535 duration=0.009837387 seconds

Playground

1

u/gabyjunior 1 2 Nov 12 '19 edited Nov 12 '19

Ruby with bonus (takes 220ms including input parsing).

dice = {}
while line = STDIN.gets do
        n = line.chomp.to_i
        if dice[n].nil?
                dice[n] = n
        else
                dice[n] += n
        end
end
puts "#{dice.values.max}"

1

u/morgon-of-hed Nov 12 '19

JavaScript

const yahtzee_upper = roll => {
  return Math.max(...roll.map(el => roll.filter(num => num === el).reduce((a, b) => a + b, 0)))
}

1

u/ro-ok Nov 17 '19

Man, this is so much better than my solution. Thanks for sharing!

1

u/neo2049 Nov 12 '19 edited Nov 13 '19

Python

from collections import Counter def yahtzee_upper(list): newList = {} total = [] newList = Counter(list) for keys, values in newList.items(): total.append(keys * values) print(max(total))

→ More replies (2)

1

u/octolanceae Nov 13 '19 edited Nov 13 '19

C++17 w/optional bonus

Note: Editted for code formatting purposes

#include <iostream>
#include <fstream>
#include <map>

int main(int, char** argv) {
  std::ifstream input(argv[1]);

  if (input) {
        uint64_t die{0};
        std::map<uint64_t, uint64_t> results;
        while (input >> die) {
      results[die] += die;
    }

    uint64_t max{0};
    for (auto score: results) {
      if (score.second > max)
        max = score.second;
    }
    std::cout << "Max upper_score: " << max << '\n';
  }
}

For 100k input - The cake is a lie, it is all about the pi.

0.04 seconds, including file i/o.

Max upper score: 31415926535
0.04user 0.00system 0:00.04elapsed 100%CPU (0avgtext+0avgdata 1336maxresident)k
0inputs+0outputs (0major+397minor)pagefaults 0swaps

1

u/SuperVGA Nov 13 '19

ES6 meant to allow association between faces and values for each side of a die, as well as any special rules to scoring when a particular face wasn't showing. It's by no means economical, but it looks pretty good, I think...

let die_sides = {'1':1, '2':2, '3':3, '4':4, '5':5, '6':6};
let value_before_first_increment = 0;
let value_when_absent = 0;

let yahtzee_value_per_side_present = (arr_throw) => {
 let values = {};
 for(die_value of arr_throw) {
  const value = die_sides[die_value];
  values[die_value] = values[die_value] ?
    values[die_value] + value :
    value_before_first_increment + value;
 }
 return values;
};

let yahtzee_upper = (arr_throw) => {
  const throw_values = yahtzee_value_per_side_present(arr_throw);
  return Math.max(...Object.values(throw_values));
}

You'd invoke it with values matching the property values;

let a = yahtzee_upper(['2', '3', '5', '5', '6']);

1

u/Edwizzy102 Nov 13 '19

Code in Java 8:

Did the simple version because the challenge would most likely have the same result with O(N) time complexity:

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

public class Yahtzee {
	public static void main(String[] args) {
		ThreadLocalRandom rng = ThreadLocalRandom.current();
		
		long [] roll = new long[5];
		
		for (int i = 0; i < roll.length; i++) {
			roll[i] = rng.nextInt(6) + 1;
		}
		System.out.println(Arrays.toString(roll));
		Map<Long, Integer> freqMap = new HashMap<>();
		int highest = 0;
		long highestKey = 0;
		
		for (int i = 0; i < roll.length; i++) {
			if (highestKey < roll[i]) {
				highestKey = roll[i];
			}
			if (!freqMap.containsKey(roll[i])) {
				freqMap.put(roll[i], 1);
			} else {
				int val = freqMap.get(roll[i]);
				freqMap.replace(roll[i], ++val);
				if (freqMap.get(roll[highest]) < freqMap.get(roll[i]))
					highest = i;
			}
		}

		if (highestKey > roll[highest] * freqMap.get(roll[highest]))
			System.out.printf("YAHTZEE %d%n", highestKey);
		else
			System.out.printf("YAHTZEE %d%n", roll[highest] * freqMap.get(roll[highest]));

	}
}

1

u/RevenantMachine 1 0 Nov 13 '19

Kotlin, with bonus:

fun main() {
    File("yahtzee-upper-1.txt").useLines { lines ->
        lines.map(String::toLong).yahtzeeMax().let(::println)
    }
}

fun Sequence<Long>.yahtzeeMax(): Long? = this
    .groupingBy { it }
    .fold(0L, Long::plus)
    .maxBy { it.value }
    ?.value

This runs in about 80 ms cold, and 15 ms after a dozen iterations, when the JVM is warmed up a bit.

Time complexity is linear wrt. the length of the sequence, and space complexity is linear wrt. the distinct values present in the sequence.

1

u/bruce3434 Nov 14 '19 edited Nov 14 '19

##D

The test cases with optional bonus and with the data from the URI. libcurl must be installed in your computer.

immutable string URI = `https://gist.githubusercontent.com/cosmologicon/beadf49c9fe50a5c2a07ab8d68093bd0/raw/fb5af1a744faf79d64e2a3bb10973e642dc6f7b0/yahtzee-upper-1.txt`;
    
    struct Ytz(T)
    {
    	T[T] valuePairs;
    
    	this(in T[] keys)
    	{
    		foreach (key; keys)
    			add(key);
    	}
    
    	void add(in T key)
    	{
    		if (key in valuePairs)
    			valuePairs[key] += key;
    		else
    			valuePairs[key] = key;
    	}
    
    	@property T largest() const
    	{
    		T largest = valuePairs.byValue().front();
    		foreach (key; valuePairs.byKey())
    			if (valuePairs[key] > largest)
    				largest = valuePairs[key];
    		return largest;
    	}
    }
    
    unittest
    {
    	assert(Ytz!uint([2, 3, 5, 5, 6]).largest == 10);
    	assert(Ytz!uint([1, 1, 1, 1, 3]).largest == 4);
    	assert(Ytz!uint([1, 1, 1, 3, 3]).largest == 6);
    	assert(Ytz!uint([1, 2, 3, 4, 5]).largest == 5);
    	assert(Ytz!uint([6, 6, 6, 6, 6]).largest == 30);
    	assert(Ytz!uint([
    				1654, 1654, 50995, 30864, 1654, 50995, 22747, 1654, 1654, 1654,
    				1654, 1654, 30864, 4868, 1654, 4868, 1654, 30864, 4868, 30864
    			]).largest == 123456);
    }
    
    void main()
    {
    	auto ytz = Ytz!ulong();
    
    	import std.net.curl : byLineAsync;
    	import std.conv : to;
    
    	foreach (nstr; byLineAsync(URI))
    		ytz.add(nstr.to!ulong);
    
    	import std.datetime.stopwatch : StopWatch, AutoStart;
    	auto sw = StopWatch(AutoStart.no);
    
    	sw.start();
    	const auto largest = ytz.largest;
    	sw.stop();
    	import std.stdio : writefln;
    
    	writefln!"%s in %s microseconds"(largest, sw.peek.total!"usecs");
    }

$ dub build -b release --compiler ldc && ./tstd

Performing "release" build using ldc for x86_64.

tstd ~master: building configuration "application"...

Linking...

31415926535 in 56 microseconds

1

u/[deleted] Nov 15 '19

You are counting the time incorrectly. `ytz.add(nstr.to!ulong);` is the heaviest part of the challenge and you are skipping it

→ More replies (1)

1

u/__dict__ Nov 15 '19

Racket Scheme.

Code:

#lang racket/base

(define (tally ls)
  (let ([h (make-hash)])
    (for ([el ls])
      (hash-set! h el (+ 1 (hash-ref h el 0))))
    h))

(define (yahtzee-upper ls)
  (for/fold ([max-score 0])
            ([(roll cnt) (tally ls)])
    (max max-score (* roll cnt))))

;; Calling map-sequence over in-lines is slower.
(define (file->list in)
  (let loop ([lines '()])
    (let ([line (read-line in)])
      (if (eof-object? line)
          lines
          (loop (cons (string->number line) lines))))))

(module* main #f
  (call-with-input-file "data.txt"
    (lambda (in) (yahtzee-upper (file->list in)))))

Output:

Time to do the large file:

[lain@riki yahtzee]$ time ./yahtzee
31415926535

real    0m0.175s
user    0m0.161s
sys     0m0.014s

Time to do a file with a single line:

[lain@riki yahtzee]$ time ./yahtzee
3

real    0m0.119s
user    0m0.111s
sys     0m0.007s

So yea, it's roughly 0.119s startup time and 0.06s time actually processing the list.

1

u/jslinde123 Nov 15 '19

Possible solution in Python 3. Feedback appreciated.

from random import randint




def roll_dice(number_of_rolls):
    """Receive the number of rolls and return a list with the results of each roll.
    """




    # Create list for rolls
    roll = []




    # Roll the dice and populate list
    for i in range(number_of_rolls):
        die_roll = randint(1, 6)
        roll.append(die_roll)





    return sorted(roll)




def create_result(lst):
    """Receive list with rolls and return a dictionary of the number of times each
        roll appeared.
    """





    # Creates dictionary to hold results
    result = {}




    # Populates dictionary with keys only
    for i in range(len(lst) + 1):
        if i == 0:
            continue
        else:
            result[i] = 0




    # Populate dictionary with values from list of rolls
    for num in lst:
        result[num] = result.get(num, 0) + 1




    return result




def calculate_scores(dict):
    """Receives dictionary with the number of times each roll appeard and
        returns a list with the total possible score for each number.
    """




    # Create list to hold scores
    scores = []




    # Iterate through dictionary appending the appropiate total scores                    
    for i, j in sorted(dict.items()):
        scores.append(i*j)




    return scores


# Roll the dice
roll = roll_dice(6)




# Create dictionary with results
result = create_result(roll)




# Calculate possible scores
scores = calculate_scores(result)




# Print the maximum score
print(max(scores))

1

u/[deleted] Nov 16 '19 edited Nov 17 '19

Here's some feedback in no particular order:


Your whitespace (while all personal preference) makes it hard to read, try condensing your code a bit aesthetically.


In create_list

    # Populates dictionary with keys only
    for i in range(len(lst) + 1):
        if i == 0:
            continue
        else:
            result[i] = 0

In order to ignore a useless first loop, try starting the range at one instead of just discarding the first loop

    for i in range(1, len(lst) + 1):
        result[i] = 0
→ More replies (1)

1

u/ogniloud Nov 15 '19

Raku

use v6.c;
use Test;

sub yahtzee-upper(@rolls --> Int:D) {
	my &p = { $^pair.key * $^pair.value };
	@rolls
	.Bag     # weight each number
	.map(&p) # get number times its weight
	.max     # get maximum product
}

my @large-arr = [
1654, 1654, 50995, 30864, 1654, 50995, 22747, 1654, 1654, 1654,
1654, 1654, 30864, 4868, 1654, 4868, 1654, 30864, 4868, 30864
];

is 10, yahtzee-upper [2, 3, 5, 5, 6];
is 4,  yahtzee-upper [1, 1, 1, 1, 3];
is 6,  yahtzee-upper [1, 1, 1, 3, 3];
is 5,  yahtzee-upper [1, 2, 3, 4, 5];
is 30, yahtzee-upper [6, 6, 6, 6, 6];
is 123456, yahtzee-upper @large-arr;

1

u/austinll Nov 18 '19

Java

Yahtzee function ~280 ms

public static void Yahtzee(LinkedList<Long> vals){

HashMap<Long,Long> score = new HashMap<Long,Long>();
Long max = new Long(0);
for(Long l : vals){
  score.put(l,Long.sum(l,score.getOrDefault(l,new Long(0))));
  if(score.get(l).compareTo(max) == 1){max = score.get(l);}
}
System.out.println(max.toString());

}

main ~330 ms

public static void main(String[] args){

long time1, time2, time3;
time1 = System.currentTimeMillis();

File file = new File(System.getProperty("user.dir") + "/DiceRolls.txt");
Scanner scan;
LinkedList<Long> vals = new LinkedList<Long>();
try{
  scan = new Scanner(file);
   while(scan.hasNextLong()){
      vals.add(scan.nextLong());
    }
}
catch (FileNotFoundException e1){
}
time2 = System.currentTimeMillis();
Yahtzee(vals);
time3 = System.currentTimeMillis();
System.out.println("whole program time: " + (time3 - time1));
System.out.println("Scoring time: " + (time2 - time1));

}

1

u/[deleted] Nov 21 '19
def yahtzee_upper(nums):
    sums = dict()
    for num in nums:
        if num in sums:
            sums[num] += num
        else:
            sums[num] = num
    return max(sums.values())

1

u/paopaopao69 Nov 22 '19 edited Nov 22 '19

C#

using System;
using System.Collections.Generic;
using System.Linq;

namespace DailyProgrammer
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = new int[5]{ 6, 6, 6, 6, 6 };
            int result = yahtzee_upper(numbers);
            Console.WriteLine(result);
            Console.ReadKey();
        }

        public static int yahtzee_upper(int[] numbers)
        {
            List<int> sums = new List<int>();
            int sumOfOnes = numbers.Sum(x => x == 1 ? 1 : 0 );
            int sumOfTwos = numbers.Sum(x => x == 2 ? 2 : 0);
            int sumOfThrees = numbers.Sum(x => x == 3 ? 3 : 0);
            int sumOfFours = numbers.Sum(x => x == 4 ? 4 : 0);
            int sumOfFives = numbers.Sum(x => x == 5 ? 5 : 0);
            int sumOfSixes = numbers.Sum(x => x == 6 ? 6 : 0);
            sums.Add(sumOfOnes);
            sums.Add(sumOfTwos);
            sums.Add(sumOfThrees);
            sums.Add(sumOfFours);
            sums.Add(sumOfFives);
            sums.Add(sumOfSixes);
            return sums.Max();
        }
    }
}

1

u/Dasaru Nov 22 '19

Javascript

function yahtzee_upper (input) {

    var result = {};

    input.map(function(val, idx, arr){

        if(!result[arr[idx]]) result[arr[idx]] = 0;

        result[arr[idx]] += val;

    });

    return Math.max.apply(null, Object.values(result));
};

1

u/arthurelamothe Nov 23 '19 edited Nov 23 '19

C++/Qt

int yahtzeeUpper(const QList<int> yLst)
{
    int max = 0;
    QSet<int> uniques = QSet<int>::fromList(yLst);
    foreach (int i, uniques ) {
        if (yLst.count(i) * i > max)
             max = yLst.count(i) * i;
    }
    return max;
}
qDebug() << "yahtzeeUpper({ 2, 3, 5, 5, 6 })=>" << yahtzeeUpper({ 2, 3, 5, 5, 6 });
qDebug() << "yahtzeeUpper({ 1, 1, 1, 1, 3 })=>" << yahtzeeUpper({ 1, 1, 1, 1, 3 });
qDebug() << "yahtzeeUpper({ 1, 1, 1, 3, 3 })=>" << yahtzeeUpper({ 1, 1, 1, 3, 3 });
qDebug() << "yahtzeeUpper({ 1, 2, 3, 4, 5 })=>" << yahtzeeUpper({ 1, 2, 3, 4, 5 });
qDebug() << "yahtzeeUpper({ 6, 6, 6, 6, 6 })=>" << yahtzeeUpper({ 6, 6, 6, 6, 6 });
// Bonus
qDebug() << "yahtzeeUpper({ 1654, 1654, 50995, 30864, 1654, 50995, 22747,\\
1654, 1654, 1654, 1654, 1654, 30864, 4868, 1654, 4868, 1654,\\
30864, 4868, 30864 })=> " << yahtzeeUpper({ 1654, 1654, 50995, 30864, 1654, 50995, 22747,
1654, 1654, 1654, 1654, 1654, 30864, 4868, 1654, 4868, 1654, 30864, 4868, 30864 });

1

u/raevnos Nov 24 '19 edited Nov 24 '19

And Sqlite, with bonus:

CREATE TABLE rolls(trial, roll);
INSERT INTO rolls VALUES (1, 2), (1, 3), (1, 5), (1, 5), (1, 6);
INSERT INTO rolls VALUES (2, 1), (2, 1), (2, 1), (2, 1), (2, 3);
INSERT INTO rolls VALUES (3, 1), (3, 1), (3, 1), (3, 3), (3, 3);
INSERT INTO rolls VALUES (4, 1), (4, 2), (4, 3), (4, 4), (4, 5);
INSERT INTO rolls VALUES (5, 6), (5, 6), (5, 6), (5, 6), (5, 6);
INSERT INTO rolls VALUES (6, 1654), (6, 1654), (6, 50995), (6, 30864),
       (6, 1654), (6, 50995), (6, 22747), (6, 1654), (6, 1654), (6, 1654),
       (6, 1654), (6, 1654), (6, 30864), (6, 4868), (6, 1654), (6, 4868),
       (6, 1654), (6, 30864), (6, 4868), (6, 30864);
CREATE INDEX rolls_idx ON rolls(trial, roll);

CREATE TABLE expected(trial, score);
INSERT INTO expected VALUES (1, 10), (2, 4), (3, 6), (4, 5), (5, 30),
       (6, 123456);

WITH counts AS
  (SELECT trial, roll, count(*) AS cnt
   FROM rolls
   GROUP BY trial, roll)
SELECT trial
     , max(roll * cnt) AS score
     , (SELECT score FROM expected AS e WHERE e.trial = c.trial) AS expected
FROM counts AS c
GROUP BY trial
ORDER BY trial;

After loading challenge input into into the database, all seven cases are solved in less than 0.025 seconds on my old Sandybridge. Indexes are great.

1

u/TheMightyShovelface Nov 24 '19

Go

package main

import "fmt"

func yahtzee_upper(input []int, sides int) int {
    scores := make([]int, sides)
    max := 0
    val := 0
    for _, curr := range input {
	    scores[curr-1]++
	    val = scores[curr-1] * curr
	    if val > max {
		    max = val
	    }
    }
    return max
}

func main() {
    input := [][]int{
	    {2, 3, 5, 5, 6},
	    {1, 1, 1, 1, 3},
	    {1, 1, 1, 3, 3},
	    {1, 2, 3, 4, 5},
	    {6, 6, 6, 6, 6}}
    fmt.Println(yahtzee_upper(input[0], 6) == 10)
    fmt.Println(yahtzee_upper(input[1], 6) == 4)
    fmt.Println(yahtzee_upper(input[2], 6) == 6)
    fmt.Println(yahtzee_upper(input[3], 6) == 5)
    fmt.Println(yahtzee_upper(input[4], 6) == 30)
}

1

u/raevnos Nov 24 '19

One last one, in tcl. Reads the challenge input on standard input, one number per line like it was provided.

#!/usr/bin/tclsh
array set dice {}
while { [gets stdin line] >= 0 } { incr dice($line) }
set scores [lmap die [array names dice] { expr { $die * $dice($die) } }]
puts [lindex [lsort -integer -decreasing $scores] 0]

1

u/thorwing Nov 25 '19

Kotlin

fun main(){
    File("yahtzee-upper-1.txt").readLines().map(Integer::parseInt).let(::yahtzee_upper).let(::println)
}

fun yahtzee_upper(numbers: List<Int>): Int?{
    return numbers.groupingBy { it }.eachCount().map{(key, value) -> key * value}.max()
}

1

u/Little_Danson_Man Nov 25 '19 edited Nov 25 '19

Here's a python solution

def yahtzee_upper_fast( dice_rolls ):
    '''
    Keys of dice roll results will be stored in the dictionary with the
    summed value of our roll results as the value.
    I think this allows for O(n) because we are only ever iterating over n
    items on three separate occations
    '''
    roll_dict = dict.fromkeys(dice_rolls, 0)

    for roll in dice_rolls:
        roll_dict[roll] += int(roll)

    return max(roll_dict.values())

I'm pretty sure this is an O(n) time complexity solution. I'm not sure about space complexity. dict.fromkeys() is a great helper function because it allowed me to initialize with relative ease

1

u/[deleted] Dec 03 '19

Python

from collections import Counter
from random import randint

rolls = []

for i in range(6):
    rolls.append(randint(1,6))

counts = dict(Counter(rolls))

print(counts,'=> ',end="")

max_count = -1
max_key = 0
for (key,value) in counts.items():
        if value*key > max_count*max_key:
            max_count = value
            max_key = key

print(max_count*max_key)

1

u/awsome855 Dec 08 '19 edited Dec 08 '19
Javascript

function getMaxYahtzee(list){
    var max = 0;
    list.forEach(die =>{ 
        let inst = count(list,die); 
        if(max<instdie)
        { max = instdie; 
        } 
    })
    return(max); 
} 
function count(list, val)
{ 
    let count = 0; 
    list.forEach(die => { 
    if(die==val){ 
        count+=1; } 
    }); 
    return(count); 
}

1

u/darknes1234 Dec 08 '19

Go

package main

import (
	"fmt"
)

func max(arr [6]int) int {
	max := 0
	for i:=0; i<6; i++ {
		if arr[i] > max {
			max = arr[i]
		}
	}
	return max
}

func yahtzee(in [5]int) int {
	var score [6]int

	for i := 1; i <= 6; i++ {
		sum := 0
		for j:=0; j<5; j++ {
			if in[j] == i {
				sum += i
			}
		}
		score[i-1] = sum
	}

	return max(score)
}

func main() {
	ins := [5][5]int{
		{2, 3, 5, 5, 6},
		{1, 1, 1, 1, 3},
		{1, 1, 1, 3, 3},
		{1, 2, 3, 4, 5},
		{6, 6, 6, 6, 6},
	}

	for i:=0; i<5; i++ {
		fmt.Println(yahtzee(ins[i]))
	}
}

1

u/[deleted] Dec 09 '19

Erlang

-module(yahtzee_upper).
-export([yahtzee_upper/1, test/0, test_large_input/0]).

read_input(InputFile) ->
    {'ok', IODevice} = file:open(InputFile, ['read']),
    read_input(IODevice, file:read_line(IODevice), []).

read_input(IODevice, 'eof', Lines) ->
    file:close(IODevice),
    Lines;
read_input(IODevice, {'ok', Line}, Lines) ->
    read_input(IODevice, file:read_line(IODevice), [ list_to_integer(Line -- "\n") | Lines]).

yahtzee_upper(L) ->
    yahtzee_upper(L, #{}).

yahtzee_upper([H | T], Dice) ->
    case maps:get(H, Dice, 'unknown') of
        'unknown' -> yahtzee_upper(T, Dice#{ H => 1});
        N         -> yahtzee_upper(T, Dice#{ H => N + 1})
    end;
yahtzee_upper([], Dice) ->
    highest_score(Dice, maps:keys(Dice)).

highest_score(Dice, [H | T]) ->
    HighScore = H * maps:get(H, Dice),
    highest_score(Dice, T, HighScore, HighScore).

highest_score(Dice, [H | T], CurrentScore, HighScore) when CurrentScore > HighScore ->
    highest_score(Dice, T, maps:get(H, Dice) * H, CurrentScore);
highest_score(Dice, [H | T], _CurrentScore, HighScore) ->
    highest_score(Dice, T, maps:get(H, Dice) * H, HighScore);
highest_score(_Dice, [], CurrentScore, HighScore) when CurrentScore > HighScore ->
    CurrentScore;
highest_score(_Dice, [], _CurrentScore, HighScore) ->
    HighScore.

test() ->
    10 = yahtzee_upper([2, 3, 5, 5, 6]),
    4 = yahtzee_upper([1, 1, 1, 1, 3]),
    6 = yahtzee_upper([1, 1, 1, 3, 3]),
    5 = yahtzee_upper([1, 2, 3, 4, 5]),
    30 = yahtzee_upper([6, 6, 6, 6, 6]),
    123456 = yahtzee_upper([1654, 1654, 50995, 30864, 1654, 50995, 22747,
    1654, 1654, 1654, 1654, 1654, 30864, 4868, 1654, 4868, 1654,
    30864, 4868, 30864]),
    test_passed.

test_large_input() ->
    yahtzee_upper(read_input("input.txt")).

1

u/JackMagic1 Dec 09 '19

C# (with Linq)

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
	public static void Main()
	{
		int[] first = new int[]{2, 3, 5, 5, 6}; //10
		int[] second = new int[]{1, 1, 1, 3, 3}; //6
		int[] third = new int[]{1, 2, 3, 4, 5}; //5
		int[] fourth = new int[]{6, 6, 6, 6, 6}; //30
		
		var firstResult = first.GroupBy(m =>m).Select(m => new { Value = m.Sum(s => s)}).Max(m => m.Value);
		var secondResult = second.GroupBy(m =>m).Select(m => new { Value = m.Sum(s => s)}).Max(m => m.Value);
		var thirdResult = third.GroupBy(m =>m).Select(m => new { Value = m.Sum(s => s)}).Max(m => m.Value);
		var fourthResult = fourth.GroupBy(m =>m).Select(m => new { Value = m.Sum(s => s)}).Max(m => m.Value);
		
		Console.WriteLine($"First Maximum {firstResult}");
		Console.WriteLine($"Second Maximum {secondResult}");
		Console.WriteLine($"Third Maximum {thirdResult}");
		Console.WriteLine($"Fourth Maximum {fourthResult}");

	}  
}

1

u/AviatingFotographer Dec 11 '19
Python:

def yahtzee_scoring(roll):
    possible_scores = dict()

    for num in roll:
        possible_scores[num] = possible_scores.get(num, 0) + 1

    print(possible_scores)

    scores = []
    for num, freq in possible_scores.items():
        scores.append(num*freq)

    highscore = max(scores)

    return highscore

1

u/weijiajun Dec 11 '19

Python:

from collections import Counter

def yahtzee_upper(rolls):
    dice_counts = Counter(rolls)
    scores = [d * c for d,c in dice_counts.items()]
    return max(scores)

yahtzee_scoring([2, 3, 5, 5, 6])

1

u/9spaceking Dec 13 '19

you know, I can't help but think there's a one liner python solution to this using map plus lambda without importing counter, though it's hard to think of how to organize the syntax

1

u/aureliogrb Dec 12 '19

Java:

Loading the values from a text file called "values", one value per line.

Path path = FileSystems.getDefault().getPath("./src", "values.txt");

//put the values on a list of strings

List<String> lines = Files.readAllLines(path, Charset.defaultCharset());

//Convert them to a stream of Integers

Stream<Integer> values = lines.stream().mapToInt(Integer::valueOf).boxed();

// Map all the values, the key is the value chosen, the value is the sum of the dices that rolled the key

Map<Integer, Integer> counts = values.collect(Collectors.groupingBy(Function.identity(),Collectors.summingInt(Integer::valueOf)) );

//Return the highest value.

return counts.values().stream().mapToInt(Integer::valueOf).max().orElse(0);

Processes the test file with 100K values in 0.22 seconds on my laptop.

1

u/normantas Dec 17 '19

C# code with an explanation to it, feel free to use it


public static int yahtzee_upper(int[] Input)
        {
            List<int> input = Input.ToList<int>(); // Converts the array to List<T> elements, as it has some functions I will need, like dynamic changes to it.
            int output = 0; //Declares and initialized the output.
            while(input.Count > 0) //Goes through elements till there are no more
            {
                int temp = input[0]; // Goes to the first element in the list, as it will be removed later in the upcoming while loop
                int compare = 0;
                while (input.Contains(temp)) //If it finds the selected element, adds to the 'compare' sum until there are no more elements with the 'temp' value
                {
                    compare += temp;
                    input.Remove(temp);
                }
                if (compare > output) //compares if the score is bigger than the current biggest score.
                    output = compare;
            }
            return output; //returns the data
        }

Pastebin code for colored code

1

u/blanket_warrior Dec 18 '19 edited Dec 18 '19

Haskell (using list comprehension)


sectionUpper:: [Integer] -> Integer
sectionUpper [] = 0
sectionUpper xs = maximum [ sum [ y | y <- xs, y==x ] | x <- xs ]

1

u/seth5124 Dec 18 '19

Java w/ Bonus - Run time w/ input parsing: ~350ms

package com.company;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;




public class Main {
    public static void main(String[] args) throws FileNotFoundException {

        long startTime = System.currentTimeMillis();
        File file = new File("yahtzee-upper-1.txt");
        Scanner input = new Scanner(file);
        ArrayList<Long> rollInput = new ArrayList<>();
        while (input.hasNextLong()) {
            rollInput.add(input.nextLong());
        }
        System.out.println("Values added");

        int iterator = 0;
        long highestDie = 0;
        long highestScore = 0;
        HashMap<Long, Long> knownRolls = new HashMap<>();

        for (long x : rollInput) {
            do {
                if (knownRolls.containsKey(x)) {

                    knownRolls.put((x), (knownRolls.get(x) + 1));
                    if ((knownRolls.get(x) * x) > highestScore) {
                        highestScore = knownRolls.get(x) * x;
                        highestDie = x;
                    }
                } else {
                    knownRolls.put(x, (long) 1);
                }
                iterator++;
            } while (iterator < knownRolls.size());

        }

        System.out.println("Your highest score is " + highestScore + ", achieved by scoring your " + highestDie + "'s.");
        long endTime = System.currentTimeMillis();
        System.out.println(endTime - startTime);


    }

}

1

u/Hellakittehs Dec 19 '19

Java with bonus

public static int upperScore(int[] rolls){
        Map<Integer, Integer> scoreMap = new HashMap<>();
        Arrays.stream(rolls).forEach(roll -> scoreMap.put(roll, scoreMap.getOrDefault(roll, 0) + roll));
        return scoreMap.values().stream().max(Integer::compare).get();
    }

1

u/kapatchie1 Dec 19 '19

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Yahtzee_Upper_Section_Scoring
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                int counterA = 0; int counterB = 0; int max = 0;

                Console.WriteLine("Enter Dice Scores seperated by a space"); 
                string input = Console.ReadLine();

                int[] yahtzee_Upper = input.Split(' ')
                    .Select(int.Parse).ToArray();

                for (int i = 0; i < yahtzee_Upper.Length; i++)
                {
                    counterA = yahtzee_Upper[i];

                    for (int y = 0; y < yahtzee_Upper.Length; y++)
                    {

                        if (yahtzee_Upper[i] == yahtzee_Upper[y])
                            counterB++;
                    }

                    if (counterA * counterB > max)
                        max = counterA * counterB;
                    counterB = 0;
                }
                Console.WriteLine(max); Console.ReadLine(); Console.Clear();
            }
        }
    }
}

1

u/coniferish Dec 20 '19

Python (1 line)

def yahtzee_upper(lst):
    return(max([lst.count(x)*x for x in lst]))

1

u/GoldenShadowBG Dec 24 '19

C#. A lot of 'follow code lines'

using System;
using System.Collections.Generic;

namespace ConsoleApp6
{
    class Program
    {
        static void Main(string[] args)
        {
            string consoleInputString = Console.ReadLine();
            consoleInputString = consoleInputString + " ";
            Dictionary<int, int> numberList = new Dictionary<int, int>();
            int currentNumber = 0;
            for (int i = 0; i < consoleInputString.Length; i++)
            {
                if (consoleInputString[i] >= '0' && consoleInputString[i] <= '9')
                {
                    
                    currentNumber = currentNumber * 10 + ((int)consoleInputString[i] - 48);
                }
                else
                {
                    if (numberList.ContainsKey(currentNumber) == true)
                    {
                        numberList[currentNumber] += currentNumber;                       
                        Console.WriteLine(numberList[currentNumber]);
                    }
                    else
                    {
                        Console.WriteLine(currentNumber);
                        numberList.Add(currentNumber, currentNumber);
                    }
                    currentNumber = 0;
                }
            }
            int max = 0;
            foreach ( KeyValuePair<int, int> kvp in numberList)
            {
                Console.WriteLine("Key == {0}, Value == {1}", kvp.Key, kvp.Value);
                if (kvp.Value > max) max = kvp.Value;
            }
            Console.WriteLine(max);
        }
    }
}

1

u/raevnos Dec 25 '19

tcl, with bonus:

#!/usr/bin/tclsh

proc yahtzee_upper {dice} {
    set counts [dict create]
    foreach die $dice {
        dict incr counts $die $die
    }
    ::tcl::mathfunc::max {*}[dict values $counts]
}

puts [yahtzee_upper {2 3 5 5 6}]
puts [yahtzee_upper {1654 1654 50995 30864 1654 50995 22747 1654 1654
    1654 1654 1654 30864 4868 1654 4868 1654 30864 4868 30864}]
puts [yahtzee_upper [split [read -nonewline stdin] "\n"]]

1

u/sudhackar Dec 27 '19

swi prolog with bonus. Use include/3 to count. Probably O(N^(2))

count(List, Element, Count) :-
    include(=(Element), List, RList), length(RList, Count).

score(Weights, Value, Result) :-
    count(Weights, Value, N), Result = N * Value.

yahtzee_upper(Dice, Score) :-
    maplist(score(Dice), Dice, Scores), max_list(Scores, Score).

test(Dice, Expected) :-
    yahtzee_upper(Dice, Result),
    Result =:= Expected.

tests :-
    test([2, 3, 5, 5, 6], 10),
    test([1, 1, 1, 1, 3], 4),
    test([1, 1, 1, 3, 3], 6),
    test([1, 2, 3, 4, 5], 5),
    test([6, 6, 6, 6, 6], 30),
    test([1654, 1654, 50995, 30864, 1654, 50995, 22747, 1654, 1654, 1654, 1654, 1654, 30864, 4868, 1654, 4868, 1654, 30864, 4868, 30864], 123456),
    write('All tests passed.'),
    nl.

:- initialization(tests).

1

u/fosterhenry03 Dec 29 '19

int max(int);

int find (int, string[]);

int main(int argc, string argv[])

{

int answer = 0;

//checking input

if(argc != 6)

{

printf("must input 5 numbers in the command line\n");

return 1;

}

for(int i = 0; i < 6; i++)

{

int x = atoi(argv[i]);

if(x > 6 || x < 0)

{

printf("numbers must be between 1 and 6 inclusive\n");

return 2;

}

}

// iterate through all possible ways

for(int i = 0; i < 6; i++)

{

answer = max(i * find(i, argv));

}

printf("%i\n", answer);

}

int find(int n, string argv[6])

{

int counter = 0;

for(int i = 0; i < 6; i++)

{

int x = atoi(argv[i]);

if(x == n)

{

counter += 1;

}

}

return counter;

}

int max(int new_n)

{

static int old_n = 0;

if(new_n > old_n)

{

old_n = new_n;

return new_n;

}

else

{

old_n = new_n;

return old_n;

}

}

1

u/mhornberger Dec 30 '19 edited Dec 30 '19

In Python 3.6:

 def yahtzee_upper(ls):
     return max([ls.count(x)*x for x in ls])

This 2nd version handles more input:

 def yahtzee_upper(ls):
     scores = []
     for x in ls:
         x=str(x)
         scores.append([len(x), max([x.count(i)*int(i) for i in x]),x])  
return scores

It returns a list with the number of sides, max score for that roll, and the roll as well.

1

u/skate777771 Jan 03 '20

Dic = {}

for i in x:

try:

	Dic\[i\] = Dic\[i\] + 1

except:

	Dic\[i\] = 1

Maior = 0

for i in Dic:

if (i\*Dic\[i\])>Maior:

	Maior = (i\*Dic\[i\])

print(Maior)

1

u/Inmassss Jan 07 '20

import random

def yahtzee(result = [random.randint(1,6),random.randint(1,6),random.randint(1,6),random.randint(1,6),random.randint(1,6)]):

reps = [result.count(1),result.count(2),result.count(3),result.count(4),result.count(5),result.count(6)]

repsnum = [result.count(1)*1,result.count(2)*2,result.count(3)*3,result.count(4)*4,result.count(5)*5,result.count(6)*6]

repeated= []

for x in reps:

if x > 1:

repeated.append(x)

if reps.count(1) == 5:

score = max(result)

elif len(repeated) == 1:

score = repeated[0]*(reps.index(repeated[0])+1)

else:

score = max(repsnum)

return f'Your result is {result[0]},{result[1]},{result[2]},{result[3]},{result[4]}\nYour score is {score}'

print(yahtzee())

1

u/oschonrock Jan 08 '20 edited Jan 08 '20

Here is a completely OTT, prematurely optimised version in C++.

It achieves complete parse and compute in < 8ms on my machine (i7 2600 with SSD) for the provided 1MB file on github. Most of that is parse (~6ms).

Tricks are:

  1. Use memory mapped file
  2. Make no std::string and no copies. Use std::string_view throughout. The mmap file gives a const char* buffer which we can parse over and access using std::string_view .
  3. Use <charchonv> from_chars because it's very fast
  4. Use the awesome ska::bytell_hash_map from here
  5. all the os::... utility wrappers are my own from here.
  6. Compiled with clang-9 -O3 for x64. Platform is Kubuntu 19.10.
#include "os/fs.hpp"
#include "os/str.hpp"
#include "flat_hash_map/bytell_hash_map.hpp"
#include <cstdint>
#include <iostream>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>

uint64_t yahtzee_upper(const std::string& filename) {
  auto mfile = os::fs::MemoryMappedFile(filename);

  auto max_total = std::uint64_t{0};
  auto accum     = ska::bytell_hash_map<std::uint64_t, std::uint64_t>{};

  os::str::proc_words(
      mfile.get_buffer(),
      [&](std::string_view word) {
        auto die = os::str::from_chars<std::uint64_t>(word);
        auto total = accum[die] += die;
        if (total > max_total) max_total = total;
      },
      os::str::ascii::isnumeric);
  return max_total;
}

int main(int argc, char* argv[]) {
  if (argc < 2) return 1;
  std::cout << yahtzee_upper(argv[1]) << std::endl;
  return 0;
}

1

u/Edwizzy102 Jan 09 '20

golang:

    package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

func main() {
	file, _ := os.Open("yahtzee-upper-1.txt")

	scan := bufio.NewScanner(file)

	var slice []int
	var currentMultiplicity int = 0
	var currentMax int64 = 0
	var tmp int64


	for scan.Scan() {
		val, _ := strconv.ParseInt(scan.Text(), 10, 32)
		slice = append(slice, int(val))
	}

	sort.Ints(slice)

	for i := range slice {
		if i == 0 {
			currentMax = int64(slice[i])
			currentMultiplicity = 1
		}

		if i > 0 && slice[i-1] == slice[i] {
			currentMultiplicity += 1;
			tmp = int64(currentMultiplicity * slice[i])
			if tmp > currentMax {
				currentMax = tmp
			}
		}
	}

	fmt.Println(currentMax)
}

1

u/PoonjabiPikachu Jan 09 '20 edited Jan 09 '20

Java:

package com.something;

import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Random;
import java.util.Set;

public class Main {
	public static void main(String[] args) {
		long startTime = System.nanoTime();
		Random rm = new Random();
		System.out.println("Hello!");
		Long[] arr = null;
		arr = new Long[10];
		for (int i = 0; i < arr.length; i++) {
			arr[i] = rm.nextLong();
		}
		System.out.println("--------------------The Numbers-----------------------");
		for (int i = 0; i < arr.length; i++) {
			System.out.println(arr[i]);
		}
		System.out.println("------------------------------------------------------");
		Set<Long> set = new LinkedHashSet<Long>(Arrays.asList(arr));
		long count = 0;
		for (int i = 0; i < set.toArray().length; i++) {
			long value;
			value = (long) set.toArray()[i];
			System.out.println(value);
			count = count + value;
		}
		System.out.println("--------------------Total Count ----------------------");
		System.out.println(count);
		long endTime = System.nanoTime();
		long totalTime = endTime - startTime;
		System.out.println(totalTime);
	}
}

1

u/Pto2 Jan 10 '20

Python 3

"""
>>>Yahtzee Baby<<<
"""

#Version One: first attempt; meh
def yahtzee_upper(numsIn):
    scores = []
    for num in numsIn:
        scores.append(num*numsIn.count(num))
    print("Max 1: ", max(scores)) 


#Version Two: Condensed
def yahtzee_upper2(numsIn):
    print("Max 2: " , max([score * numsIn.count(score) for score in numsIn]))

Yea I used print statements instead of return.

1

u/shepherdjay Jan 10 '20 edited Jan 12 '20

Python 3

Haven't cracked O(n) yet but this is O(n + k) where k is the number of unique values in the list. Worse case it is still O(n2 ) - works on that file in 0.118 seconds (not including parsing).

Edit: I guess this is O(n) need to revisit time complexity self study.

def yahtzee_upper(dice_roll: List):
  results_dict = {}
  for value in dice_roll:
    current_value = results_dict.get(value, 0)
    results_dict[value] = current_value + value
  
  cur_max = 0
  for k, v in results_dict.items():
    if v > cur_max:
        cur_max = v

  return cur_max, len(results_dict)

# Solution for the input file is (31415926535, 790), 790 being the number of unique values

3

u/[deleted] Jan 12 '20 edited Jan 12 '20

Hi! your worse case is O(n), I don't know why you think it's polynomial but it's definitely linear (k must be <= n it's not a quadratic function of n so your solution is O(n + k) = O(n + n) = O(n) )

2

u/shepherdjay Jan 12 '20

Thanks for the clarification. Time complexity is still one of those aspects that evade me

1

u/[deleted] Jan 12 '20 edited Jan 12 '20

Go

O(n) solution based on dictionaries, it takes 0.04s of real time

main.go

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	input := getInput("./input.txt")
	fmt.Println(getMaxScore(input))
}

func getInput(fileName string) []int {

	var input []int
	file, _ := os.Open(fileName)
	//defer file.Close()

	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		number, _ := strconv.Atoi(fmt.Sprintf(scanner.Text()))
		input = append(input, number)
	}

	return input
}

func getMaxScore(input []int) int {
	allScores := make(map[int]int)
	maxScore := 0
	for i := 0; i < len(input); i++ {
		allScores[input[i]] += input[i]
	}

	for _, v := range allScores {
		if v > maxScore {
			maxScore = v
		}
	}

	return maxScore
}

1

u/GoAwayStupidAI Jan 15 '20

scala

def yahtzeeUpper(rolls: List[Int]): Int = {                                                                                                                                                                                                                   
  val rollAccumulators = collection.mutable.HashMap.empty[Int, Int]                                                                                                                                                                                                      
                                                                                                                                                                                                                                                              
  def get(roll: Int) = rollAccumulators.get(roll) getOrElse 0                                                                                                                                                                                                 
                                                                                                                                                                                                                                                              
  def inc(roll: Int) = {                                                                                                                                                                                                                                      
    val newAcc = get(roll) + roll                                                                                                                                                                                                                             
    rollAccumulators.put(roll, newAcc)                                                                                                                                                                                                                        
    newAcc                                                                                                                                                                                                                                                    
  }                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                              
  var max = 0                                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                              
  for(roll <- rolls) {                                                                                                                                                                                                                                        
    val possibleMax = inc(roll)                                                                                                                                                                                                                               
    if (possibleMax > max) max = possibleMax                                                                                                                                                                                                                  
  }                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                              
  max                                                                                                                                                                                                                                                         
}                                                                                                                                                                                                                                                             

1

u/khalidkh Jan 15 '20

Python 3

import time

def yahtzee_upper(rolls):
    m = 0
    d = dict()
    for r in rolls:
        if r in d: d[r] = d[r]+1
        else: d[r]=1
    for k in d:
        m = max(m, int(k)*d[k])
    return m

def yahtzee_upper_file(file_name):
    with open(file_name) as file_obj:
        lines = file_obj.readlines()
    return yahtzee_upper(lines)

#-----------------------------------------------

start_time = time.time()

result = yahtzee_upper([2, 3, 5, 5, 6])
print(result)

result = yahtzee_upper([1654, 1654, 50995, 30864, 
1654, 50995, 22747, 1654, 1654, 1654, 1654, 1654, 
30864, 4868, 1654, 4868, 1654, 30864, 4868, 30864])
print(result)

result = yahtzee_upper_file('yahtzee-upper-1.txt')
print(result)

print("--- %s seconds ---" % (time.time() - start_time))

Output

10
123456
31415926535
--- 0.03699994087219238 seconds ---

1

u/neur0sys Jan 16 '20 edited Jan 16 '20

Emacs Lisp

(defun yahtzee-upper (v)
  (let ((bucket (make-hash-table)))
    (dolist (d v)
      (let ((sum (gethash d bucket)))
        (if (not sum)
            (puthash d d bucket)
          (puthash d (+ d sum) bucket))))
    (let ((max-sum 0))
      (maphash (lambda (k v)
                 (if (> v max-sum)
                     (setq max-sum v)))
               bucket)
    max-sum)))

;; tests
(let ((inputs (list
               (list (list 2 3 5 5 6) 10)
               (list (list 1 1 1 1 3) 4)
               (list (list 1 1 1 3 3) 6)
               (list (list 1 2 3 4 5) 5)
               (list (list 6 6 6 6 6) 30)
               (list (list 1654 1654 50995 30864 1654
                           50995 22747 1654 1654 1654
                           1654 1654 30864 4868 1654
                           4868 1654 30864 4868 30864) 123456))))
  (dolist (input inputs)
    (let* ((data (elt input 0))
           (result (elt input 1)))
      (if (/= (yahtzee-upper data) result)
          (throw 'test-failed input)))))

1

u/DaVileKial6400 Jan 17 '20 edited Jan 17 '20

Here is my attempt using JavaScript.

Web browsers took about 0.2 - 0.4 seconds to solve the 100,000 problem.

Though Edge has a problem reloading the script and it jumps to a whopping 50 secs to solve if you refresh.

To test you have to fill the diceRolls array with input numbers.

diceRolls = [];
var d; 
var stime, etime;
var added = false;
var count = [];
var sum = [];
var max = 0;

main();

function main() {
  d = new Date();
  stime = d.getTime() / 1000;
  getDiceCount();
  CountScore();
  findLargestScore();
  d = new Date();
  etime = d.getTime() / 1000;
  console.log("Start Time : " + stime)
  console.log("End Time : " + etime)
  console.log("Time Taken : " + (etime - stime))
  console.log("Upper Yahtzee : " + max);
}

function CountDice(){
  for(let i = 0; i < diceRolls.length; i++){
    added = false;
    for(let k = 0; k < count.length; k++){  
      if(diceRolls[i] == count[k][0]){
        count[k][1]++;
        added = true;
      }
    }
    if(!added){
    count.push([diceRolls[i], 1]);
    }
  }
}
 
function CountScore() {
    for(let k = 0; k < count.length; k++){
    if(count[k][1] > 1){
      sum.push(count[k][0] * count[k][1]);
    }
  }
}

function findLargestScore() {
  for(let i = 0; i < sum.length; i++){
    if(sum[i] > max){
      max = sum[i];
   }
  }
}

2

u/Kumaravel47Kums Jan 21 '20

Python 3

import collections

def yahtzee_upper(data):

c=collections.Counter(data)

list=[]

for x in c.items():

list.append(x[0]*x[1])

print(max(list))

Input :

yahtzee_upper([1654, 1654, 50995, 30864, 1654, 50995, 22747,1654, 1654, 1654, 1654, 1654, 30864, 4868, 1654, 4868, 1654,30864, 4868, 30864])

Output :

123456

1

u/boonetang3 Jan 23 '20

Python 3

from collections import Counter
def yahtzee_upper(roll):
    counter = Counter()
    for dice in roll:
        counter[dice] += 1
    max_output = 0
    for key, value in counter.items():
        if (key * value) > max_output:
            max_output = key * value
    return max_output

1

u/mayhem-makers Jan 25 '20
Python3

yahtzee_roll = [6, 6, 6, 6, 6]

def max_score(yahtzee_list):
    best_scores = []
    unique_list = list(set(yahtzee_list))
    for i in unique_list:
        score = i * yahtzee_list.count(i)
        best_scores.append(score)
    return max(best_scores)

print(max_score(yahtzee_roll))

1

u/[deleted] Jan 26 '20 edited Jan 26 '20

C# solution:

long MaxScore(params long[] dices)

=> dices.GroupBy(x => x).Select(x => x.Key * x.Count()).Max();

Tests:

Debug.Assert(MaxScore(2, 3, 5, 5, 6) == 10);

Debug.Assert(MaxScore(1, 1, 1, 1, 3) == 4);

Debug.Assert(MaxScore(1, 1, 1, 3, 3) == 6);

Debug.Assert(MaxScore(1, 2, 3, 4, 5) == 5);

Debug.Assert(MaxScore(6, 6, 6, 6, 6) == 30);

Debug.Assert(MaxScore(1654, 1654, 50995, 30864, 1654, 50995, 22747, 1654, 1654, 1654, 1654, 1654, 30864, 4868, 1654, 4868, 1654, 30864, 4868, 30864) == 123456);

Bonus Challenge

var largeData = LargeTestData.TestValues;

var sw = new Stopwatch();

sw.Start();

var result = MaxScore(largeData); // result: 31415926535

sw.Stop();

var duration = sw.Elapsed; // duration: 8ms to 21ms (run ~20 times)

Edit:

Changed datatype from int to long, since int is too small for the result of the bonus challenge.

1

u/RoastingAltAccount Jan 29 '20

Python 3.7

def yahtzee_upper(dies):

    dies.sort(); highest = 0; counts = {}

    for num in dies:
        counts[num] = counts.get(num, 0) + 1 

    for num, quan in counts.items():
        score = num*quan
        if score > highest:
            highest = score

    return highest

1

u/[deleted] Jan 31 '20

My method for unsorted list of rolls. It should handle different dices and different values:

public static int highestScore(List<Integer> results) {
		
		int k = 6; //number of dice sides
		
		Collections.sort(results);
		
		List<Integer> sums = new ArrayList<Integer>();
		
		int score = results.get(0);
		
		for (int i = 1; i < k - 1; i++) {
			if (results.get(i - 1) == results.get(i)) {
				score = score + results.get(i);
				if (i == 4) { sums.add(score); }
			} else {
				sums.add(score);
				score=results.get(i);
			}
		}
		
		Collections.sort(sums);
		int highestScore = sums.get(sums.size() - 1);

		return highestScore;
	}

It works fine in any case, but what about optimization of code? Is it good?

→ More replies (1)

1

u/NikstatioN Jan 31 '20

Trying to learn programming, and my attempt at the challenge using C++ !

#include <iostream>
#include <algorithm>
using namespace std;

int diceMax(int n[], int arrSize);

int main() {
	int arr[] = { 1654, 1654, 50995, 30864, 1654, 50995, 22747,
	1654, 1654, 1654, 1654, 1654, 30864, 4868, 1654, 4868, 1654,
	30864, 4868, 30864 };
	int col = sizeof(arr) / sizeof(arr[0]);

	printf("The higheset value is : %d", diceMax(arr, col));
	return 0;
}

int diceMax(int n[], int arrSize) {
	int pastTotal = 0, newTotal = 0;
	sort(n, n + arrSize);

	for (int i = 0; i < arrSize; i++) {
		if (n[i + 1] == n[i]) {
			newTotal += n[i];
		}
		else {
			newTotal += n[i];
			if (newTotal > pastTotal) {
				pastTotal = newTotal;
				}
			newTotal = 0;
		}
	}
	
	return pastTotal;

}

Any tips and improvements would be much appreciated ! =) Thanks !!

1

u/mickira Feb 05 '20

python3:

import random

def creator(massimo, larghezza):
    i=0
    myath=[]
    while i<larghezza:
        myath.append(random.randint(1,massimo))
        i+=1
    return calculator(myath)

def calculator(myath):
    myath.sort()
    c=0
    w=0
    i=0
    while i<len(myath):
        l=myath[0]
        punti=l
        del myath[0]
        try:
            while True:
                if myath[0]==l:
                    del myath[0]
                    punti+=l
                else: break
        except:
            pass
        if punti>w:
            w=punti
            c=l   
    return c, w

yathzeelist=open("yathzee/yahtzee-upper-1.txt","rt").read().splitlines()
for i in range(0,len(yathzeelist)):
    yathzeelist[i]= int(yathzeelist[i])
print(calculator(yathzeelist))

1

u/SkrtelSquad Feb 06 '20

I have been teaching myself Python3 for the last few days and this is my first attempt at a challenge. Any feedback would be welcome. It works but my time taken for the challenge input is 1.60 seconds.

Basically I have 2 functions. The first one, smaller, creates a list of unique values from the list of rolls.

Then the 2nd function, tally, multiplies each of the unique values against how many times it appears in the original file.

If anyone is reading and has any feedback that would be great. Thanks! I know it is not super fast. If there are any ways to make it faster without completely rewriting, that would be cool. I originally tried just doing the tally function on the whole list, however this took forever to calculate.

def smaller(rolls):
    uniques = []
    for i in range(len(rolls)):
        if rolls[i] not in uniques:
            uniques.append(rolls[i])
    return uniques

def tally(rolls):
    uniques = smaller(rolls)
    totals = []
    for i in range(len(uniques)):
        totals.append(rolls.count(uniques[i]) * int(uniques[i]))
    return max(totals)

die = open("yahtzee-upper-1.txt").readlines()

print(tally(die))

1

u/Chilllking Feb 21 '20

A bit late but maybe I can help you a bit :)

your first instinct with counting unique values is good!

Unfortnuateley your solution gets slow in the second function because you are searching the whole list of values times your unique value list which will take a lot of time the bigger the list gets (lets assume your list contains n values of which all are unique(of cause not all values are unique in reality but most will be) then you have to search your list n times which will result in n*n value compares. This means your execution time will get bigger by the square as the list gets longer.)

An improved solution here would be counting the values while you check if they are unique. To achive this you might add a new list in your smaller() function (and get rid of the tally() function completly), lets call it count = []. And as you add an unique value you will add the value 1 to count and if the value is not unique you will increase the corresponding count value by 1. So in the end you will have 2 arrays uniques and count. Now count[i] will contain the amounts of time uniques[i] is in the list. So you just multiply them and you have the biggest total value (instead of storing the count you can of cause just store the total value at the point).

Note that this solution is still not perfect since you still have to check every value in your uniques list for every new value added. This still is a lot of compares. To get rid of this you can use a dictionary (see https://www.w3schools.com/python/python_dictionaries.asp) instead of 2 arrays (see the python solutions I or others posted here).

This reduction of compares by the way ist what OP meant by:

Try to find a solution that's linear (O(N)) in both time and space requirements.

Your Solution was quadratic (O(N2)). If you are interested this time complexity and why it matters maybe have a look at https://www.hackerearth.com/de/practice/basic-programming/complexity-analysis/time-and-space-complexity/tutorial/

I hope you could understand my explanation since english is not my first laguage and I hope that I was able to help you with your python journey :)

→ More replies (2)

1

u/BadgerRobot Feb 08 '20

My first time trying this after learning about these sorts of challenges. I am relearning coding after not using it for a long time. Teaching myself Java at home working towards getting an SDET job. (I've been in QA for 20 years, but the job is changing to more coding forward.)

Here is my Java result:

package redditCodingChallenge;

import java.util.ArrayList;

public class CodingChallenge {

public static void main(String\[\] args) {

	ArrayList<Integer> diceRoll = new ArrayList<Integer>();

	//\[2, 3, 5, 5, 6\] == 30

	diceRoll.add(2);

	diceRoll.add(3);

	diceRoll.add(5);

	diceRoll.add(5);

	diceRoll.add(6);

	

	System.out.println(YahtzeeScoringChallenge.yahtzeeUpper(diceRoll));

}

}

package redditCodingChallenge;

import java.util.ArrayList;

public class YahtzeeScoringChallenge {

// Challenge #381 [Easy] Yahtzee Upper Section Scoring

public static int yahtzeeUpper(ArrayList<Integer> roll) {

  int result = 0;

  int max = 0;

for(int i = 1 ; i <=6 ; i++) {

result = i * countInList(roll, i);

if (result > max) {

max = result;

}

}

return max;

}

private static int countInList(ArrayList<Integer> roll, int n) {

int count = 0;

for (int i = 0; i < roll.size(); i++) {

if (roll.get(i).equals(n)) {

count++;

}

}

return count;

}

}

1

u/a_bcd-e Feb 08 '20

c++. O(N log N) as I couldn't find a solution in O(N).

#include <stdio.h>
#include <map>
#include <vector>
using namespace std;
int main(){
    int a,n;
    long long max=0;
    map<int,long long> m;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&a);
        m[a]++;
    }
    for(auto &p:m)if(max<p.first*p.second)max=p.first*p.second;
    printf("%lld",max);
}
→ More replies (5)

1

u/[deleted] Feb 09 '20 edited Dec 03 '25

sheet pet follow sugar distinct handle amusing dinosaurs steep one

This post was mass deleted and anonymized with Redact

1

u/sebamestre Feb 10 '20 edited Feb 10 '20

Here is a small O(dice * faces) time solution (though it is hardcoded for 6 faces). It is the first thing that came to mind.

const yahtzee_upper = arr => [1,2,3,4,5,6]
	.map(x => x * arr.filter(y => y == x).length)
	.reduce((x,y) => Math.max(x,y), 0);

For the more general case, I came up with this O(dice) space and time solution:

const yahtzee_upper = arr => {
	let counts = new Map();
	arr.forEach(x => counts.set(x, 0));
	arr.forEach(x => counts.set(x, counts.get(x) + x));
	return [...counts.values()].reduce((a,b) => Math.max(a,b), 0);
};

The entire runtime was 430ms, but after adding some timers, it seems like this function only takes 55ms, with IO & number parsing taking 49ms.

55ms + 49ms = 104ms, which is under 25% of the total run-time. I don't know where the rest comes from but I'd say it's probably Node's startup time.

1

u/Zezeroth Feb 11 '20 edited Feb 11 '20

Quick and Dirty Golang. Havent spent any time making it efficient

package main

import "fmt"
import "strconv"

func main() {
    dice0 := []int {2, 3, 5, 5, 6}
    dice1 := []int {1, 1, 1, 1, 3}
    dice2 := []int {1, 1, 1, 3, 3}
    dice3 := []int {1, 2, 3, 4, 5}
    dice4 := []int {6, 6, 6, 6, 6}
    
    _, score := calculateHighestPossibleScore(dice0)
    
    fmt.Println("Score: " + strconv.Itoa(score))
    _, score = calculateHighestPossibleScore(dice1)
    
    fmt.Println("Score: " + strconv.Itoa(score))
    _, score = calculateHighestPossibleScore(dice2)
    
    fmt.Println("Score: " + strconv.Itoa(score))
    _, score = calculateHighestPossibleScore(dice3)
    
    fmt.Println("Score: " + strconv.Itoa(score))
    _, score = calculateHighestPossibleScore(dice4)
    
    fmt.Println("Score: " + strconv.Itoa(score))
    bonus := []int {1654, 1654, 50995, 30864, 1654, 50995, 22747, 1654, 1654, 1654, 1654, 1654, 30864, 4868, 1654, 4868, 1654, 30864, 4868, 30864}`
    _, score = calculateHighestPossibleScore(bonus)
    
    fmt.Println("Score: " + strconv.Itoa(score))

}

func findRangeForDice(dice []int) (int, int) {
    low  := dice[0]
    high := dice[0]

    for die := range dice {
	if low > dice[die] {
		low = dice[die]
	}
	if high < dice[die] {
		high = dice[die]
	}
    }
    return low, high
}

func calculateHighestPossibleScore(dice []int) (int, int) {
    low, high := findRangeForDice(dice)
   
    var highDie int = 0
    var highScore int = 0

    for i := low; i <= high; i++ {
    	score := calculateScoreForValue(i, dice)
    	if highScore < score {
    		highScore = score
    		highDie = i
    	}
    }
    
    return highDie, highScore
}

func calculateScoreForValue(die int, dice []int) int {
    var count int = 0
    
    for d := range dice {
    	if dice[d] == die {
    		count++;
    	}
    }
    return count * die
}

1

u/AdzzZa Feb 12 '20 edited Feb 12 '20

Simple python solution:

sez = [1654, 1654, 50995, 30864, 1654, 50995, 22747, 1654,1654,1654,1654,1654,30864,4868,4868,1654,30864,4868,30864]

def Yahtzee(sez):

   return(max([sez.count(i) * i for i in sez]))

print(Yahtzee(sez))

1

u/pamdirac1 Feb 16 '20

Rust

extern crate elapsed;
use elapsed::measure_time;
use std::collections::HashMap;
use std::io::{self, BufRead};
use std::path::Path;
use std::fs::File;
use std::num::ParseIntError;

fn main()  {
    let mut vector: Vec<u64> = vec![];
    if let Ok(lines) = read_lines("src/yahtzee-upper.txt") {
        // Consumes the iterator, returns an (Optional) String
        for line in lines {
            let s = line.unwrap().trim().parse::<u64>().unwrap();
            vector.push(s);
        }
    }



//    let nums = read(File::open("yahtzee-upper.txt")?)?;
    let (elapsed, sum) = measure_time(|| {
        let value:u64 = yahtzee_upper(vector.as_slice(), vector.len());
        println!("\nvalue {}", value);
    });
    println!("elapsed = {}", elapsed);

}


fn yahtzee_upper(v:&[u64], dice_sides:usize) -> u64 {
    let mut counter:HashMap<u64, u64> = HashMap::with_capacity(dice_sides);
//    let mut counter:Vec<u64> = vec![0; dice_sides];
    for idx in 0..v.len() {
        let num = v[idx];
        let val = match counter.get(&num) {
            Some(x) => x,
            None => &0
        };
        counter.insert(num, val + num);
    }
   *counter.values().max_by(|x, y| x.cmp(y)).unwrap()
}

fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
    where P: AsRef<Path>, {
    let file = File::open(filename)?;
    Ok(io::BufReader::new(file).lines())
}

1

u/Mathgeek007 Feb 17 '20

GolfScript

[0]6*\.,{)(:i;\..i<@i=i)+@i>(;++\}\*;$)\;

Try it online!

2

u/killer_unkill Mar 01 '20

Earlier people used to write golfscript in pearl. Awseme solution

→ More replies (1)

1

u/Chilllking Feb 21 '20 edited Feb 21 '20

am a bit late to the party but here is my Python solution:

f = open ("yahtzee-upper-1.txt")
diceNumbers = f.readlines()
counts = {}
for numberString in diceNumbers:
    number = int(numberString.strip())
    if counts.get(number) == None:
        counts[number] = 1
    else:
        counts[number] += 1
highest = 0
highestVal = 0
for x in counts:
    if (x * counts[x]) > highestVal:
        highest = x
        highestVal = x * counts[x]

print (str(highest) + " gets the highest value with " + str(counts[highest]) + " occurences and a total Value of: " + str(highestVal))

the solution it gets is:

897597901 gets the highest value with 35 occurences and a total Value of: 31415926535

EDIT: my first time posting code, had to reformat it cause im stupid

1

u/Fiuryn Feb 21 '20 edited Feb 21 '20

c# Solution

       static void Main(string[] args)
        {
           Yahtzee.Print(Yahtzee.diceRoll);
           Console.ReadLine();
        }
     public class Yahtzee
    {
       static Random dice = new Random();
       public static int[] diceRoll = new int[5];
        public static int[] Roll(int[] roll)
        {
            
            for (int i = 0; i < 4; i++)
            {
               roll[i] = dice.Next(1, 6);
            }
            return diceRoll;
        }   
        public static void Print(int[] score)
        {
            for (int i = 0; i < 5; i++)
            {
                Roll(diceRoll);
                Console.WriteLine($"[{score[0]},{score[1]},{score[2]},{score[3]},{score[4]}]");
                var dict = new Dictionary<int, int>();
                foreach (var value in score)
                {
                    if (dict.ContainsKey(value))
                        dict[value]++;
                    else
                        dict[value] = 1;
                }
                var maxk = 0;
                var maxv = 1;
                foreach (var pair in dict)
                {
                    if (pair.Value > maxv)
                    {
                        maxk = pair.Key;
                        maxv = pair.Value;
                    }
                }
                Console.WriteLine($"score = {maxk*maxv}");
            }

1

u/FlyingVince Feb 24 '20 edited Feb 24 '20

PHP:

function yatzee_Upper($arr){

$highest_number=max($arr); //defines how many times we have to loop $new_array=array_count_values($arr); //count occurence of each value

$final_array=[];

for ($i=0;$i<=$highest_number;$i++){

if (array_key_exists($i,$new_array)) {

array_push($final_array,($new_array[$i]*$i));

} }

$max=max($final_array);

echo $max;

}

1

u/HereBehindMyWall Feb 28 '20

Python 3:

from collections import Counter

def yaht(rolls):
    return max(k*v for k, v in Counter(rolls).items())

1

u/ArtemiyKra Jan 03 '23

Maybe I just don't understand something, but what Counter does here? This seems to work:

yahtzee_upper=lambda _:max([_.count(x)*x for x in _])

Won't Counter be slow?

1

u/AthosBlade Feb 29 '20

Haskell:

module Yahtzee  
 where  
 import Data.List   


yahtzeeUpper :: [Int] -> Int  
 comparator :: (Int, [Int]) -> (Int, [Int]) -> (Int, [Int])  
   
  yahtzeeUpper l = sum $ snd $ foldr comparator (head zips) zips   
 where groups = group l  
zips   = zip (map (\\x -> length x) groups) groups  
  comparator a b  
 | fst a > fst b || (fst a == fst b && (head $ snd a) > (head $ snd b)) = a   
 | otherwise = b

1

u/fo0man Mar 01 '20

Javascript something like:

const maxKey = Symbol('max');

const maxValue = array => array.reduce((numberMap, roll) => {

  const prev = numberMap.get(roll) || 0;

  const newVal = prev + roll;

  const max = numberMap.get(maxKey) || 0;

  numberMap.set(roll, newVal);

  if (newVal > max) numberMap.set(maxKey, newVal);

  return numberMap;

}, new Map()).get(maxKey);

Should get you there in O(n) space complexity would be O(2n) i believe.

1

u/WildRosso Mar 02 '20

My inelegant attempt using Java

import java.util.Arrays;
public class Yahtzee {

public static void main(String[] args) {

int[] Yahtzee = new int[]{1654, 1654, 50995, 30864, 1654, 50995, 22747,
1654, 1654, 1654, 1654, 1654, 30864, 4868, 1654, 4868, 1654,
30864, 4868, 30864};
int[] Storage = Yahtzee;
int score = 0;
int store = 0;
int count;
System.out.println("Yahtzee" + Arrays.toString(Yahtzee));
for (int i = 0; i < Yahtzee.length; i++) {
count = 0;
for (int j = 0; j < Storage.length; j++) {
if (Storage[j] == Yahtzee[i]) {
count++;
}
}
store = count * Yahtzee[i];
if (store > score) {
score = store;
}
}
System.out.println(" => " + score);
}

}

Took my compiler about 3s, however.

1

u/Techser Mar 02 '20

Erlang:

maxScore(L) -> max([count(L, X)*X || X <- L]).
count(L, Val) -> count(L, Val, 0, false).
count([], _, Acc, false) -> Acc;
count([], _, Acc, true) -> Acc + 1;
count([H | T], Val, Acc, false) -> count(T, Val, Acc, H == Val);
count([H | T], Val, Acc, true) -> count(T, Val, Acc + 1, H== Val).

1

u/purpletquertz Mar 06 '20

Here is my python version:

import requests
url = 'gist url' 
response = requests.get(url)
data = [int(i) for i in response.text.split('\n')]

def calc(base, seen):
    lkp = {}
    for n in base:
        if n not in seen and n > max(seen): 
            lkp[n] = base.count(n) * n
            seen.append(n)
        else:
            pass

    print(max(lkp.values()))

calc(base = data, seen = [0])

1

u/OptionX Mar 07 '20

Java with lambdas for fun

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Main {

    public static void main(String[] args) throws Exception {

        long start = System.currentTimeMillis();
        BufferedReader br = new BufferedReader(new FileReader("yahtzee-upper-1.txt"));
        List<Long> input = new ArrayList<>();
        String line = null;
        while ((line = br.readLine()) != null) {
            input.add(Long.parseLong(line));
        }
        long answer = yahtzee_upper(input);
        System.out.println("Got:" + answer + " Time: " + (System.currentTimeMillis() - start) + "ms");
    }

    static long yahtzee_upper(List<Long> input) {
        return input.stream().collect(Collectors.toMap(
                Function.identity(),
                Function.identity(),
                (oldVal, newVal) -> oldVal + newVal
        )).values().stream().mapToLong(e -> e).max().orElse(-1);
    }
}

I feel it could be faster. Not sure if its the lambda usage or I did something wrong. Probably the latter.

1

u/Sarius- Mar 11 '20 edited Mar 12 '20

Python 3.6

def yahtzee_upper(list):
    before = 0
    for i in range(1, 7):
        if list.count(i) * i > before:
            before = list.count(i) * i
    return before
→ More replies (2)

1

u/better-contract Mar 12 '20

Code is in python. Would appreciate feedback! Thanks.

def yahtzee_upper(dice_roll):
    map = {}
    for i in dice_roll:
        map[i] = map.get(i, 0) + 1
    return max([x * y for (x,y) in map.items()])

1

u/bruce3434 Mar 13 '20

Nim

import tables, os, memfiles

proc parseUInt(data: cstring, size: int): uint {.inline.} =
  let zero = ord('0')
  let nine = ord('9')
  for i in 0 ..< size:
    let code = ord(data[i])
    if code >= zero and code <= nine:
      result *= 10
      result += uint(code - zero)
    else:
      return 0  #non-digit; parse error

proc ytz(path: string): uint =
    var
      table = initTable[uint, uint]()
      mf = memfiles.open(path)
    if mf.mem == nil:
        quit("Cannot open file")
    for s in memSlices(mf):
        let n = parseUInt(cast[cstring](s.data), s.size)
        if n == 0:
            quit("Parse error")
        table.mgetOrPut(n, 0) += n
    for key, val in table:
        result = max(result, val)
    mf.close

proc main() =
    echo ytz(paramStr(1))

main()

Runs in 0.04 seconds in my machine for the big input.

1

u/[deleted] Mar 19 '20

I changed the output a little so you know what number is the max as well. Perhaps adding a fueature for the max count as well as the max sum so you know which set is the best.

import numpy as np

def yahtzee_upper(numbers):
    numbers = np.array(numbers)
    unique, counts = np.unique(numbers, return_counts=True)
    dictionary = zip(unique, counts)
    maxes = []
    for i,s in dictionary:
        maxes.append([i, (i*s)])

    max = np.max(maxes)

    for i, s in maxes:
        if s == max:
            print('yahtzee_upper({}) => {}:{}'.format(numbers, i, s))

1

u/Hvedar13 Dec 15 '21

return max(unique*counts) # using pairwise array multiplication

1

u/Dfree35 Mar 23 '20

Posted in the wrong thread originally lol

Python 3.6

Tried to keep it simple

def yahtzee_upper(roll_list):
    possible_scores = []
    for roll in roll_list:
        possible_scores.append(roll_list.count(roll) * roll)

    return max(possible_scores)

1

u/AntillaOnAsiaa Mar 25 '20

Python:

def yatsi(list):
	maksimi=max(list, key=list.count)
	return maksimi*list.count(maksimi)
lista = [6,6,6,6,6]
yatsi(lista)

1

u/Hvedar13 Dec 15 '21

try [1,2,3,4,5]

1

u/gpalyan Mar 27 '20 edited Mar 27 '20
public int yahtzeeUpper(final Collection<Integer> rolls) {
    final Map<Integer, Integer> rollToScoreMap = new HashMap<>();
    for (final int roll : rolls) {
        if (rollToScoreMap.containsKey(roll)) {
            rollToScoreMap.put(roll, sideToScoreMap.get(roll) + roll);
        } else {
            rollToScoreMap.put(roll, roll);
        }
    }
    return max(rollToScoreMap.values());
}

@Test
public void test_yahtzeeUpper() {
    assertThat(yahtzeeUpper(newArrayList(2, 3, 5, 5, 6))).isEqualTo(10);
    assertThat(yahtzeeUpper(newArrayList(1, 1, 1, 1, 3))).isEqualTo(4);
    assertThat(yahtzeeUpper(newArrayList(1, 1, 1, 3, 3))).isEqualTo(6);
    assertThat(yahtzeeUpper(newArrayList(1, 2, 3, 4, 5))).isEqualTo(5);
    assertThat(yahtzeeUpper(newArrayList(6, 6, 6, 6, 6))).isEqualTo(30);
    assertThat(yahtzeeUpper(newArrayList(1654, 1654, 50995, 30864, 1654, 50995, 22747, 1654, 1654, 1654,
            1654, 1654, 30864, 4868, 1654, 4868, 1654, 30864, 4868, 30864))).isEqualTo(123456);
}

1

u/ImproperGesture Mar 30 '20 edited Mar 30 '20

Haskell with Data.Map.Strict (pretending you don't have group, which makes it trivial)

import Data.Map.Strict (Map)

import qualified Data.Map.Strict as Map

yahtzee_upper :: [Int] -> Int

yahtzee_upper xs = foldr max 0 [ x * y | (x, y) <- counts xs ]

counts :: [Int] -> [(Int, Int)]

counts xs = Map.toList $ fillCounts xs (Map.fromList [(x, 0) | x <- xs])

fillCounts :: [Int] -> Map.Map Int Int -> Map.Map Int Int

fillCounts [] merp = merp

fillCounts (x:xs) merp = fillCounts xs $ Map.adjust ((+) 1) x merp

1

u/broem86 Apr 11 '20 edited Apr 11 '20

I've been learning Elixir and have found it to be really awesome. Here's my take which handles the optional bonus handily.

defmodule Yahtzee do
  def yahtzee_upper(somelist) do
    somelist
    |> Enum.reduce(%{}, fn x, acc -> Map.update(acc, x, 1, &(&1 + 1)) end)
    |> Enum.map(fn {k, v} -> k*v end)
    |> Enum.max
  end
end

There may be more elegant solutions but this one makes sense to me.

Optional Answer (hehe): 31415926535

Edit: takes roughly .18 sec on 100k input fwiw

1

u/sunnyabd Apr 11 '20

Fun complexity exercise. Would love some tips and critiques to improve this.

import cProfile
# load and sort rolls. given that yahtzee gives rolls in ascending order
rolls = open("yahtzee-upper-1.txt", "r")
roll = []
for x in rolls.readlines():
    roll.append(int(x.strip()))
roll = sorted(roll)


def yahtzee_upper(roll = [2, 3, 5, 5, 6]):

    # initialize variables
    # since numbers sorted, similar numbers will be grouped together,
    # currNum used to find last of group of numbers
    # count used to count how many of the same numbers are in the roll
    # highscore used to save highest score
    currNum = 0
    count = 1
    highscore = 0
    roll.append(0)
    for nextNum in roll:
        if currNum != nextNum:
            if currNum*count > highscore:
                highscore = currNum*count
            currNum = nextNum
            count = 1
        elif currNum == nextNum:
            count += 1
    print(highscore)

#print(yahtzee_upper([1, 1, 1, 3, 3]))
cProfile.run('yahtzee_upper(roll)')
# 0.011s function runtime

1

u/caninbc Apr 26 '20 edited Apr 26 '20

C++ , no bonus

#include <iostream>

using namespace std;

int main() {

int j=6,score=0,sum=0;

int YahtzeeUpper[5] = {1,1,1,2,2};

for(int i = 4; i>=0; i--){

	if(YahtzeeUpper[i]==j){

		sum += j;

		if(sum>score) score = sum;

	}

	else if (j!=0){

		j--;

		i++;

		sum = 0;	

	}

}

cout<<"Score ="<<score;

}

Feedback welcome

→ More replies (1)

1

u/wcypierre Apr 26 '20

C#

using System;
using System.Collections.Generic;

namespace Reddit.DailyProgrammer._381.YahtzeeUpperSection
{
    class Program
    {
        static void Main(string[] args)
        {
            yahtzee_upper(new List<int>() { 2, 3, 5, 5, 6 });
            yahtzee_upper(new List<int>() { 1, 1, 1, 1, 3 });
            yahtzee_upper(new List<int>() { 1, 1, 1, 3, 3 });
            yahtzee_upper(new List<int>() { 1, 2, 3, 4, 5 });
            yahtzee_upper(new List<int>() { 6, 6, 6, 6, 6 });
            yahtzee_upper(new List<int>() {
                1654, 1654, 50995, 30864, 1654, 50995, 22747,
                1654, 1654, 1654, 1654, 1654, 30864, 4868, 1654, 4868, 1654,
                30864, 4868, 30864
            });
            Console.ReadKey();
        }

        static void yahtzee_upper(List<int> list)
        {
            list.Sort();

            int maxTotal = 0;
            int currentTotal = 0, currentDigit = 0;

            foreach(var num in list)
            {
                if(num != currentDigit)
                {
                    currentTotal = num;
                    currentDigit = num;

                    if(currentTotal > maxTotal)
                    {
                        maxTotal = currentTotal;
                    }
                }
                else if(num == currentDigit)
                {
                    currentTotal += num;

                    if (currentTotal > maxTotal)
                    {
                        maxTotal = currentTotal;
                    }
                }
            }

            Console.WriteLine(maxTotal);
        }
    }
}

1

u/sadsoulthatslost Jan 13 '22
def yahtzee_upper(list):
op=[]
for x in list:
    op.append(list.count(x)*x)
j=max(op)
return j

1

u/[deleted] Feb 18 '22

python

accidentally did optional as well

def yahtzee_upper(list):
dict = {}

for i in list:
    dict[i] = 0

for i in list:
    dict[i] += 1

list = [dict[i]*i for i in dict]
return max(list)

yahtzee_upper([1654, 1654, 50995, 30864, 1654, 50995, 22747, 1654, 1654, 1654, 1654, 1654, 30864, 4868, 1654, 4868, 1654, 30864, 4868, 30864])

1

u/sgaltair Mar 08 '22

Powershell 7

I just like doing these. I script in Powershell for work. Feedback welcome! About 2.18 seconds on the challenge set. Not the fastest language, apparently. Or maybe it's the method.

$rolls = $args[0]
[system.collections.arraylist]$al = @()
$rolls.split(",") | foreach-object {[void]$al.add($_)}
$al = $al.trim()
$max = ($al | measure-object -maximum).maximum

$al | group-object | foreach-object {$hl = @{} } {$hl[$_.name] = $_.count } {$null}
$highest_c = ($al | group | select -ExpandProperty count | measure-object -maximum).maximum
$highest_n = $hl.GetEnumerator() | where-object value -eq $highest_c | select -expandproperty name
$score = (($hl.keys | foreach-object {[int]$_ * [int]$hl.$_}) | measure-object -maximum).maximum

$score

1

u/[deleted] Jun 22 '22 edited Jun 22 '22

Rust

use std::{collections::HashMap, io::BufRead};

fn main() {
    let bytes = include_bytes!("input.txt");
    let input: Vec<usize> = bytes.lines().map(|x| x.unwrap().parse().unwrap()).collect();

    assert_eq!(max_score(&input), 31415926535);
}

fn max_score(input: &Vec<usize>) -> usize {
    let mut count = HashMap::new();

    for roll in input {
        *count.entry(roll).or_insert(0) += 1;
    }

    count.iter().map(|(k, v)| *k * v).max().unwrap()
}

Measure-Command (Powershell) says the average time with the challenge input is just over 17 ms on a 7700k.

1

u/Zonetecde Aug 01 '22

c#

private static int yahtzeeUppernew (List<int> v)

{

return v.GroupBy(i => i).ToList().ConvertAll(x => x.Key * x.Count()).Max();

}

11ms Execution time

0

u/[deleted] Jan 12 '20

[deleted] — view deleted comment

2

u/Cosmologicon 2 3 Jan 12 '20

There have been several posted already. Most posted bonus solutions are O(N). Here's one for instance.

→ More replies (1)

0

u/[deleted] Nov 11 '19

[deleted] — view deleted comment

3

u/g00glen00b Nov 12 '19

I think you're having an integer overflow for calculating the result.

0

u/[deleted] Nov 14 '19

[deleted] — view deleted comment

3

u/agenttiny200 Nov 18 '19

maybe i'm being a little daft, but could you help me understand this line:

def yahtzee_upper(roll: List[int]) -> int:
  1. What is the semicolon after 'roll' for? (eg; roll: List[int])

I get that semicolon allows continuing a python code while getting a break in, but aren't you supposed to pass arguments to the function here?

2) what is "->" for? (eg; -> int)

are you passing the arguments to int() or something?

0

u/[deleted] Nov 15 '19

[deleted] — view deleted comment

2

u/tomekanco Nov 19 '19

Love Julia: take python abstractions and get top notch performance.

→ More replies (1)

0

u/[deleted] Dec 20 '19

[deleted] — view deleted comment

1

u/smile-bot-2019 Dec 20 '19

I noticed one of these... :(

So here take this... :D

2

u/NemPlayer Dec 20 '19

I noticed one of these... :(

So here take this... :D