summaryrefslogtreecommitdiff
path: root/2b.c
blob: 640d6e49b524c08039922919ad1029b7a040e9d6 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <string.h>


enum RPS {
	ROCK,
	PAPR,
	SCRS,
};


enum RPS
rps_beats(enum RPS rps)
{
	switch (rps) {
	case ROCK: return SCRS;
	case PAPR: return ROCK;
	case SCRS: return PAPR;
	}
	return -1;
}


enum RPS
rps_loses(enum RPS rps)
{
	switch (rps) {
	case ROCK: return PAPR;
	case PAPR: return SCRS;
	case SCRS: return ROCK;
	}
	return -1;
}


int
rps_pickscore(enum RPS rps)
{
	switch (rps) {
	case ROCK: return 1;
	case PAPR: return 2;
	case SCRS: return 3;
	}
	return 0;
}


int
rps_gamescore_b(enum RPS a, enum RPS b)
{
	if (b == a)
		return 3;
	if (rps_beats(b) == a)
		return 6;
	return 0;
}


int
main()
{
	char buf[BUFSIZ];
	int score = 0;

	while (fgets(buf, sizeof(buf), stdin)) {
		enum RPS a, b;

		if (strlen(buf) != 4)
			break;
		switch (buf[0]) {
			case 'A': a = ROCK; break;
			case 'B': a = PAPR; break;
			case 'C': a = SCRS; break;
			default: return 1;
		}
		switch (buf[2]) {
			case 'X': b = rps_beats(a); break;
			case 'Y': b = a; break;
			case 'Z': b = rps_loses(a); break;
			default: return 1;
		}
		score += rps_pickscore(b) + rps_gamescore_b(a, b);
	}

	printf("Score: %d\n", score);

	return 0;
}