mirror of
https://github.com/chylex/Advent-of-Code.git
synced 2025-06-02 06:34:05 +02:00
Add 2015 - Day 1 - Part 1
This commit is contained in:
parent
c79b3c60a1
commit
e6f58c50d2
7
.idea/runConfigurations/AOC_2015_1.xml
Normal file
7
.idea/runConfigurations/AOC_2015_1.xml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="AOC_2015_1" type="CMakeRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" WORKING_DIR="file://$PROJECT_DIR$/2015/01" PASS_PARENT_ENVS_2="true" PROJECT_NAME="AOC" TARGET_NAME="AOC_2015_1" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="AOC" RUN_TARGET_NAME="AOC_2015_1">
|
||||||
|
<method v="2">
|
||||||
|
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
|
</component>
|
9
2015/01/CMakeLists.txt
Normal file
9
2015/01/CMakeLists.txt
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
set(PROJECT_NAME AOC_2015_1)
|
||||||
|
|
||||||
|
project(${PROJECT_NAME} C ASM_NASM)
|
||||||
|
enable_language(ASM_NASM)
|
||||||
|
|
||||||
|
set(CMAKE_NASM_LINK_EXECUTABLE "ld <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
|
||||||
|
set(CAN_USE_ASSEMBLER TRUE)
|
||||||
|
|
||||||
|
add_executable(${PROJECT_NAME} main.asm main.c)
|
1
2015/01/input/1.txt
Normal file
1
2015/01/input/1.txt
Normal file
File diff suppressed because one or more lines are too long
44
2015/01/main.asm
Normal file
44
2015/01/main.asm
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
bits 32
|
||||||
|
|
||||||
|
section .data
|
||||||
|
section .text
|
||||||
|
|
||||||
|
global _walkFloors
|
||||||
|
|
||||||
|
_walkFloors:
|
||||||
|
enter 0,0
|
||||||
|
push ebx
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
.instructionLoop:
|
||||||
|
mov bl, [ edx ] ; bl = instructions[edx]
|
||||||
|
inc edx ; edx++
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
.moveUpFloor:
|
||||||
|
inc eax
|
||||||
|
loop .instructionLoop
|
||||||
|
|
||||||
|
.moveDownFloor:
|
||||||
|
dec eax
|
||||||
|
loop .instructionLoop
|
||||||
|
|
||||||
|
.end:
|
||||||
|
pop ebx
|
||||||
|
leave
|
||||||
|
ret
|
33
2015/01/main.c
Normal file
33
2015/01/main.c
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#include "stdio.h"
|
||||||
|
|
||||||
|
#define BUFFER_SIZE 256
|
||||||
|
|
||||||
|
int walkFloors(int currentFloor, char* instructions, size_t instructionCount);
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
FILE* file;
|
||||||
|
errno_t openErr = fopen_s(&file, "input/1.txt", "rb");
|
||||||
|
if (openErr != 0 || !file) {
|
||||||
|
printf("Error opening input file, code %d\n", openErr);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int floor = 0;
|
||||||
|
|
||||||
|
char buffer[BUFFER_SIZE];
|
||||||
|
while (!feof(file)) {
|
||||||
|
size_t readBytes = fread_s(&buffer, BUFFER_SIZE, 1, BUFFER_SIZE, file);
|
||||||
|
|
||||||
|
int readErr = ferror(file);
|
||||||
|
if (readErr) {
|
||||||
|
printf("Error reading input file, code %d\n", readErr);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
floor = walkFloors(floor, buffer, readBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
|
printf("Final floor: %d", floor);
|
||||||
|
return 0;
|
||||||
|
}
|
5
CMakeLists.txt
Normal file
5
CMakeLists.txt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.20)
|
||||||
|
|
||||||
|
project(AOC)
|
||||||
|
|
||||||
|
add_subdirectory(2015/01)
|
Loading…
Reference in New Issue
Block a user