-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday22a.c
66 lines (59 loc) · 1.57 KB
/
day22a.c
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
#include "common.h"
#include "deque.h"
int main (int argc, char ** argv) {
Deque_t player1;
Deque_t player2;
deque_init(&player1, 64);
deque_init(&player2, 64);
char buf [BUFSIZ];
char * s = fgets(&(buf[0]), sizeof(buf), stdin);
ASSERT(!strcmp(s, "Player 1:\n"));
do {
s = fgets(&(buf[0]), sizeof(buf), stdin);
ASSERT(s);
if (!strcmp(s, "\n")) {
break;
}
long value = strtol(s, NULL, 10);
deque_push(&player1, value);
} while (1);
s = fgets(&(buf[0]), sizeof(buf), stdin);
ASSERT(!strcmp(s, "Player 2:\n"));
do {
s = fgets(&(buf[0]), sizeof(buf), stdin);
if (!s) {
break;
}
if (!strcmp(s, "\n")) {
break;
}
long value = strtol(s, NULL, 10);
deque_push(&player2, value);
} while (1);
do {
intptr_t v1 = deque_pop(&player1);
intptr_t v2 = deque_pop(&player2);
if (v1 > v2) {
deque_push(&player1, v1);
deque_push(&player1, v2);
} else {
deque_push(&player2, v2);
deque_push(&player2, v1);
}
} while (player1.count && player2.count);
Deque_t * winner = NULL;
if (player1.count) {
winner = &(player1);
} else {
ASSERT(player2.count);
winner = &(player2);
}
intptr_t multiplier = winner->count;
intptr_t score = 0;
do {
intptr_t value = deque_pop(winner);
score += value * multiplier;
--multiplier;
} while (winner->count);
DISP(score);
}