From 563076a899a2e96128a4ead52dae3d2d0151f9a3 Mon Sep 17 00:00:00 2001 From: Lucia Ceionia Date: Sat, 2 Dec 2023 20:41:34 -0600 Subject: [PATCH] day 2 part 2 --- 02/input_test2 | 5 ++ 02/main.s | 45 ++++-------- 02/main_part1.s | 177 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 197 insertions(+), 30 deletions(-) create mode 100644 02/input_test2 create mode 100644 02/main_part1.s diff --git a/02/input_test2 b/02/input_test2 new file mode 100644 index 0000000..295c36d --- /dev/null +++ b/02/input_test2 @@ -0,0 +1,5 @@ +Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green +Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue +Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red +Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red +Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green diff --git a/02/main.s b/02/main.s index 79d521e..2b331a4 100644 --- a/02/main.s +++ b/02/main.s @@ -35,9 +35,11 @@ mov esi, read_buff call dec_parse test eax, eax jz .get_id -push eax ; id +; we don't need to save the id ; get another number lol -xor ebx, ebx ; counts for the game ??RRGGBB +xor ebx, ebx ; B +xor edi, edi ; G +xor ebp, ebp ; R .get_num: call dec_parse test eax, eax @@ -50,15 +52,16 @@ je .r_chk cmp al, 'g' je .g_chk .b_chk: -or ebx, ecx ; no shift +cmp ecx, ebx +cmovg ebx, ecx jmp .next .g_chk: -shl ecx, 8 -or ebx, ecx +cmp ecx, edi +cmovg edi, ecx jmp .next .r_chk: -shl ecx, 16 -or ebx, ecx +cmp ecx, ebp +cmovg ebp, ecx .next: xor edx, edx ; game over flag ; find one of , ; \n @@ -66,36 +69,18 @@ lodsb cmp al, ',' je .get_num ; next number cmp al, ';' -je .check_poss +je .get_num ; we don't care about rounds cmp al, 10 ; \n je .new_game cmp esi, dword [file_lim] ; end of file jge .new_game jmp .next ; other char .new_game: -mov edx, 1 ; game over flag -.check_poss: -mov eax, limits -.check_start: -cmp bl, al -jle .next_check -mov dword [esp], 0 ; fail check, game is null -jmp .round_done ; no need to check the other limits -.next_check: -shr ebx, 8 -shr eax, 8 -test ebx, ebx -jnz .check_start -.round_done: -; check for game end -test edx, edx -jnz .game_done -; reset counts -xor ebx, ebx -jmp .get_num ; new round +mov eax, ebx ; B +mul edi ; G +mul ebp ; R .game_done: -; add id to final value (impossible game is 0) -pop eax +; add power to final value add dword [final_value], eax ; checked earlier but... meh cmp esi, dword [file_lim] ; end of file diff --git a/02/main_part1.s b/02/main_part1.s new file mode 100644 index 0000000..79d521e --- /dev/null +++ b/02/main_part1.s @@ -0,0 +1,177 @@ +%define BUFF_LIM 32768 + +%define red_lim 12 +%define green_lim 13 +%define blue_lim 14 +%define limits ((red_lim << 16) | (green_lim << 8) | blue_lim) + +; call # val val2 +; int $0x80 eax eax edx - +; +; arg1 arg2 arg3 arg4 arg5 arg6 arg7 +; ebx ecx edx esi edi ebp - +global _start +[bits 32] +[section .text] +_start: +push 0 ; eof +mov eax, 5 ; open +mov ebx, filename +xor ecx, ecx ; read only +int 0x80 +mov ebx, eax +mov eax, 3 ; read +mov ecx, read_buff +mov edx, BUFF_LIM +int 0x80 +test eax,eax +js exit +add eax, read_buff +mov [file_lim], eax + +mov esi, read_buff +; just try until number lol +.get_id: +call dec_parse +test eax, eax +jz .get_id +push eax ; id +; get another number lol +xor ebx, ebx ; counts for the game ??RRGGBB +.get_num: +call dec_parse +test eax, eax +jz .get_num +mov ecx, eax +; r g b +lodsb +cmp al, 'r' +je .r_chk +cmp al, 'g' +je .g_chk +.b_chk: +or ebx, ecx ; no shift +jmp .next +.g_chk: +shl ecx, 8 +or ebx, ecx +jmp .next +.r_chk: +shl ecx, 16 +or ebx, ecx +.next: +xor edx, edx ; game over flag +; find one of , ; \n +lodsb +cmp al, ',' +je .get_num ; next number +cmp al, ';' +je .check_poss +cmp al, 10 ; \n +je .new_game +cmp esi, dword [file_lim] ; end of file +jge .new_game +jmp .next ; other char +.new_game: +mov edx, 1 ; game over flag +.check_poss: +mov eax, limits +.check_start: +cmp bl, al +jle .next_check +mov dword [esp], 0 ; fail check, game is null +jmp .round_done ; no need to check the other limits +.next_check: +shr ebx, 8 +shr eax, 8 +test ebx, ebx +jnz .check_start +.round_done: +; check for game end +test edx, edx +jnz .game_done +; reset counts +xor ebx, ebx +jmp .get_num ; new round +.game_done: +; add id to final value (impossible game is 0) +pop eax +add dword [final_value], eax +; checked earlier but... meh +cmp esi, dword [file_lim] ; end of file +jl .get_id ; new game +; we can print final value now +mov eax, [final_value] +call print_dec +jmp exit + +exit: +mov eax, 1 +int 0x80 ; exit + +; string in esi +; modifies EAX ESI +dec_parse: +push ebx +push ecx +push edx +push edi +xor eax, eax +xor ecx, ecx +xor edi, edi +mov ebx, 10 +.loop: +lodsb +sub al, '0' +js .dec_done +cmp al, 9 +jg .dec_done +xchg edi,eax +mul ebx +add edi,eax +jmp .loop +.dec_done: +mov eax,edi +pop edi +pop edx +pop ecx +pop ebx +ret + +; i hate this +print_dec: +sub esp, 12 +lea ecx, [esp+11] +test eax, eax +jz .zero +mov esi, 0 +mov ebx, 10 +.div_shit: +xor edx, edx +div ebx +dec ecx +inc esi +add dl, '0' +mov byte [ecx], dl +test eax, eax +jnz .div_shit +mov edx, esi +jmp .write +.zero: +mov byte [esp+10], 0 +lea ecx, [esp+10] +mov edx, 1 ; count +.write: +mov eax, 4 ; write +mov ebx, 1 ; stdout +int 0x80 +add esp, 12 +ret + +[section .data] +file_lim: dd 0 +final_value: dd 0 +filename: db "input",0 + +[section .bss] +read_buff: resb BUFF_LIM