day 3 part 1

This commit is contained in:
Lucia Ceionia 2023-12-03 02:11:23 -06:00
parent f9d0e13336
commit f805701abf
7 changed files with 634 additions and 2 deletions

View File

@ -60,7 +60,7 @@ cmp al, 9
jle .got_char
.no_input:
stc ; set CF
jmp .dec_done
jmp .done
.loop:
lodsb
sub al, '0'
@ -72,8 +72,9 @@ xchg edi,eax
mul ebx
add edi,eax
jmp .loop
clc ; clear CF
.dec_done:
clc ; clear CF
.done:
mov eax,edi
pop edi
pop edx

3
03/Makefile Normal file
View File

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

140
03/input Normal file
View File

@ -0,0 +1,140 @@
416.........................559...............417...............785.......900.......284...........503...796....992..........................
.........702*....772............378..569.........&.49..606...14*..............$.453*.........307....*......$.....-.................995......
.....................458...856......+.........+....&..............680.......104.............%....516.................................*......
...........822..174..*.....&...........711.746.......&............$....../.............656....#...........265=......634.*.............430...
..827.137..*...*....39................*..............856..............767........522......$..773....619..............*...287....501.........
..........726...511.............*.....320........476...............................*................%...899....72..731...........%....$.....
.....861..............232....223.933...............*.@........424*618.858.......................................$.......338.205........535..
.......#.............-....................676...713...427.................-.......615.........126...................=..*.......*...&........
....40..........996..............520.974.*..........#......*.566........907......................&...214...996*911.115.363..960..897........
....+.............*....................$..172.....559...763.....*............554......*.............*.......................................
.......527...#.....90.+....66.................................890..............=...802.93.131..791...209......&........928......303....$....
950.....*....773......105..............725........................................................%............886.......*........$.384.....
........383......741..............@..=....#.....179.18..%974..........624.......64.266.................701...........%..671.721.........942.
..................*.............914..548..........+.*.................*...........*...................*.........834.394.....*...............
.......502*80..960........................25........464.........831.846........25.........329..985...458.+.....&................377..659....
..........................................*.....292...............*............/..................@......350........938............*....$...
...738..............428......+.......311...742.........236*631....816.&......+....86.........81.......................*.973*341.266.........
......*.....673......*......614.........*.......689.48.................450...816.....754...........258..@585.......154..............@.......
...231.....#.........681...........855...775..........*.....=..257...................................*.............................469..=682
........#...................807...&.............418.19.....259....*459..906...185.356.......778.......230....................556............
.......924.........459......*..............804.=.........................*...*...........&....*................=........836..+......618.....
.....=............#.......900........+...........702..383........%..*...250..503.......637.808.......97/....@...370.......&........#.....710
..988........685............../663.273...........*...@..........16.251...........$..............*..........964.......658.....537........*...
...............*......171*......................714.....543............737.....372.............941.............113..*....=.......853....733.
............470..161......508.....56...170.............$...................389.....544.....208.....98.........%.....617...884......-..@.....
.766.591............../.........-......*.......210.........618......*874....-.........#...*....129.=..194......../...................992....
....../....170@..140+..753....918....467.854....*................989..........979...............*.............-.907...276...931*618.........
..700............................................801....929..859.....#..........*.............594.493......981..........*...................
....+............173..............................................251........629..........970........*................136.........388...*...
......721.......$......911....766..................541.234....=...........47......614....@.....*217..885.680.742....................#..84...
.377*....*81.............*.......&...937...........+...*....155..@......*............*......648............&...*.....&401......493..........
.....1.................533............*..399...........861......951..709.331..126.....876.....................289.........@.....*.......*...
..........107....452-......594.888.431....*..+660...........79................................$......275*876.......978.....594..400...39....
.........+................*....*.........925..........$.......*........599.......228..........60....................+....#..................
...283...................177...708...........642....661........294........*............................288*793.867.....482.479.769....73....
...........617..855*...&.............605................783.91.........847................&...../...............*..#..........*.......+.....
.672..303+...*........967...............*453.....884@......*...208.949........*..........986.....249.....352..818.596.56....................
...*..........88...............569.....................975.....*.....*..638....619.......................+.............*.......*.......462..
....500.....=...................*.................*205..*..-...228.508....&........................395..............845.....683.365....*....
........197.672.841..............214.=...847...912.....24.349...................%....584.257............152.................................
.626....-..........*..447............714....&.....................463....287...360........*............*...............*.................717
..........53......334...*........................*167......................*.......................554..246...........295.396......./.......
.......24..............92...........897.......220...........................930..703...414.....711..*.........723.........*....117...146....
.......&...........548.....324.21......................622.........820.404...............*....../..139......&......*.......578...*..........
.....*......204......*..........*.............226...........663.......*......555.992.....502...........631..49.#....366........677..........
..958.350.....*......44...694...449..-39........*...........-...786.........*......*............................140.........................
...............477.........*...................815..............*..........815.................521....................&...273........103....
.........828............781....464..................276......646....665-............924..........*.......955........759.........934...#.....
........*..........*..........*..........15.193.......*.....................&.................808....347*......963........*....+............
.........810.....360........638...434.....*....*751....812.....573...........85.........+.........95.......892...*...153...220..........187.
....714*....................................................80*....162=.................214...........842...&...39..........................
........265.........................51.......285$.......................586.......=.................................*.....*..............948
..989..........22.=......374.......%....................142.......736..@.....507.636.797.....273.........872.....567.978..334.....382...*...
..........@.......68..=......................116.130.......%.250../......-...=........*.........*486.415..........................*....813..
.......505............61..140..........435..........*691......*........852..........571....408..............12......80.......228...109......
.........................*.........207...*..24................402.=...........................@....................../...162................
....285.................672.552........492....$./..................3..620.391...............................179............*................
...*..........159...428.......*.................390.111.........$.....*...*..................348.355.481.......&.210..99...41...............
...52......12..@..........808.810.897...................663..280....57..............476.............*............*.....*........208=........
..........*...............*.........@.286..................*...........................*..........................905.296..............148..
.......298......119.....172.................622$..637................#......342.......679.111......-968..104*478............#.......*.......
.../.............*........../......................*..............658..........*718..........*136.....................503.899....889.498....
....691........341.262..36.549...........386........437.............................662...........848............#......*...................
.......................*..........936...*...............................-...........*......516....%......358....707..535...........841......
......$..............639............*..798.../..67%............137...716.......313.247......................-............@.....371..........
....433.677..605.267................1.......930........478........*........565*................................869.......372..@....228......
...........@..*..$...794.........................74.......#./......833.348.......................................+................*.........
......865...............*.........................*..........839.....................=......................................916..84.@.......
.................-....451........541@......468..684....18............759.............499................124.....426*.........*......882.....
...68*...........614........509...............*.........&...956*308.%........&....36...........480+....../..........917....32....#..........
......363..377.............*..........441.....418..........................279.....*.................139..........................944.......
........................412............*..........%....920*585....526*............931.346&.807..840.....*.626-...#....................923...
.....283.....924...+..................628.......33....................908..766..............*........336..........446...........191.........
...............*.249........@264..35...................502.791.#...........*......=............126....................957....71....*.768....
.........144&.36.....216.........*...........................&.730..........201..581.704.........$......715.............=......*.......%....
...349..............*....598...949.........189....981.....#.............524...................*............=................440...847.......
.................967....#..........999*6..%.........-......604............&.189.626...#774.159.647....................168.........../...329.
..............................................481.....*........................*.................................747..%.................#...
..........245...878.......495....57....841.........351.517..........-297........................343.599/........&.......360..........-......
430........&..............&.....*...&.....*885...........................9....392.......93..336*...........................*321.....86......
....469......#.................999..796..................899.........250*........$.......*........247............................+.......328
......*...696...615..300..603..............................*....232...................402...501......*.......77.271.....@.........146.......
......170......%....$.........*..991..........782.49.......128.............................*........161........*.....648......123...........
..........646................901..*.............*......134...........517..2..287........513...............................644...*...........
...................999*620.........541.379.....488..18*.......................*...............208....931.338..%143...........*...19.........
...+.......938...........................*...%.............&........626......268.418......841*..........*...............820.395..........913
.723...834*..........+............162.667.....84............803.804...*............*................399........172..530*....................
.....................596............*............................./.287............852....623....@.*..............@........168...965..*17...
.....=488...................758....607.....&......53%......................105.683......-...*.860..244.....&....$....232....................
.............................*..........333....................197........+....*......334.954.............686...464...............544.......
.139=..............754...993.677..../.......933..........391....*....357*.....952......................@..........................*.........
..........831............../.........94.600................+.121.........183........18..686.....665*..823...........575...........68........
141...800....*....692...........254@.....*...819-.....................................*..@...................17.......*...815...........686.
........%.602........%.................186..........562.93....774.....................11....948....912.........*328..243.*..................
.......................................................*..........607....280..................&....+.....................548.&897.100@...955
......504.....605...........889@.............975..664....$.....#............*....141.................378%.........812.......................
.....=...........*.....730...............%...............899..480.....*....375.....-.444.........47*.........@63...........828.....468..462.
.......607.../...23....=......=........383.............................522............*.............433..360..................*372...*......
........*...975.............428...................592*......@258...114......%......431...................*......+........658.......560.163..
........16........................329.......315.......973............*...333....@.......$....782..18...58....533........*...............*...
..327.............563...889.......*...........*..595...............913........671..354..937...%..*...&...........&.787...975.......676...247
.....+..&...401..*..............713........=.757...#.873*47...#465..................................440..588/..859.%..................*.....
.665....133...%..196....907..............348..................................162........305...................................163.-........
....*....................*...........170.........222.804....784..............*...................................................=.742......
..239..338.....27-.437..543.........*....609......*..*......-...............582..-...........343*560....852.../....636..263.................
..........*900.......*............17...........948.....597.....=....922...........682./255................@..35.......*.......211-..#.......
.......*...........686........984..........78......896*......13........+....................985...&....................278.........876.835..
....630.82....$851.....905.......*..281...*...............................352..........*746........215.877....845..........471.852.....*....
.........................*.....757.*......625...........924......878..........912...............-.........*../.....538%......+.........871..
..954*712..977.....-..762..........236.........527*674.*.........*........811*.....228....&.....5........739...483..........................
..........*.......470..................................707....363.................*......525.......303..........*........463....537.........
...460...947...............130.....757./96........*.................=...529....+..466........527................65..%648....=.....*.........
.....*......................$....../..............968..............479.&....630............................598.................952..........
.......&..........793...................................514...............-...................311..........=....83......#.............124...
.975....399.......*...-.......*814...663..................*....*...........381../...............*.............@.........284............*....
...*............367....198..........@.....992.....716*529....96.................729.329.688...%.322.-......67.79......-.......335....997....
488............................632........*...../......................387.............*....225......491.............391..750...%...........
.......861.527......778.165.......*........187..916.......845....-....*.......873.................................@.........................
..545.....*.....$.....*..*......627............................736.376..845...*....594........+.......171.........292...........*750..-.....
.................407.403............+...342.................+..........*.......897..........400.......$......................910.......350..
...........................40..284.66...*......494..........255.......653..............866.................942=..................*52........
....&........827.......296...*........$.401......*..............892*...................$.............1.164.......343..........485...........
.930............*.........*..944...308...........763....../.........62.....113....=...........421........*..........................580+....
........50*.....934......705............................999..................*....541...............847-.950..............*131..............
...........209....................................&404...............276..242.............723.652................873...313..............24..
......*........606....550#.....2./........@..................899.......*.....................*..........235..112...........704.....337......
.......837....*...............*...83...993.....*416.........-....580%..535......../.....-204...............#..*.....93+....*...723./........
..............168.....753..593..............504......./........................488..............=.............458.......872...*.............
...#85...................&......911/...................880........315..872..........=....494..349....466..428..................40...........
........939........648......................*................227...*......*.......924...*...........*.....*.................................
918*......&.@........*...902..269..834....87.826.........../...*...919.118...817......109..........933..643...........&..............-......
....949......883...111..%......*...%..................710.464.943.........../...................................842....305.....469..289.....
..........................%...974......*407..168.647...*..............617.......498/.....848...........@99........*...........+.............
..........176...........120.........469........&...#....997......464.......274.............*......./............477.417../.......738........
.........*.......964.........291..........................................-.............2..29...272..465...............-..819........718*265
....298...747.......#.......*.....+745...........460...741*762.275...491.......+.735*34.*............-....*841......+............*..........
...*........................593..............298...#............%....$......225.........943........................23.....702.601.616.......
..889................695........654..750.....*.............637........./...............................780....*726....233...*...............
..................../.................*.....453.....642....*.........828......@...94...........152/...*....790.......*.....445......../.....
...........................51.......681........................271..........719.......................964......399..426...............456...

10
03/input_test1 Normal file
View File

@ -0,0 +1,10 @@
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..

179
03/main.s Normal file
View File

@ -0,0 +1,179 @@
%define BUFF_LIM 32768
;%define GRID_X 10
;%define GRID_Y 10
%define GRID_X 140
%define GRID_Y 140
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
; 1. read file into array,
; padding edges by 1
read_to_array:
mov esi, read_buff
; first line padded by .
mov edi, array
mov ecx, (GRID_X+2)
mov al, '.'
rep stosb
mov ecx, GRID_Y ; line count
.read_lines:
; 1 char pad before line
mov al, '.'
stosb
push ecx
.read_chars:
mov ecx, GRID_X ; line len (discluding newline)
rep movsb ; copy line
inc esi ; skip newline
mov al, '.'
stosb ; pad with .
pop ecx
loop .read_lines
; fill last line with .
mov ecx, (GRID_X+2)
mov al, '.'
rep stosb
; 2. find numbers, putting indexes
; into numbers array in the index array
find_numbers:
push 1 ; current number index
xor ecx, ecx ; index
.find_next:
; check for end of array
cmp ecx, (GRID_X+2)*(GRID_Y*2)
jge .done
lea esi, [ecx+array] ; current char
mov edi, esi ; number start if num
call dec_parse
jnc .has_num
; no number
inc ecx
jmp .find_next
.has_num:
sub esi, edi ; get number char length
add esi, ecx ; add to index
dec esi
; write to index and to numbers
pop ebx ; cur num idx
mov [numbers+ebx*4], eax ; save number value
.write_idx:
mov [index+ecx*2], bx
; overwrite digits with .
mov byte [array+ecx], '.'
inc ecx
cmp ecx, esi
jl .write_idx
inc ecx
; push new cur num idx
inc ebx
push ebx
jmp .find_next
.done:
add esp, 4 ; no more cur num idx
; 3. go through array, checking the neighbors of
; numbers for symbols, skip *last added index*,
; adding numbers with sym neighbors to final_value
check_neighbors:
xor ecx, ecx ; index
dec ecx ; gets incremented immediately
xor ebx, ebx ; last added idx
mov esi, array
mov edi, index
.check_next_number:
; check for end of array
inc ecx
cmp ecx, (GRID_X+2)*(GRID_Y*2)
jge .done
; check if num
movzx eax, word [index+ecx*2]
test eax, eax
jz .check_next_number
.found_num:
; check if last added idx
cmp eax, ebx
je .check_next_number ; is last added idx, skip
.check_symbols:
lea edx, [ecx+array] ; current char
; right
cmp byte [edx+1], '.'
je .r_nsym
jmp .has_sym_neighbor
.r_nsym:
; left
cmp byte [edx-1], '.'
je .l_nsym
jmp .has_sym_neighbor
.l_nsym:
; top
cmp byte [edx-(GRID_X+2)], '.'
je .t_nsym
jmp .has_sym_neighbor
.t_nsym:
; bottom
cmp byte [edx+(GRID_X+2)], '.'
je .b_nsym
jmp .has_sym_neighbor
.b_nsym:
; top left
cmp byte [edx-((GRID_X+2)+1)], '.'
je .tl_nsym
jmp .has_sym_neighbor
.tl_nsym:
; top right
cmp byte [edx-((GRID_X+2)-1)], '.'
je .tr_nsym
jmp .has_sym_neighbor
.tr_nsym:
; bottom left
cmp byte [edx+((GRID_X+2)-1)], '.'
je .bl_nsym
jmp .has_sym_neighbor
.bl_nsym:
; bottom right
cmp byte [edx+((GRID_X+2)+1)], '.'
je .br_nsym
jmp .has_sym_neighbor
.br_nsym:
; there's no symbol neighbor
; get next number
jmp .check_next_number
.has_sym_neighbor:
; we have a symbol neighbor!
; add to final value
mov edx, [numbers+eax*4]
add [final_value], edx
; set last added idx
mov ebx, eax
; get next number
jmp .check_next_number
.done:
; we can print final value
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]
numbers: resd 2048
array: resb (GRID_X+2)*(GRID_Y*2)
index: resw (GRID_X+2)*(GRID_Y*2)
read_buff: resb BUFF_LIM

179
03/main_part1.s Normal file
View File

@ -0,0 +1,179 @@
%define BUFF_LIM 32768
;%define GRID_X 10
;%define GRID_Y 10
%define GRID_X 140
%define GRID_Y 140
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
; 1. read file into array,
; padding edges by 1
read_to_array:
mov esi, read_buff
; first line padded by .
mov edi, array
mov ecx, (GRID_X+2)
mov al, '.'
rep stosb
mov ecx, GRID_Y ; line count
.read_lines:
; 1 char pad before line
mov al, '.'
stosb
push ecx
.read_chars:
mov ecx, GRID_X ; line len (discluding newline)
rep movsb ; copy line
inc esi ; skip newline
mov al, '.'
stosb ; pad with .
pop ecx
loop .read_lines
; fill last line with .
mov ecx, (GRID_X+2)
mov al, '.'
rep stosb
; 2. find numbers, putting indexes
; into numbers array in the index array
find_numbers:
push 1 ; current number index
xor ecx, ecx ; index
.find_next:
; check for end of array
cmp ecx, (GRID_X+2)*(GRID_Y*2)
jge .done
lea esi, [ecx+array] ; current char
mov edi, esi ; number start if num
call dec_parse
jnc .has_num
; no number
inc ecx
jmp .find_next
.has_num:
sub esi, edi ; get number char length
add esi, ecx ; add to index
dec esi
; write to index and to numbers
pop ebx ; cur num idx
mov [numbers+ebx*4], eax ; save number value
.write_idx:
mov [index+ecx*2], bx
; overwrite digits with .
mov byte [array+ecx], '.'
inc ecx
cmp ecx, esi
jl .write_idx
inc ecx
; push new cur num idx
inc ebx
push ebx
jmp .find_next
.done:
add esp, 4 ; no more cur num idx
; 3. go through array, checking the neighbors of
; numbers for symbols, skip *last added index*,
; adding numbers with sym neighbors to final_value
check_neighbors:
xor ecx, ecx ; index
dec ecx ; gets incremented immediately
xor ebx, ebx ; last added idx
mov esi, array
mov edi, index
.check_next_number:
; check for end of array
inc ecx
cmp ecx, (GRID_X+2)*(GRID_Y*2)
jge .done
; check if num
movzx eax, word [index+ecx*2]
test eax, eax
jz .check_next_number
.found_num:
; check if last added idx
cmp eax, ebx
je .check_next_number ; is last added idx, skip
.check_symbols:
lea edx, [ecx+array] ; current char
; right
cmp byte [edx+1], '.'
je .r_nsym
jmp .has_sym_neighbor
.r_nsym:
; left
cmp byte [edx-1], '.'
je .l_nsym
jmp .has_sym_neighbor
.l_nsym:
; top
cmp byte [edx-(GRID_X+2)], '.'
je .t_nsym
jmp .has_sym_neighbor
.t_nsym:
; bottom
cmp byte [edx+(GRID_X+2)], '.'
je .b_nsym
jmp .has_sym_neighbor
.b_nsym:
; top left
cmp byte [edx-((GRID_X+2)+1)], '.'
je .tl_nsym
jmp .has_sym_neighbor
.tl_nsym:
; top right
cmp byte [edx-((GRID_X+2)-1)], '.'
je .tr_nsym
jmp .has_sym_neighbor
.tr_nsym:
; bottom left
cmp byte [edx+((GRID_X+2)-1)], '.'
je .bl_nsym
jmp .has_sym_neighbor
.bl_nsym:
; bottom right
cmp byte [edx+((GRID_X+2)+1)], '.'
je .br_nsym
jmp .has_sym_neighbor
.br_nsym:
; there's no symbol neighbor
; get next number
jmp .check_next_number
.has_sym_neighbor:
; we have a symbol neighbor!
; add to final value
mov edx, [numbers+eax*4]
add [final_value], edx
; set last added idx
mov ebx, eax
; get next number
jmp .check_next_number
.done:
; we can print final value
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]
numbers: resd 2048
array: resb (GRID_X+2)*(GRID_Y*2)
index: resw (GRID_X+2)*(GRID_Y*2)
read_buff: resb BUFF_LIM

120
03/utils.s Normal file
View File

@ -0,0 +1,120 @@
; 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:
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
; 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