-
-
Save zid/eee35eb9317c53ba93e6f61a3939024c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
enum throw | |
{ | |
ROCK, | |
PAPER, | |
SCISSORS | |
}; | |
enum targ | |
{ | |
LOSE, | |
DRAW, | |
WIN | |
}; | |
static int p2(enum throw op, enum targ target) | |
{ | |
switch(op) | |
{ | |
case ROCK: | |
if(target == WIN) | |
return 6 + PAPER + 1; | |
if(target == DRAW) | |
return 3 + ROCK + 1; | |
if(target == LOSE) | |
return 0 + SCISSORS + 1; | |
break; | |
case PAPER: | |
if(target == WIN) | |
return 6 + SCISSORS + 1; | |
if(target == DRAW) | |
return 3 + PAPER + 1; | |
if(target == LOSE) | |
return 0 + ROCK + 1; | |
break; | |
case SCISSORS: | |
if(target == WIN) | |
return 6 + ROCK + 1; | |
if(target == DRAW) | |
return 3 + SCISSORS + 1; | |
if(target == LOSE) | |
return 0 + PAPER + 1; | |
break; | |
} | |
} | |
int main(void) | |
{ | |
FILE *f; | |
char line[1024]; | |
long sum = 0, sum2 = 0; | |
f = fopen("day2.txt", "r"); | |
while(1) | |
{ | |
int o, m, s; | |
if(!fgets(line, 128, f)) | |
break; | |
o = line[0] - 'A'; | |
m = line[2] - 'X'; | |
sum += ((o==m)+(((o^2)-!!(o^2)==m)<<1))*3+m+1; | |
sum2 += p2(o, m); | |
} | |
printf("Final sum: %ld, %ld\n", sum, sum2); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment