adventofcode2023/04/main.s
2023-12-04 01:21:44 -06:00

133 lines
2.4 KiB
ArmAsm

%define BUFF_LIM 32768
%define WINNING_COUNT 10
global _start
[bits 32]
[section .text]
%include "utils.s"
_start:
mov ebx, filename
call open_file
mov ecx, read_buff
mov edx, BUFF_LIM
call read_file
add eax, read_buff
mov [file_lim], eax
mov byte [eax], 10 ; add a newline at the end
; 1. get number, save in id, checking for file_lim
; 2. repeat WINNING_COUNT times
; get the next number
; record in winning list
; end
; 3. repeat until \n
; get the next number
; check against the winning list
; if match then
; increment matches
; end
; end
; 4. add copies + 1 (original) to final value
; 5. tmp := id + 1
; repeat matches times
; copies_list@tmp += instances
; increment tmp
; end
; 6. goto 1
mov esi, read_buff
; 1. get number, save in id, checking for file_lim
find_colon:
call dec_parse
jnc start_round
cmp esi, [file_lim]
jge game_over
jmp find_colon
; 2. repeat WINNING_COUNT times
; get the next number
; record in winning list
; end
start_round:
push eax ; save game id
mov ecx, WINNING_COUNT
.get_next_number:
call dec_parse
jc .get_next_number
; record in list (backward since loop)
mov [(winning_list-4)+ecx*4], eax
loop .get_next_number
; 3. repeat until \n
; get the next number
; check against the winning list
; if match then
; increment matches
; end
; end
check_wins:
xor ebx, ebx ; matches
.get_next_number:
call dec_parse
jc .get_next_number
; check against winning list
mov ecx, WINNING_COUNT
mov edx, 1
.check_winning:
cmp [(winning_list-4)+ecx*4], eax
jne .continue
; matched!
inc ebx
.continue:
loop .check_winning
; check for \n
dec esi
lodsb
cmp al, 10 ; \n
jne .get_next_number
; 4. add copies + 1 (original) to final value
pop edx ; game id
mov eax, [copies_list+edx*4]
inc eax ; copies+1
add [final_value], eax
; if there's no matches, skip 5
test ebx, ebx
jz find_colon ; restart
; 5. tmp := id + 1
; repeat matches times
; copies_list@tmp += instances
; increment tmp
; end
add_copies:
inc edx ; tmp
mov ecx, ebx ; matches
.loop:
add [copies_list+edx*4], eax ; instances
inc edx
loop .loop
; 6. goto 1
jmp find_colon ; restart
; print everything out
game_over:
mov eax, [final_value]
call print_dec
jmp exit
[section .data]
file_lim: dd 0
final_value: dd 0
filename: db "input",0
[section .bss]
winning_list: resd WINNING_COUNT
copies_list: resd 256
read_buff: resb BUFF_LIM