aboutsummaryrefslogtreecommitdiff
path: root/challenge-104/paulo-custodio/awk/ch-2.awk
blob: 05ef1a56c13f60270a2af79c12d71400b3570b30 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/gawk

# Challenge 104
#
# TASK #2 � NIM Game
# Submitted by: Mohammad S Anwar
# Write a script to simulate the NIM Game.
#
# It is played between 2 players. For the purpose of this task, let assume you
# play against the machine.
#
# There are 3 simple rules to follow:
#
# a) You have 12 tokens
# b) Each player can pick 1, 2 or 3 tokens at a time
# c) The player who picks the last token wins the game

# A plays to win, B plays randomly, A wins 91% of the time
# If both play to win, the second player to play always wins

function play_to_win(T) {
    if (T <= 3)
        return T
    else if ((T % 4)==0)
        return 1
    else if ((T % 5)==0)
        return 1
    else if ((T % 6)==0)
        return 2
    else if ((T % 7)==0)
        return 3
    else if ((T % 9)==0)
        return 2
    else if ((T % 11)==0)
        return 2
    else
        exit 1
}

function rand_play(T) {
    return int(3*rand())+1;
}

BEGIN {
    matches = 100000
    play = ARGV[1]
    for (i = 0; i < matches; i++) {
        T = 12
        while (T > 0) {
            # player A
            move = play_to_win(T)
            T -= move
            if (T <= 0) {
                winsA++
                break
            }
            # player B
            if (play == "random")
                move = rand_play(T)
            else
                move = play_to_win(T)
            T -= move
            if (T <= 0) {
                winsB++
                break
            }
        }
    }

    a_wins = int((winsA)/(winsA+winsB)*100)
    print "A wins " a_wins "% of the matches."

    exit 0
}