blob: ccddb3716f37068efb763bc5d0a26d770795cd2f (
plain)
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
|
#include "aoc.h"
#include <stdlib.h>
#include <string>
namespace aoc2015 {
int len(int i) {
int x{0};
while (i > 0) {
i = i / 10;
x++;
}
return x;
}
void itoa(int i, char* s) {
if (i < 10) {
*s = '0' + i;
} else {
int x = i % 10;
*s = '0' + x;
itoa(i / 10, s--);
}
}
void record(const char* p1, const char* p2, std::string& x) {
char c = *p1;
int d = p2 - p1;
int l = len(d);
char* s = (char*)malloc(l + 1);
s[l] = '\0';
itoa(d, s + l - 1);
x.append(s);
x.append(1, c);
free(s);
}
std::string look_and_say(const char* s) {
std::string x;
const char* p1 = s;
const char* p2 = s;
while (*p2 != '\0') {
if (*p2 != *p1) {
record(p1, p2, x);
p1 = p2;
}
p2++;
}
record(p1, p2, x);
return x;
}
size_t day10(const char* s, int repeat) {
std::string x(s);
for (int i = 0; i < repeat; i++) {
// printf("%s\n", x.c_str());
x = look_and_say(x.c_str());
}
return x.size();
}
} // namespace aoc2015
|