-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc22.c
175 lines (162 loc) · 4.55 KB
/
aoc22.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <ctype.h>
typedef struct {
int data[60];
int dataCount;
} buf_t;
typedef struct {
buf_t player[2];
uint64_t combination[4000]; // player 1 lower 16 Bit player 2 upper 16 bit
int combinationCount;
int won;
int game, round;
} game_t;
void bufInit(buf_t *r) {
r->dataCount = 0;
}
int takeFromBegin(buf_t *r) {
if (r->dataCount == 0) {
printf("empty buf\n");
exit(1);
}
int result = r->data[0];
r->dataCount--;
int *to = &r->data[0], *from = &r->data[1];
for (int i = r->dataCount; i > 0; i--) {
*to++ = *from++;
}
return result;
}
void addToEnd(buf_t *r, int add) {
if (r->dataCount >= (sizeof r->data / sizeof r->data[0])) {
printf("buf to small\n");
exit(1);
}
r->data[r->dataCount++] = add;
}
int bufLen(buf_t *r) {
return r->dataCount;
}
void bufPrint(buf_t *r, int player) {
printf("Player %d: ", player + 1);
buf_t *rp = &r[player];
for (int i = 0; i < rp->dataCount; i++) {
printf("%d, ", rp->data[i]);
}
puts("\b\b ");
}
int getGameResult(buf_t *r) {
int j = 1;
int sum = 0;
for (int i = r->dataCount - 1; i >= 0; i--) {
sum += j++ * r->data[i];
}
return sum;
}
void play1(game_t *g) {
g->won = g->player[1].dataCount == 0 ? 1 :
g->player[0].dataCount == 0 ? 2 : 0;
if (g->won > 0) {
// printf("Player %d wins\n", g->won);
return;
}
int p1 = takeFromBegin(&g->player[0]), p2 = takeFromBegin(&g->player[1]);
if (p1 < p2) {
addToEnd(&g->player[1], p2);
addToEnd(&g->player[1], p1);
} else {
addToEnd(&g->player[0], p1);
addToEnd(&g->player[0], p2);
}
// bufPrint(&g->player[0], 0);
// bufPrint(&g->player[0], 1);
play1(g);
}
bool addTurnWasBefore(game_t *g) { // a hash map would be nicer here
uint64_t combined = getGameResult(&g->player[0]) + 100000 * getGameResult(&g->player[1]);
for (int i = 0; i < g->combinationCount; i++) {
if (g->combination[i] == combined) {
return true;
}
}
g->combination[g->combinationCount++] = combined;
if (g->combinationCount > sizeof g->combination / sizeof g->combination[0]) {
puts("combination array too small");
exit(0);
}
return false;
}
void play2(game_t *g) {
int len1 = bufLen(&g->player[0]), len2 = bufLen(&g->player[1]);
g->won = len2 == 0 ? 1 :
len1 == 0 ? 2 : 0;
if (addTurnWasBefore(g)) {
g->won = 1;
}
if (g->won > 0) {
//printf("Player %d wins\n", g->won);
//printGameResult(&g->player[g->won - 1]);
return;
}
int p1 = takeFromBegin(&g->player[0]), p2 = takeFromBegin(&g->player[1]);
bool player2Wins = p1 < p2;
if (len1 > p1 && len2 > p2) { // len is includuing p1 and p2
game_t subGame = *g;
subGame.game++;
subGame.round = 0;
subGame.player[0].dataCount = p1;
subGame.player[1].dataCount = p2;
subGame.combinationCount = 0;
// printf(">> Game %d <<\n", subGame.game);
play2(&subGame);
player2Wins = (subGame.won == 2);
// printf(">> END Game <<\n");
}
if (player2Wins) {
addToEnd(&g->player[1], p2);
addToEnd(&g->player[1], p1);
} else {
addToEnd(&g->player[0], p1);
addToEnd(&g->player[0], p2);
}
g->round++;
// printf("round %d game %d combinations %d\n", g->round, g->game, g->combinationCount);
// bufPrint(&g->player[0], 0);
// bufPrint(&g->player[0], 1);
play2(g);
}
int main(void) {
FILE* f = fopen("input22.txt", "r");
if (f != NULL) {
char buf[256];
int part = 0;
game_t game;
bufInit(&game.player[0]);
bufInit(&game.player[1]);
while (fgets(buf, sizeof buf, f) != NULL) {
if (*buf != 0 && *buf != 10) {
if (isdigit(*buf)) {
addToEnd(&game.player[part], strtol(buf, 0, 0));
}
} else {
part++;
if (part == 2) {
return 1;
}
}
}
// bufPrint(game.player, 0);
// bufPrint(game.player, 1);
game_t game2 = game;
play1(&game);
printf("Part 1: %d\n", getGameResult(&game.player[game.won - 1]));
play2(&game2);
printf("Part 2: %d\n", getGameResult(&game2.player[game2.won - 1]));
fclose(f);
}
return 0;
}