This commit is contained in:
Dobos Ádám
2022-12-05 02:18:30 +01:00
parent 3764808e21
commit 0b308aa771
5 changed files with 2563 additions and 0 deletions

2500
day2/input.txt Normal file
View File

File diff suppressed because it is too large Load Diff

BIN
day2/rockpaperscissors Executable file
View File

Binary file not shown.

30
day2/rockpaperscissors.c Normal file
View File

@@ -0,0 +1,30 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 5
#define score_play(p) (p - 'W')
#define win(o, m) (o == m ? 3 : ((o - m == 1 || o - m == -2) ? 0 : 6))
#define score_game(o, m) (score_play(m) + win((o - 'A'), (m - 'X')))
int main() {
char buf[BUFFER_SIZE], *p, c, opponent, me;
memset(buf, 0, BUFFER_SIZE);
p = buf;
unsigned score = 0;
while ((c = getchar()) != EOF) {
*p++ = c;
if (c == '\n') {
sscanf(buf, "%c %c", &opponent, &me);
score += score_game(opponent, me);
printf("%u\n", score);
memset(buf, 0, BUFFER_SIZE);
p = buf;
}
}
printf("%u\n", score);
}

BIN
day2/rockpaperscissors2 Executable file
View File

Binary file not shown.

33
day2/rockpaperscissors2.c Normal file
View File

@@ -0,0 +1,33 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 5
#define score_play(p) (p - 'W')
#define win(o, m) (o == m ? 3 : ((o - m == 1 || o - m == -2) ? 0 : 6))
#define score_game(o, m) (score_play(m) + win((o - 'A'), (m - 'X')))
#define win_strategy(o) (o - 'A' + 1 > 2 ? 'X' : o - 'A' + 1 + 'X')
#define lose_strategy(o) (o - 'A' - 1 < 0 ? 'Z' : o - 'A' - 1 + 'X')
#define draw_strategy(o) (o - 'A' + 'X')
int main() {
char buf[BUFFER_SIZE], *p, c, opponent, result, me;
memset(buf, 0, BUFFER_SIZE);
p = buf;
unsigned score = 0;
while ((c = getchar()) != EOF) {
*p++ = c;
if (c == '\n') {
sscanf(buf, "%c %c", &opponent, &result);
me = result == 'X' ? lose_strategy(opponent) : (result == 'Y' ? draw_strategy(opponent) : win_strategy(opponent));
score += score_game(opponent, me);
memset(buf, 0, BUFFER_SIZE);
p = buf;
}
}
printf("%u\n", score);
}