update
This commit is contained in:
60
day1.c
Normal file
60
day1.c
Normal 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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user