aboutsummaryrefslogtreecommitdiff
path: root/challenge-270/luca-ferrari/python/ch-2.py
blob: 1b9794bb2ead5c8a3c0e772f313a14f2325a9465 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!python

#
# Perl Weekly Challenge 270
# Task 2
#
# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-270>
#

import sys

# task implementation
# the return value will be printed
def task_2( args ):
    single = int( args[ 0 ] )
    double = int( args[ 1 ] )
    nums   = list( map( int, args[ 2: ] ) )

    # compute the current max
    max = None
    for v in nums:
        if not max or v > max:
            max = v

    need_operation = list( filter( lambda x: nums[ x ] < max, range( 0, len( nums ) ) ) )
    score = 0
    while len( need_operation ) > 0:
        if len( need_operation ) > 1 :
            score += double
            nums[ need_operation[ 0 ] ] += 1
            nums[ need_operation[ 1 ] ] += 1
            need_operation = need_operation[ 2: ]
        elif len( need_operation ) == 1:
            score += single
            nums[ need_operation[ 0 ] ] += 1
            need_operation = need_operation[ 2: ]


    return score



# invoke the main without the command itself
if __name__ == '__main__':
    print( task_2( sys.argv[ 1: ] ) )