adventofcode2023/10/main.s
2023-12-10 04:45:09 -06:00

232 lines
3.5 KiB
ArmAsm

global _start
[bits 32]
[section .text]
%include "utils.s"
_start:
; find line length
mov edi, file
mov ecx, file.over - file
mov al, 10 ; \n
repne scasb
mov eax, edi
sub eax, file
mov [line_len], eax
call print_dec
call newline
; find S
mov edi, file
mov ecx, file.over - file
mov al, 'S' ; \n
repne scasb
mov eax, edi
sub eax, file
dec eax
mov [s_idx], eax
mov ebx, [line_len]
div ebx
mov [s_line], eax
call print_dec
call space
mov [s_col], edx
mov eax, edx
call print_dec
call newline
mov ecx, (1024*64)/141
mov edi, new_f
fill_new_f:
push ecx
mov al, '.'
mov ecx, 140
rep stosb
mov al, 10
stosb
pop ecx
loop fill_new_f
; walk the loop
mov esi, file
add esi, [s_idx]
mov edx, esi ; last loc
mov ebx, [line_len]
; downs
lea edi, [esi+ebx]
cmp byte [edi], '|'
je walking_loop
cmp byte [edi], 'J'
je walking_loop
cmp byte [edi], 'L'
je walking_loop
; ups
neg ebx
lea edi, [esi+ebx]
cmp byte [edi], '|'
je walking_loop
cmp byte [edi], 'F'
je walking_loop
cmp byte [edi], '7'
je walking_loop
; lefts
lea edi, [esi-1]
cmp byte [edi], '-'
je walking_loop
cmp byte [edi], 'L'
je walking_loop
cmp byte [edi], 'F'
je walking_loop
; rights
lea edi, [esi+1]
cmp byte [edi], '-'
je walking_loop
cmp byte [edi], 'J'
je walking_loop
cmp byte [edi], '7'
je walking_loop
jmp fucked_up
; | - 7 J L F
walking_loop:
pushad
xor edx, edx
mov eax, edi
sub eax, file
mov ebx, [line_len]
div ebx
call print_dec
call space
mov eax, edx
call print_dec
call newline
popad
inc dword [final_value]
mov ebx, edi
sub ebx, file
mov al, [edi]
mov [ebx+new_f], al
cmp byte [edi], 'S'
je done_walking
c_pipe:
cmp byte [edi], '|'
jne c_hyphen
mov eax, edi
add eax, [line_len]
cmp eax, edx ; last loc
je .other_dir
mov edx, edi ; last loc = loc
mov edi, eax
jmp walking_loop
.other_dir:
mov edx, edi ; last loc = loc
sub edi, [line_len]
jmp walking_loop
c_hyphen:
cmp byte [edi], '-'
jne c_7
mov eax, edi
add eax, 1
cmp eax, edx ; last loc
je .other_dir
mov edx, edi ; last loc = loc
mov edi, eax
jmp walking_loop
.other_dir:
mov edx, edi ; last loc = loc
sub edi, 1
jmp walking_loop
c_7:
cmp byte [edi], '7'
jne c_J
mov eax, edi
add eax, [line_len] ; down
cmp eax, edx ; last loc
je .other_dir
mov edx, edi ; last loc = loc
mov edi, eax
jmp walking_loop
.other_dir:
mov edx, edi ; last loc = loc
sub edi, 1 ; left
jmp walking_loop
c_J:
cmp byte [edi], 'J'
jne c_L
mov eax, edi
sub eax, [line_len] ; up
cmp eax, edx ; last loc
je .other_dir
mov edx, edi ; last loc = loc
mov edi, eax
jmp walking_loop
.other_dir:
mov edx, edi ; last loc = loc
sub edi, 1 ; left
jmp walking_loop
c_L:
cmp byte [edi], 'L'
jne c_F
mov eax, edi
sub eax, [line_len] ; up
cmp eax, edx ; last loc
je .other_dir
mov edx, edi ; last loc = loc
mov edi, eax
jmp walking_loop
.other_dir:
mov edx, edi ; last loc = loc
add edi, 1 ; right
jmp walking_loop
c_F:
cmp byte [edi], 'F'
jne fucked_up
mov eax, edi
add eax, [line_len] ; down
cmp eax, edx ; last loc
je .other_dir
mov edx, edi ; last loc = loc
mov edi, eax
jmp walking_loop
.other_dir:
mov edx, edi ; last loc = loc
add edi, 1 ; right
jmp walking_loop
done_walking:
mov eax, [final_value]
inc eax
shr eax, 1
mov [final_value], eax
game_over:
mov eax, [final_value]
call print_dec
call newline
; go get an image editor and fill
; and ready for lots of counting :3
mov esi, new_f
mov ecx, file.over - file
call print_string
call newline
jmp exit
fucked_up:
mov eax, 999999999
call print_dec
jmp exit
[section .data]
line_len: dd 0
s_idx: dd 0
s_line: dd 0
s_col: dd 0
final_value: dd 0
file: incbin "input"
.over:
[section .bss]
new_f: resb 1024*64