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
|
#include <stdio.h>
#include <stdlib.h>
#if 0
#define PART part1
#else
#define PART part2
#endif
#if 0
#define FILENAME "sample.txt"
#else
#define FILENAME "input.txt"
#endif
// outcome[opponent][player]
int outcome[3][3] = {
/* player rock paper scissors*/
/* rock */ { 1, 2, 0 },
/* paper */ { 0, 1, 2 },
/* scissors */ { 2, 0, 1 },
};
// player[opponent][player]
int player[3][3] = {
/* lose draw win */
/* rock */ { 2, 0, 1 },
/* paper */ { 0, 1, 2 },
/* scissors */ { 1, 2, 0 },
};
int score = 0;
void part1(char *line)
{
int opponent = line[0] - 'A';
int player = line[2] - 'X';
score += (player + 1) + (outcome[opponent][player] * 3);
}
void part2(char *line)
{
int opponent = line[0] - 'A';
int outcome = line[2] - 'X';
score += (player[opponent][outcome] + 1) + (outcome * 3);
}
void parse()
{
FILE *fp = fopen(FILENAME, "r");
if(!fp) {
fprintf(stderr, "ERROR: Could not open file: %s\n", FILENAME);
exit(1);
}
char line[8];
while(fgets(line, sizeof(line), fp))
PART(line);
fclose(fp);
}
int main(void)
{
parse();
printf("%d\n", score);
return 0;
}
|