mirror of
https://github.com/chylex/Advent-of-Code.git
synced 2025-06-19 20:39:54 +02:00
Add 2015 - Day 1 - Part 2
This commit is contained in:
parent
e6f58c50d2
commit
7f8aeedd21
@ -8,37 +8,60 @@ global _walkFloors
|
|||||||
_walkFloors:
|
_walkFloors:
|
||||||
enter 0,0
|
enter 0,0
|
||||||
push ebx
|
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 eax, [ ebp + 8 ] ; eax = currentFloor
|
||||||
mov edx, [ ebp + 12 ] ; edx = char* instructions
|
mov edx, [ ebp + 20 ] ; edx = instructions
|
||||||
mov ecx, [ ebp + 16 ] ; ecx = size_t instructionCount
|
xor ecx, ecx ; ecx = 0
|
||||||
|
|
||||||
jcxz .end ; skip to end if there are no instructions
|
|
||||||
|
|
||||||
inc ecx ; increment instruction count by 1
|
|
||||||
; because loop instruction uses prefix decrement
|
|
||||||
|
|
||||||
.instructionLoop:
|
.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]
|
mov bl, [ edx ] ; bl = instructions[edx]
|
||||||
inc edx ; edx++
|
inc edx ; edx++
|
||||||
|
|
||||||
cmp bl, '(' ; left parenthesis...
|
cmp bl, '(' ; left parenthesis...
|
||||||
je .moveUpFloor ; moves up a floor
|
je .moveUpFloor ; moves up a floor
|
||||||
|
|
||||||
cmp bl, ')' ; right parenthesis...
|
cmp bl, ')' ; right parenthesis...
|
||||||
je .moveDownFloor ; moves down a floor
|
je .moveDownFloor ; moves down a floor
|
||||||
|
jmp .end ; unknown character skips to the end
|
||||||
jmp .end ; unknown character skips to the end
|
|
||||||
|
|
||||||
.moveUpFloor:
|
.moveUpFloor:
|
||||||
inc eax
|
inc eax
|
||||||
loop .instructionLoop
|
jmp .instructionLoop
|
||||||
|
|
||||||
.moveDownFloor:
|
.moveDownFloor:
|
||||||
dec eax
|
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:
|
.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
|
pop ebx
|
||||||
leave
|
leave
|
||||||
ret
|
ret
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
#define BUFFER_SIZE 256
|
#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() {
|
int main() {
|
||||||
FILE* file;
|
FILE* file;
|
||||||
@ -13,6 +13,8 @@ int main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int floor = 0;
|
int floor = 0;
|
||||||
|
int totalInstructionCounter = 0;
|
||||||
|
int firstEnteredBasement = -1;
|
||||||
|
|
||||||
char buffer[BUFFER_SIZE];
|
char buffer[BUFFER_SIZE];
|
||||||
while (!feof(file)) {
|
while (!feof(file)) {
|
||||||
@ -24,10 +26,11 @@ int main() {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
floor = walkFloors(floor, buffer, readBytes);
|
floor = walkFloors(floor, &totalInstructionCounter, &firstEnteredBasement, buffer, readBytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
printf("Final floor: %d", floor);
|
printf("Final floor: %d\n", floor);
|
||||||
|
printf("First entered basement: %d\n", firstEnteredBasement);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user