This commit is contained in:
Daniel Ledda
2025-12-25 14:05:36 +01:00
commit a67e150d0f
12 changed files with 4913 additions and 0 deletions

60
day1.c Normal file
View File

@@ -0,0 +1,60 @@
#define DJSTD_BASIC_ENTRY
#include "./djstdlib/core.c"
int djstd_entry(Arena *arena, StringList args) {
string input = os_readEntireFile(arena, s("./day1-input"));
int64 dial = 50;
int64 lineNo = 0;
StringList split = strSplit(arena, s("\n"), input);
int32 password = 0;
for (EachEl(split, string, line)) {
if (!strEql(*line, s(""))) {
bool lineValid = false;
if (line->length > 1) {
char dir = line->str[0];
Int32Result numStrParsed = parsePositiveInt32(strChopStart(*line, 1));
if (numStrParsed.valid) {
lineValid = true;
int32 dialPrev = dial;
int32 pwPrev = password;
int32 turns = (+numStrParsed.result)/100;
password += turns;
int32 remainder = numStrParsed.result - (numStrParsed.result/100)*100;
if (dir == 'L') {
dial -= remainder;
} else if (dir == 'R') {
dial += remainder;
}
if (dialPrev != 0 && (dial >= 100 || dial <= 0)) {
password += 1;
}
println("turn: %S, %d => %d, %d", *line, dialPrev, dial, password - pwPrev);
dial %= 100;
if (dial < 0) {
dial += 100;
}
}
}
if (!lineValid) {
println("Input corrupted at line %d", lineNo);
return 1;
}
}
lineNo++;
}
println("%d", password);
return 0;
}