adventofcode2023/04/main.s

133 lines
2.4 KiB
ArmAsm
Raw Permalink Normal View History

2023-12-03 23:55:43 -06:00
%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
2023-12-04 00:46:30 -06:00
; 1. get number, save in id, checking for file_lim
2023-12-03 23:55:43 -06:00
; 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
2023-12-04 00:46:30 -06:00
; increment matches
2023-12-03 23:55:43 -06:00
; end
; end
2023-12-04 00:46:30 -06:00
; 4. add copies + 1 (original) to final value
2023-12-04 01:21:44 -06:00
; 5. tmp := id + 1
; repeat matches times
; copies_list@tmp += instances
; increment tmp
2023-12-04 00:46:30 -06:00
; end
; 6. goto 1
2023-12-03 23:55:43 -06:00
mov esi, read_buff
2023-12-04 00:46:30 -06:00
; 1. get number, save in id, checking for file_lim
2023-12-03 23:55:43 -06:00
find_colon:
2023-12-04 00:46:30 -06:00
call dec_parse
jnc start_round
2023-12-03 23:55:43 -06:00
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:
2023-12-04 00:46:30 -06:00
push eax ; save game id
2023-12-03 23:55:43 -06:00
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
2023-12-04 00:46:30 -06:00
; increment matches
2023-12-03 23:55:43 -06:00
; end
; end
check_wins:
2023-12-04 00:46:30 -06:00
xor ebx, ebx ; matches
2023-12-03 23:55:43 -06:00
.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!
2023-12-04 00:46:30 -06:00
inc ebx
2023-12-03 23:55:43 -06:00
.continue:
loop .check_winning
; check for \n
dec esi
lodsb
cmp al, 10 ; \n
jne .get_next_number
2023-12-04 00:46:30 -06:00
; 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
2023-12-03 23:55:43 -06:00
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
2023-12-04 00:46:30 -06:00
copies_list: resd 256
2023-12-03 23:55:43 -06:00
read_buff: resb BUFF_LIM