Advent of Code 2023

Day 1, Part 1

Solution in 192 bytes

I wrote a simple solution to the first day's first part of the Advent of Code in x86 assembly.

The compiled binary takes up 192 bytes.

I couldn't test it against the full set but it does support 32bit numbers, even tho it's a 16bit .com file, so it won't overflow.

Unfortunately DOSBox crashes, and DOSBox-X doesn't support clipboard over 2048 bytes and pipes. Test it for me if you have the time. I don't see why it should not work.\

Download (192 bytes)

Here's a short proof it works:

And here's the source code:

%macro print 1 push ax push dx mov ah,02h mov dl,%1 int 21h pop dx pop ax %endmacro %macro num 1 ; print out num mov eax,%1 cmp eax,0 jne %%notzero print '0' jmp %%endend %%notzero: mov cx,0 %%loop: xor edx,edx cmp eax,0 je %%end mov ebx,10 div ebx push dx inc cx jmp %%loop %%end: cmp cx,0 je %%endend %%typeloop: pop dx add dx,30h mov ah,02h int 21h dec cx cmp cx,0 jne %%typeloop %%endend: %endmacro org 100h start: mov [cs:total],ax next_line: xor ax,ax mov dx,ax next_char: ; get char mov ah,01h int 21h ; if return, next line cmp al,13 je add_number ; check if digit cmp al,30h jl next_char cmp al,39h jg next_char ; if digit, checkl if first is set sub al,30h cmp dl,0 jne first_already_set ; if fisrt not set we set first and the flag inc dl mov [cs:first_num],al first_already_set: mov [cs:last_num],al ; repeat until enter jmp next_char add_number: ; if first not set, we finish cmp dl,0 je print_total xor ah,ah ; combine digits into number xor eax,eax mov al,[cs:first_num] mov ah,10 mul ah add al,[cs:last_num] mov [cs:combined],ax ; add number to total mov eax,[cs:combined] mov ebx,[cs:total] add eax,ebx mov [cs:total],eax jmp next_line print_total: num [cs:total] the_end: ; newline print 10 print 13 ret first_num_set: resb 1 first_num: resw 1 last_num: resw 1 combined: resd 1 total: resd 1

© Sos Sosowski 2023