1
0
mirror of https://github.com/chylex/Advent-of-Code.git synced 2025-05-15 07:34:05 +02:00

Add 2015 - Day 1 - Part 2

This commit is contained in:
chylex 2021-11-30 05:20:17 +01:00
parent e6f58c50d2
commit 7f8aeedd21
Signed by: chylex
GPG Key ID: 4DE42C8F19A80548
2 changed files with 43 additions and 17 deletions

View File

@ -8,37 +8,60 @@ global _walkFloors
_walkFloors:
enter 0,0
push ebx
push edi
; [ ebp + 8 ] = int currentFloor
; [ ebp + 12 ] = int* totalInstructionCounter
; [ ebp + 16 ] = int* firstEnteredBasement
; [ ebp + 20 ] = char* instructions
; [ ebp + 24 ] = size_t instructionCount
mov eax, [ ebp + 8 ] ; eax = currentFloor
mov edx, [ ebp + 12 ] ; edx = char* instructions
mov ecx, [ ebp + 16 ] ; ecx = size_t instructionCount
jcxz .end ; skip to end if there are no instructions
inc ecx ; increment instruction count by 1
; because loop instruction uses prefix decrement
mov edx, [ ebp + 20 ] ; edx = instructions
xor ecx, ecx ; ecx = 0
.instructionLoop:
cmp ecx, [ ebp + 24 ] ; check if we went past instructionCount
jge .end ; if so, we are done here
inc ecx ; otherwise, increment instruction counter and continue
mov bl, [ edx ] ; bl = instructions[edx]
inc edx ; edx++
cmp bl, '(' ; left parenthesis...
je .moveUpFloor ; moves up a floor
cmp bl, '(' ; left parenthesis...
je .moveUpFloor ; moves up a floor
cmp bl, ')' ; right parenthesis...
je .moveDownFloor ; moves down a floor
jmp .end ; unknown character skips to the end
jmp .end ; unknown character skips to the end
.moveUpFloor:
inc eax
loop .instructionLoop
jmp .instructionLoop
.moveDownFloor:
dec eax
loop .instructionLoop
cmp eax, -1 ; check if we entered the basement
je .onEnteredBasement
jmp .instructionLoop
.onEnteredBasement:
mov ebx, [ ebp + 16 ] ; ebx = &firstEnteredBasement
cmp dword [ ebx ], -1 ; check if firstEnteredBasement has not been set yet
jne .instructionLoop ; if it was already set, go to next instruction
mov edi, [ ebp + 12 ] ; edi = &totalInstructionCounter
mov edi, [ edi ] ; edi = totalInstructionCounter
add edi, ecx ; add current instruction counter to the total
mov dword [ ebx ], edi ; set firstEnteredBasement to the total
jmp .instructionLoop
.end:
mov edi, [ ebp + 12 ] ; edi = &totalInstructionCounter
mov ebx, [ edi ] ; ebx = totalInstructionCounter
add ebx, ecx ; add current instruction counter to the total
mov dword [ edi ], ebx ; set totalInstructionCounter to the new total
pop edi
pop ebx
leave
ret

View File

@ -2,7 +2,7 @@
#define BUFFER_SIZE 256
int walkFloors(int currentFloor, char* instructions, size_t instructionCount);
int walkFloors(int currentFloor, int* totalInstructionCounter, int* firstEnteredBasement, char* instructions, size_t instructionCount);
int main() {
FILE* file;
@ -13,6 +13,8 @@ int main() {
}
int floor = 0;
int totalInstructionCounter = 0;
int firstEnteredBasement = -1;
char buffer[BUFFER_SIZE];
while (!feof(file)) {
@ -24,10 +26,11 @@ int main() {
return 1;
}
floor = walkFloors(floor, buffer, readBytes);
floor = walkFloors(floor, &totalInstructionCounter, &firstEnteredBasement, buffer, readBytes);
}
fclose(file);
printf("Final floor: %d", floor);
printf("Final floor: %d\n", floor);
printf("First entered basement: %d\n", firstEnteredBasement);
return 0;
}