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

Add 2015 - Day 2 - Part 1

This commit is contained in:
chylex 2022-02-11 07:23:13 +01:00
parent 4ccd7125e8
commit c7433b0a82
Signed by: chylex
GPG Key ID: 4DE42C8F19A80548
6 changed files with 1127 additions and 0 deletions

View File

@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="2015 - Day 02" type="CMakeRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" WORKING_DIR="file://$PROJECT_DIR$/2015/02" PASS_PARENT_ENVS_2="true" PROJECT_NAME="AOC" TARGET_NAME="AOC_2015_02" CONFIG_NAME="Debug x64" RUN_TARGET_PROJECT_NAME="AOC" RUN_TARGET_NAME="AOC_2015_02">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
</component>

5
2015/02/CMakeLists.txt Normal file
View File

@ -0,0 +1,5 @@
set(PROJECT_NAME AOC_2015_02)
project(${PROJECT_NAME} C ASM_NASM)
add_executable(${PROJECT_NAME} main.asm ../utils/main.c ../utils/file.h ../utils/benchmark.h)

1000
2015/02/input/1.txt Normal file

File diff suppressed because it is too large Load Diff

113
2015/02/main.asm Normal file
View File

@ -0,0 +1,113 @@
bits 64
default rel
section .text
extern print
global entryPoint
entryPoint:
push rbp
mov rbp, rsp
push rbx
push r12
sub rsp, 32
; rax = reserved for math
; rdx = reserved for math
; rbx = value of current character of input string
; rcx = pointer to current character of input string
; r8 = return value for nextNumber
; r9 = first dimension
; r10 = second dimension
; r11 = third dimension
; r12 = total wrapping paper required
xor rdx, rdx
xor r12, r12
.nextLine:
call .nextNumber
mov r9, r8 ; save first dimension to r9
call .nextNumber
mov r10, r8 ; save second dimension to r10
call .nextNumber
mov r11, r8 ; save third dimension to r11
.sort1: ; initiate bubble sort of the three dimensions (possible configurations 123; 132; 213; 231; 312; 321)
cmp r9, r10 ; compare first pair of dimensions
jb .sort2 ; if they are already sorted, skip ahead (123; 132; 231)
xchg r9, r10 ; otherwise, swap the first pair (213->123; 312->132; 321->231)
.sort2:
cmp r10, r11 ; compare last pair of dimensions
jb .sort3 ; if they are already sorted, skip ahead (123)
xchg r10, r11 ; otherwise, swap the last pair (132->123; 231->213)
.sort3:
cmp r9, r10 ; compare first pair of dimensions again in case the smallest number started at the end and was moved to second position
jb .sortEnd ; if they are already sorted, skip ahead (123)
xchg r9, r10 ; otherwise, swap the first pair (213->123)
.sortEnd: ; at this point, the dimensions are sorted from smallest to largest in registers r9, r10, r11
mov rax, 2
mul r9
mul r10
add r12, rax ; add 2 * dim1 * dim2
mov rax, 2
mul r9
mul r11
add r12, rax ; add 2 * dim1 * dim3
mov rax, 2
mul r10
mul r11
add r12, rax ; add 2 * dim2 * dim3
mov rax, r9
mul r10
add r12, rax ; add square area of smallest side
movzx rbx, byte [ rcx ] ; read current character
cmp bl, 0 ; if we have not reached end of string yet,
jne .nextLine ; go read the next line
lea rcx, [ print_wrapping_paper ]
mov rdx, r12
call print
add rsp, 32
pop r12
pop rbx
leave
ret
.nextNumber:
xor r8, r8 ; reset return value to zero
.nextDigit:
movzx rbx, byte [ rcx ] ; read current character
inc rcx ; move to next character
cmp bl, '0'
jb .nonDigitCharacter
cmp bl, '9'
ja .nonDigitCharacter
mov rax, 10
mul r8 ; shift digit accumulator by one decimal place
mov r8, rax ; move multiplied result back to digit accumulator
sub rbx, '0' ; convert ASCII to digit
add r8, rbx ; add digit to accumulator
jmp .nextDigit
.nonDigitCharacter:
ret ; return from nextNumber calls
section .data
print_wrapping_paper: db `Wrapping paper required: %d\n`, 0

View File

@ -9,3 +9,4 @@ set(CMAKE_NASM_LINK_EXECUTABLE "ld <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBR
set(CAN_USE_ASSEMBLER TRUE)
add_subdirectory(01)
add_subdirectory(02)

View File

@ -32,6 +32,7 @@ The versions should not matter, but I used Visual Studio 2019 with `MSVC v142 (1
| Year | Day | Language |
|-----:|----:|----------|
| 2015 | 01 | NASM x86 |
| 2015 | 02 | NASM x64 |
| 2021 | 01 | Kotlin |
| 2021 | 02 | Kotlin |
| 2021 | 03 | Kotlin |