hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/aoc-2025/day3.asm
diff options
context:
space:
mode:
Diffstat (limited to 'aoc-2025/day3.asm')
-rw-r--r--aoc-2025/day3.asm146
1 files changed, 73 insertions, 73 deletions
diff --git a/aoc-2025/day3.asm b/aoc-2025/day3.asm
index e547475..c8b8b75 100644
--- a/aoc-2025/day3.asm
+++ b/aoc-2025/day3.asm
@@ -15,7 +15,7 @@ global _start
%define STDOUT 1
section .data
- inputPath db "input/day3.txt", 0
+ input_path db "input/day3.txt", 0
newline db 10
@@ -24,28 +24,28 @@ section .data
section .bss
buffer resb 4096
- lineBuffer resb 65536
- lineLen resd 1
+ line_buffer resb 65536
+ line_len resd 1
sum1 resq 1
sum2 resq 1
- workDigits resb 64 ; stack for greedy subsequence
- numberBuffer resb 32 ; for printing
+ work_digits resb 64 ; stack for greedy subsequence
+ number_buffer resb 32 ; for printing
section .text
_start:
mov rax, SYS_OPEN
- mov rdi, inputPath
+ mov rdi, input_path
mov rsi, O_RDONLY
xor rdx, rdx
syscall
- mov r12, rax ; fd
- mov dword [lineLen], 0
+ mov r12, rax ; fd
+ mov dword [line_len], 0
-ReadLoop:
+read_loop:
mov rax, SYS_READ
mov rdi, r12
mov rsi, buffer
@@ -53,80 +53,80 @@ ReadLoop:
syscall
cmp rax, 0
- jle EOF
+ jle eof
mov rdi, buffer
mov rbp, rax
-ScanChunk:
+scan_chunk:
cmp rbp, 0
- je ReadLoop
+ je read_loop
mov al, [rdi]
cmp al, 13
- je SkipCR
+ je skip_cr
cmp al, 10
- je EndLine
+ je end_line
- mov edx, [lineLen]
+ mov edx, [line_len]
cmp edx, 65535
- jae SkipStore
- mov [lineBuffer+rdx], al
+ jae skip_store
+ mov [line_buffer+rdx], al
inc edx
- mov [lineLen], edx
+ mov [line_len], edx
-SkipStore:
+skip_store:
inc rdi
dec rbp
- jmp ScanChunk
+ jmp scan_chunk
-SkipCR:
+skip_cr:
inc rdi
dec rbp
- jmp ScanChunk
+ jmp scan_chunk
-EndLine:
- mov edx, [lineLen]
- mov byte [lineBuffer+edx], 0
+end_line:
+ mov edx, [line_len]
+ mov byte [line_buffer+edx], 0
cmp edx, 0
- je ClearLine
+ je clear_line
push rdi
- mov rsi, lineBuffer
- call HandleLine
+ mov rsi, line_buffer
+ call handle_line
pop rdi
-ClearLine:
- mov dword [lineLen], 0
+clear_line:
+ mov dword [line_len], 0
inc rdi
dec rbp
- jmp ScanChunk
+ jmp scan_chunk
-EOF:
+eof:
; possible last partial line
- mov edx, [lineLen]
+ mov edx, [line_len]
cmp edx, 0
- jle PrintResults
+ jle print_results
- mov byte [lineBuffer+edx], 0
- mov rsi, lineBuffer
- call HandleLine
+ mov byte [line_buffer+edx], 0
+ mov rsi, line_buffer
+ call handle_line
-PrintResults:
+print_results:
mov rcx, label1
- call PrintString
+ call print_string
mov rax, [sum1]
- call PrintInt64
- call Newline
+ call print_int64
+ call print_newline
mov rcx, label2
- call PrintString
+ call print_string
mov rax, [sum2]
- call PrintInt64
- call Newline
+ call print_int64
+ call print_newline
mov rax, SYS_CLOSE
mov rdi, r12
@@ -136,34 +136,34 @@ PrintResults:
xor rdi, rdi
syscall
-HandleLine:
+handle_line:
mov rdi, rsi
- xor ecx, ecx ; ECX = 0
-.lengthLoop:
+ xor ecx, ecx ; ECX = 0
+.length_loop:
mov al, [rdi+rcx]
cmp al, 0
- je .lengthDone
+ je .length_done
inc ecx
- jmp .lengthLoop
-.lengthDone:
- mov r15d, ecx ; r15d = n
+ jmp .length_loop
+.length_done:
+ mov r15d, ecx ; r15d = n
; part 1: k = 2
- mov ecx, r15d ; ECX = n
- mov edi, 2 ; K = 2
- call GetMaxForK ; RAX = best 2-digit value
+ mov ecx, r15d ; ECX = n
+ mov edi, 2 ; K = 2
+ call get_max_for_k ; RAX = best 2-digit value
add qword [sum1], rax
; part 2: k = 12, only if n >= 12
cmp r15d, 12
- jl .skipPart2
+ jl .skip_part2
mov ecx, r15d
mov edi, 12
- call GetMaxForK ; RAX = best 12-digit value
+ call get_max_for_k ; RAX = best 12-digit value
add qword [sum2], rax
-.skipPart2:
+.skip_part2:
ret
; Greedy lexicographically largest subsequence of length K
@@ -175,14 +175,14 @@ HandleLine:
; Output:
; RAX = numeric value of chosen k digits
; Clobbers: R8..R11, EAX, EDX
-GetMaxForK:
- ; dropsRemaining = n - k
+get_max_for_k:
+ ; drops_remaining = n - k
mov eax, ecx
sub eax, edi
- mov r8d, eax ; r8d = drops
+ mov r8d, eax ; r8d = drops
- xor r9d, r9d ; r9d = stackSize
- xor r10d, r10d ; r10d = i
+ xor r9d, r9d ; r9d = stack_size
+ xor r10d, r10d ; r10d = i
.loop:
cmp r10d, ecx
@@ -201,7 +201,7 @@ GetMaxForK:
mov edx, r9d
dec edx
mov r11d, edx
- mov bl, [workDigits + r11]
+ mov bl, [work_digits + r11]
cmp bl, al
jge .push
@@ -211,13 +211,13 @@ GetMaxForK:
jmp .pop
.push:
mov r11d, r9d
- mov [workDigits + r11], al
+ mov [work_digits + r11], al
inc r9d
inc r10d
jmp .loop
.after_input:
- ; trim if stackSize > k
+ ; trim if stack_size > k
.trim:
cmp r9d, edi
jle .convert
@@ -226,12 +226,12 @@ GetMaxForK:
.convert:
xor rax, rax
xor r11d, r11d
-.convertLoop:
+.convert_loop:
cmp r11d, edi
jge .done
mov rdx, r11
- mov dl, [workDigits + rdx]
+ mov dl, [work_digits + rdx]
sub dl, '0'
movzx rdx, dl
@@ -239,11 +239,11 @@ GetMaxForK:
add rax, rdx
inc r11d
- jmp .convertLoop
+ jmp .convert_loop
.done:
ret
-Newline:
+print_newline:
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, newline
@@ -251,8 +251,8 @@ Newline:
syscall
ret
-PrintInt64:
- mov rdi, numberBuffer+31
+print_int64:
+ mov rdi, number_buffer+31
mov byte [rdi], 0
dec rdi
@@ -277,12 +277,12 @@ PrintInt64:
mov rax, SYS_WRITE
mov rsi, rdi
mov rdi, STDOUT
- mov rdx, numberBuffer+31
+ mov rdx, number_buffer+31
sub rdx, rsi
syscall
ret
-PrintString:
+print_string:
push rcx
push rdx
mov rdx, 0