flat assembler
Message board for the users of flat assembler.
Index
> Macroinstructions > fasmg usage examples: set of integers, long list of integers 
Author 

Grom PE 21 Sep 2016, 12:07
For fun and practice, I rewrote a puzzle solution I made with Python in fasmg.
Code: ; fasmg puzzle: ; find the shortest list of numbers from 1 to 13 ; where each number is adjacent to every other number ; ; this also shows how to use ; sets of integers and lists of integers with many elements ; ; solution by Grom PE ; ; benchmark: ; equ list, max 180 = 2.1 seconds ; equ list, max 200 = out of memory ; dword list, max 180 = 0.2 seconds ; dword list, max 200 = 0.2 seconds ; dword list, max 2000 = 24.3 seconds ; dword list, max 4000 = 112.6 seconds (12 million elements) macro display_int value temp = value if temp<0 display '' temp = temp end if virtual at 0 if temp=0 db '0' end if while temp>0 d = '0' + temp mod 10 db d temp = temp / 10 end while repeat $ load d byte from $% display d end repeat end virtual end macro macro set_count b result = 0 if b rept bsr bbsf b+1, n:bsf b if b and (1 shl n) result = result + 1 end if end rept end if end macro macro set_pop b result = 0 if b result = bsf b + 1 b = b and not (1 shl (result1)) else err 'the set is empty' result = 1 end if end macro max = 13 virtual at 0 rd max/2 l: dd 1 ; starting list: [1] lap = l ; first element pointer la = 1 ; first element value lz = 1 ; last element value rept max1,n:2 ; add numbers 2 ... max s = 1 shl (n1)  1 ; set that contains numbers 1 to (n1) if ~ n mod 2 ; if n is even ; exclude first and last list elements from the set s = s and not (1 shl (la1)) and not (1 shl (lz1)) ; extend list from its start lap = lap  4 store n:dword at lap la = n end if set_count s if result mod 2 ; if set's length is odd set_pop s ; append to list dd result,n lz = n end if if s and (1 shl (la1)) ; if set contains first list element ; exclude first and last list elements from the set s = s and not (1 shl (la1)) and not (1 shl (lz1)) dd n,la lz = la end if while s set_pop s dd result,n set_pop s dd result lz = result end while end rept lzp: ; display the list p = lap while p < lzp load x dword from p if %>1 display ' ' end if display_int x p = p + 4 end while end virtual Attached same code for convenience.


21 Sep 2016, 12:07 

< Last Thread  Next Thread > 
Forum Rules:

Copyright © 19992023, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.
Website powered by rwasa.