update
This commit is contained in:
54
day2.c
Normal file
54
day2.c
Normal file
@@ -0,0 +1,54 @@
|
||||
#define DJSTD_BASIC_ENTRY
|
||||
#include "./djstdlib/core.c"
|
||||
|
||||
int djstd_entry(Arena *arena, StringList args) {
|
||||
string input = os_readEntireFile(arena, s("./day2-input"));
|
||||
|
||||
StringList ranges = strSplit(arena, s(","), input);
|
||||
|
||||
int64 result = 0;
|
||||
|
||||
for (EachEl(ranges, string, range)) {
|
||||
if (range->length) {
|
||||
StringList startStop = strSplit(arena, s("-"), *range);
|
||||
if (startStop.length == 2) {
|
||||
Int64Result start = parsePositiveInt64(startStop.data[0]);
|
||||
Int64Result stop = parsePositiveInt64(startStop.data[1]);
|
||||
if (start.valid && stop.valid) {
|
||||
for (int64 i = start.result; i <= stop.result; i++) {
|
||||
Scratch scratch = scratchStart(0, 0);
|
||||
string asStr = strPrintf(scratch.arena, "%zu", i);
|
||||
for (int j = 1; j <= asStr.length/2; j++) {
|
||||
if (asStr.length/j*j == asStr.length) {
|
||||
StringList list = PushList(scratch.arena, StringList, asStr.length/j);
|
||||
for (int k = 0; k < asStr.length/j; k++) {
|
||||
ListAppend(list, strSlice(asStr, j * k, j*k + j));
|
||||
}
|
||||
string firstChunk = list.data[0];
|
||||
bool matched = true;
|
||||
for (EachEl(list, string, chunk)) {
|
||||
if (!strEql(firstChunk, *chunk)) {
|
||||
matched = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (matched) {
|
||||
printStrList(list);
|
||||
result += i;
|
||||
i++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
scratchEnd(scratch);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println("%zu", result);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user