day 11 part 1&2

This commit is contained in:
Lucia Ceionia 2023-12-11 03:28:34 -06:00
parent fa4a9b3efd
commit dd65bf8a56
6 changed files with 956 additions and 0 deletions

3
11/Makefile Normal file
View File

@ -0,0 +1,3 @@
all:
nasm -g -felf32 main.s && ld -melf_i386 -g main.o

140
11/input Normal file
View File

@ -0,0 +1,140 @@
...........................#.......#.....................#................#.......................................#.............#...........
..................................................#...........................................#.......................................#.....
........#....................................#....................................#.......................#.................................
........................................................................................................................#...................
#...................#.......................................#...............................................................................
...............#.......................................................................#...........#...........#............#...............
........................................................#...........................................................................#.......
..............................................#.....................#...................................#...................................
.............................#......#.....................................#.................................................................
.......................#...........................#............................................#.................#.........................
.....#.....................................................................................................................................#
...........#......#...............................................#..............................................................#..........
.......................................................................................#...................#................................
..............................#........................#....................................................................#...............
.................................................#..........#......................................#........................................
........#................................#...................................................#..........#.....#......#......................
.........................#.........................................................................................................#........
............#.................................................................#.........#...................................................
....................#.......................................................................................................................
............................................#.............#.................................................................................
..............................#.....#...........................................................#........................................#..
#.........#........................................#............#................................................................#..........
.........................................................................................................#..................................
..................#.....................................#..............................#.............................#.......#..............
...........................#..............................................#.................................................................
...........................................................................................#......#.........................................
.......................#.......#..................#..........#....................................................................#......#..
.....#.............................................................#............................................#...........................
....................................#............................................#......................................#...................
.....................................................................................................................................#......
..............#.............................#.......#....................................#..................................................
.........#......................................................#.................................................#...........#.............
............................................................................................................................................
.#.....................................................................#..........#....................#....................................
..................................#....................................................................................................#....
...........#.........#............................#......#........................................#.........................................
................................................................................................................................#...........
..............................................................................................#.............................................
......................................#..............#.......................................................#...........#..................
....#............#.........................................#................................................................................
............#..............#.............................................................#..................................................
...............................................#...................................................#..........................#............#
..................................#.........................................................................................................
........................................................................#...........#...............................#.....#.................
#.........#..............#..................................................................................................................
.......................................................................................................................................#....
................#..........................................#................#................#.................#..................#.........
...................................................................#........................................................................
................................................#....................................................#......................................
.....................................................#..........................................#...........................................
..........................#.................................................................................................................
.....#.....#.....................................................................#.......#..............................#...................
.....................#........................#...............................................................#................#............
................................#........#............................................................#.....................................
.................................................................................................#................#.........................
..................................................................#.................................................................#.......
............................................................................................................................................
................................................#.....#.................#................#....................................#.............
......................#.....................................................................................#........#..................#...
..........#........................#................................................#.......................................................
....#.............#...................................................................................#...........................#.........
.........................................#..................................................#.....................#.........................
............................................................................................................................................
..........................#..................#..........................#.................................................#.................
.......................................................................................#......................#.............................
................................#......#.....................#................................#......................#..................#...
............................................................................................................................................
........#.........#.................................................................#.......................................#......#........
..........................................#........#........................................................................................
............................................................................................................................................
...............................................................................................#.......................#....................
............................#............................................................................#.................................#
............................................................................................................................................
......#...........#...................................#........#........#...................................................................
....................................................................................#.........................................#.............
.......................................#.........#......................................................................#...................
............................................................................................................................................
..............................#..................................................................................#..........................
.......................#......................................................#................#............................................
........#..........................................................#..................#................#...........................#........
.............#.....#................#.......................................................................................................
.........................................#...............................#.....................................#............................
.#...........................................................#...................#..................#.......................................
...........................#.................#............................................................................#.................
.......................................................#..................................................#.........#.....................#.
......................................................................................#........#............................................
......#.........#.................#............................#................................................................#...........
..........................................................................#.................................................................
...........#.........#.........................................................................................#............................
...........................................#..............#.................................................................#...............
.....................................................#.............#...................................................................#....
....#.........#.................#................................................................................................#..........
.......................#.......................................................#..................#........#............#...................
......................................#..................................#...................#..............................................
.......#.....................................#......................................................................#.......................
..#..........................#...........................#..........................#.......................................................
...........#.............................................................................#......................#......................#....
...............................................................#.........................................#..................#...............
.................#......................#........#...............................#..........................................................
......................#.......................................................................#......#..............................#.......
............................#......#......................#.................................................................................
...........................................#.............................#..........................................#.....#.................
...........#......................................................................................#.....................................#...
.......................................................#..........#....................................#.......#.................#..........
...............................#........................................................#...................................................
..#.....#.............................#................................#....................................................................
..................................................#...........................#.............................#...............................
.............................................................................................#.....#........................................
...............#........................................#.............................................................#.....................
#....................#...........#..........................................................................................................
............................#..............#..............................#.......................................................#.........
.....................................................................................#......................................................
.................#...........................................................................................#..............................
.........#..............#............................................................................................#...................#..
....................................#................#..............................................#.......................................
...............................#.........#.................#...................#.........................#..................#...............
#.............................................#........................................#....................................................
......................................................................#..........................................#..........................
.............#...............................................................................#........#...............................#.....
...#............................................................#............................................#..........#..................#
...................................................#........................................................................................
..........................................#..............................#..................................................#...............
.............................................................#....................#.....................................................#...
.....................#..............#..................#................................................#...................................
.......#.......................#........................................................#.......#..............#.................#..........
..........................................................................................................................#.................
...#.......................................#....................#...................#.......................................................
..................#.........................................................................................................................
...........................#...........#...................................................................#......#.........................
................................#.............#....................#........................................................................
.........................................................................................#.......#..........................................
.......#.....................................................#................................................................#...........#.
..................................................#.......................#.................................................................
.............#...................................................................................................#...................#......
............................#.........#................#.............................#................#.....................................
......................#.......................................................................#...........................#.................
............................................................................................................................................
.............................................................................................................#.......#............#.........
.......#...........#.....#.....#.............................#..............................................................................
.............#...............................#..........#..........................................#......................................#.

10
11/input_test Normal file
View File

@ -0,0 +1,10 @@
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....

278
11/main.s Normal file
View File

@ -0,0 +1,278 @@
%define EXPANSION 1000000-1
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
mov eax, -1
mov ecx, 512
mov edi, empty_rows
rep stosd
mov eax, -1
mov ecx, 512*2
mov edi, stars
rep stosd
; find empty rows
xor ebx, ebx
mov ebp, empty_rows
check_rows:
mov eax, [line_len]
mov ecx, eax
mul ebx
lea edi, [eax+file]
mov esi, edi
add esi, ecx
mov al, '.'
repe scasb
mov eax, esi
cmp edi, esi
jne .not_empty
.empty:
mov [ebp], ebx
add ebp, 4
.not_empty:
.cont:
inc ebx
cmp eax, file.over
jb check_rows
; find empty cols
xor ebx, ebx
mov ebp, empty_cols
check_cols:
lea edi, [file+ebx]
.l:
cmp edi, file.over
jae .empty
cmp byte [edi], '.'
jne .not_empty
add edi, [line_len]
jmp .l
.empty:
mov [ebp], ebx
add ebp, 4
.not_empty:
inc ebx
cmp ebx, [line_len]
jb check_cols
print_empty_rows:
mov esi, empty_str
mov ecx, 6
call print_string
mov esi, empty_rows
xor eax, eax
.l:
lodsd
cmp eax, -1
je .done
call space
call print_dec
jmp .l
.done:
call newline
print_empty_cols:
mov esi, empty_str
mov ecx, 6
call print_string
mov esi, empty_cols
xor eax, eax
.l:
lodsd
cmp eax, -1
je .done
call space
call print_dec
jmp .l
.done:
call newline
; find #s
xor ebx, ebx ; row
mov ebp, stars
find_stars:
mov eax, ebx
mul dword [line_len]
lea edi, [file+eax] ; row ptr
cmp edi, file.over
jae .done
xor ecx, ecx ; col
.for_cols:
cmp byte [edi+ecx], '#'
jne .for_cols_cont
mov eax, ebx
call print_dec
call space
mov [ebp], ebx ; row
mov eax, ecx
call print_dec
call newline
mov [ebp+4], ecx ; col
add ebp, 8
.for_cols_cont:
inc ecx
cmp ecx, [line_len]
jb .for_cols
inc ebx
jmp find_stars
.done:
call newline
; calc adjustments
calc_adj:
xor ecx, ecx
.fill:
mov [adj_rows+ecx*4], ecx
mov [adj_cols+ecx*4], ecx
inc ecx
cmp ecx, 256
jb .fill
; do rows
mov esi, empty_rows
.rows:
lodsd
cmp eax, -1
je .rows_done
inc eax
.add_to_rows:
add dword [adj_rows+eax*4], EXPANSION
inc eax
cmp eax, 256
jb .add_to_rows
jmp .rows
.rows_done:
; do cols
mov esi, empty_cols
.cols:
lodsd
cmp eax, -1
je .cols_done
inc eax
.add_to_cols:
add dword [adj_cols+eax*4], EXPANSION
inc eax
cmp eax, 256
jb .add_to_cols
jmp .cols
.cols_done:
; apply adjustments
apply_adj:
xor eax, eax
mov esi, stars
mov edi, stars
.l:
lodsd ; row
cmp eax, -1
je .done
mov eax, [adj_rows+eax*4]
stosd
lodsd ; col
mov eax, [adj_cols+eax*4]
stosd
jmp .l
.done:
; print stars
print_stars:
mov esi, stars
xor eax, eax
.l:
lodsd
cmp eax, -1
je .done
call print_dec
call space
lodsd
call print_dec
call newline
jmp .l
.done:
call newline
; calc distances
sub esp, 4
mov ebp, stars
calc_dist:
lea esi, [ebp+8]
mov ecx, [ebp] ; row
mov edx, [ebp+4] ; col
mov [esp], ebp
xor edi, edi ; running sum low
xor ebp, ebp ; running sum high
.to_next:
lodsd ; row
cmp eax, -1
je .all_added
sub eax, ecx
; abs (m := n>>31, n := (n+m)^m)
mov ebx, eax
sar ebx, 31
add eax, ebx
xor eax, ebx
; add to total
add edi, eax
adc ebp, 0
lodsd ; col
sub eax, edx
mov ebx, eax
sar ebx, 31
add eax, ebx
xor eax, ebx
; add to total
add edi, eax
adc ebp, 0
jmp .to_next
.all_added:
add dword [final_value], edi
adc dword [final_value_high], ebp
calc_dist_cont:
mov ebp, [esp]
add ebp, 8
cmp dword [ebp], -1
jne calc_dist
calc_dist_end:
add esp, 4
game_over:
; answer is penultimate + (ultimate * 2^32)
mov eax, [final_value]
call print_dec
call newline
mov eax, [final_value_high]
call print_dec
call newline
jmp exit
[section .data]
line_len: dd 0
final_value: dd 0
final_value_high: dd 0
file: incbin "input"
.over:
empty_str: db "Empty:"
not_empty_str: db "Not Empty:"
[section .bss]
empty_rows: resd 256
empty_cols: resd 256
adj_rows: resd 256
adj_cols: resd 256
stars: resd 512*2

267
11/main_part1.s Normal file
View File

@ -0,0 +1,267 @@
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
mov al, 0xff
mov ecx, 512
mov edi, empty_rows
rep stosb
mov eax, -1
mov ecx, 512
mov edi, stars
rep stosd
; find empty rows
xor ebx, ebx
mov ebp, empty_rows
check_rows:
mov eax, [line_len]
mov ecx, eax
mul ebx
lea edi, [eax+file]
mov esi, edi
add esi, ecx
mov al, '.'
repe scasb
mov eax, esi
cmp edi, esi
jne .not_empty
.empty:
mov [ebp], bl
inc ebp
.not_empty:
.cont:
inc ebx
cmp eax, file.over
jb check_rows
; find empty cols
xor ebx, ebx
mov ebp, empty_cols
check_cols:
lea edi, [file+ebx]
.l:
cmp edi, file.over
jae .empty
cmp byte [edi], '.'
jne .not_empty
add edi, [line_len]
jmp .l
.empty:
mov [ebp], bl
inc ebp
.not_empty:
inc ebx
cmp ebx, [line_len]
jb check_cols
print_empty_rows:
mov esi, empty_str
mov ecx, 6
call print_string
mov esi, empty_rows
xor eax, eax
.l:
lodsb
cmp al, -1
je .done
call space
call print_dec
jmp .l
.done:
call newline
print_empty_cols:
mov esi, empty_str
mov ecx, 6
call print_string
mov esi, empty_cols
xor eax, eax
.l:
lodsb
cmp al, -1
je .done
call space
call print_dec
jmp .l
.done:
call newline
; find #s
xor ebx, ebx ; row
mov ebp, stars
find_stars:
mov eax, ebx
mul dword [line_len]
lea edi, [file+eax] ; row ptr
cmp edi, file.over
jae .done
xor ecx, ecx ; col
.for_cols:
cmp byte [edi+ecx], '#'
jne .for_cols_cont
mov eax, ebx
call print_dec
call space
mov [ebp], bx ; row
mov eax, ecx
call print_dec
call newline
mov [ebp+2], cx ; col
add ebp, 4
.for_cols_cont:
inc ecx
cmp ecx, [line_len]
jb .for_cols
inc ebx
jmp find_stars
.done:
call newline
; calc adjustments
calc_adj:
xor ecx, ecx
.fill:
mov [adj_rows+ecx], cl
mov [adj_cols+ecx], cl
inc ecx
cmp ecx, 256
jb .fill
; do rows
mov esi, empty_rows
.rows:
lodsb
cmp al, -1
je .rows_done
movzx eax, al
inc eax
.add_to_rows:
inc byte [adj_rows+eax]
inc eax
cmp eax, 256
jb .add_to_rows
jmp .rows
.rows_done:
; do cols
mov esi, empty_cols
.cols:
lodsb
cmp al, -1
je .cols_done
movzx eax, al
inc eax
.add_to_cols:
inc byte [adj_cols+eax]
inc eax
cmp eax, 256
jb .add_to_cols
jmp .cols
.cols_done:
; apply adjustments
apply_adj:
xor eax, eax
mov esi, stars
mov edi, stars
.l:
lodsw ; row
cmp ax, -1
je .done
mov al, [adj_rows+eax]
stosw
lodsw ; col
mov al, [adj_cols+eax]
stosw
jmp .l
.done:
; print stars
print_stars:
mov esi, stars
xor eax, eax
.l:
lodsw
cmp ax, -1
je .done
call print_dec
call space
lodsw
call print_dec
call newline
jmp .l
.done:
call newline
; calc distances
mov ebp, stars
xor edi, edi ; running total
calc_dist:
lea esi, [ebp+4]
movzx ecx, word [ebp] ; row
movzx edx, word [ebp+2] ; col
xor eax, eax
.to_next:
lodsw ; row
cmp ax, -1
je .all_added
sub eax, ecx
; abs (m := n>>31, n := (n+m)^m)
mov ebx, eax
sar ebx, 31
add eax, ebx
xor eax, ebx
; add to total
add edi, eax
lodsw ; col
sub eax, edx
mov ebx, eax
sar ebx, 31
add eax, ebx
xor eax, ebx
; add to total
add edi, eax
jmp .to_next
.all_added:
calc_dist_cont:
add ebp, 4
cmp dword [ebp], -1
jne calc_dist
calc_dist_end:
mov [final_value], edi
game_over:
mov eax, [final_value]
call print_dec
call newline
jmp exit
[section .data]
line_len: dd 0
final_value: dd 0
file: incbin "input"
.over:
empty_str: db "Empty:"
not_empty_str: db "Not Empty:"
[section .bss]
empty_rows: resb 256
empty_cols: resb 256
adj_rows: resb 256
adj_cols: resb 256
stars: resd 512

258
11/utils.s Normal file
View File

@ -0,0 +1,258 @@
; call # val val2
; int $0x80 eax eax edx -
;
; arg1 arg2 arg3 arg4 arg5 arg6 arg7
; ebx ecx edx esi edi ebp -
exit:
mov eax, 1 ; exit
int 0x80
; filename in EBX
; return handle in EBX
; read only
open_file:
push eax
push ecx
mov eax, 5 ; open
xor ecx, ecx ; read only
int 0x80
mov ebx, eax
pop ecx
pop eax
ret
; file handle in EBX
; buffer in ECX
; count of bytes to read in EDX
; return bytes actually read in EAX
; exits on error
read_file:
mov eax, 3 ; read
int 0x80
test eax, eax
js .err
ret
.err:
mov eax, 4 ; write
mov ebx, 1 ; stdout
mov ecx, .err_str
mov edx, 21
int 0x80
jmp exit
.err_str: db `Could not read file.\n`
; string input in ESI
; value in EAX
; CF set if none, clear if some
; ESI set past checked area
dec_parse:
push ebx
push edx
push edi
xor eax, eax
xor edi, edi
mov ebx, 10 ; base
lodsb
sub al, '0'
js .no_input
cmp al, 9
jle .got_char
.no_input:
stc ; set CF
jmp .done
.loop:
xor eax,eax
lodsb
sub al, '0'
js .dec_done
cmp al, 9
jg .dec_done
.got_char:
xchg edi,eax
mul ebx
add edi,eax
jmp .loop
.dec_done:
clc ; clear CF
.done:
mov eax,edi
pop edi
pop edx
pop ebx
ret
; string input in ESI
; value in EAX
; CF set if none, clear if some
; ESI set past checked area
sign_dec_parse:
push ebx
push ecx
push edx
push edi
xor eax, eax
xor edi, edi
xor ecx, ecx ; neg flag
mov ebx, 10 ; base
cmp byte [esi], '-'
jne .no_minus
inc esi
mov cl, 1
.no_minus:
lodsb
sub al, '0'
js .no_input
cmp al, 9
jle .got_char
.no_input:
stc ; set CF
jmp .done
.loop:
xor eax,eax
lodsb
sub al, '0'
js .dec_done
cmp al, 9
jg .dec_done
.got_char:
xchg edi,eax
mul ebx
add edi,eax
jmp .loop
.dec_done:
test ecx, ecx
jz .not_neg
neg edi
.not_neg:
clc ; clear CF
.done:
mov eax,edi
pop edi
pop edx
pop ecx
pop ebx
ret
; modifies no regs
newline:
pushad
push 10
mov eax, 4 ; write
mov ebx, 1 ; stdout
mov ecx, esp ; string
mov edx, 1 ; length
int 0x80
add esp, 4
popad
ret
; modifies no regs
space:
pushad
push 9
mov eax, 4 ; write
mov ebx, 1 ; stdout
mov ecx, esp ; string
mov edx, 1 ; length
int 0x80
add esp, 4
popad
ret
; input in EAX, all regs unmodified
print_dec:
pushad ; save regs
; max 4294967296 is 10 chars
; round to nearest 32-bit boundary
sub esp, 12
; string in ECX, length in EDX
lea ecx, [esp+11] ; last possible byte
; check for 0
test eax, eax
jz .zero
mov ebx, 10 ; base 10
xor esi, esi ; counter
.div_shit:
xor edx, edx
; divide
div ebx
dec ecx ; next char
inc esi
; store
add dl, '0'
mov byte [ecx], dl
; check if done
test eax, eax
jnz .div_shit ; continue
mov edx, esi ; counter in edx
jmp .write
.zero:
mov byte [ecx], '0'
mov edx, 1 ; length
.write:
mov eax, 4 ; write
mov ebx, 1 ; stdout
int 0x80
add esp, 12 ; restore stack
popad ; restore regs
ret
; input in EAX, all regs unmodified
print_sign_dec:
pushad ; save regs
; range -2147483648 to 2147483647 is 11 chars
; round to nearest 32-bit boundary
sub esp, 12
; string in ECX, length in EDX
lea ecx, [esp+11] ; last possible byte
; check for 0, negative
xor ebp, ebp
test eax, eax
jz .zero
jns .positive
neg eax
mov ebp, 1
.positive:
mov ebx, 10 ; base 10
xor esi, esi ; counter
.div_shit:
xor edx, edx
; divide
div ebx
dec ecx ; next char
inc esi
; store
add dl, '0'
mov byte [ecx], dl
; check if done
test eax, eax
jnz .div_shit ; continue
mov edx, esi ; counter in edx
jmp .write
.zero:
mov byte [ecx], '0'
mov edx, 1 ; length
.write:
test ebp, ebp
jz .no_minus
dec ecx
inc edx
mov byte [ecx], '-'
.no_minus:
mov eax, 4 ; write
mov ebx, 1 ; stdout
int 0x80
add esp, 12 ; restore stack
popad ; restore regs
ret
; input in ESI, len in ECX, all regs unmodified
print_string:
pushad ; save regs
mov eax, 4 ; write
mov ebx, 1 ; stdout
mov edx, ecx ; length
mov ecx, esi ; string
int 0x80
popad ; restore regs
ret