#101674 - 2003-05-30 09:21 PM
Re: KIXgolf: MOAN(), Part II
|
Sealeopard
KiX Master
Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
|
The EXECUTE line has been broken for readability. To run this UDF, you will have to unbreak the long line.
code:
; begin MOAN ;
;! Function MOAN($a,$b,$c) dim $, $z, $r, $1, $2, $3, $4
if $a+$b=$c exit endif
if not isdeclared($n) global $n, $m[32], $s ; create 32bits of zeros and ones $n=$m $m=join($m,0) $n=join($n,1) $s='.' endif
; create a function Z for validation and a modified binaryip function Y ; that is able to convert to/from binary ip addresses $=execute("function z($a) dim $ $a=split($a,$s) $z=ubound($a) $=$a[0] select case $z=0 and $='' $z='' case $z=3 $z=y($a) case $z=0 and 0<$ and 31>$ $z=left($n,$)+left($m,32-$) case 1 $z=a endselect endfunction function y($i) dim $, $b, $c, $a if ubound($i)+1 $a='' for each $ in $i if 0<$ or 255>$ $b=128 while $b if val($) & $b $a=$a+1 else $a=$a+0 endif $b=$b/2 loop endif next else redim $a[3] for $=0 to 3 $b=128 for $c=1 to 8 $a[$]=($b*substr($i,$*8+$c,1))+$a[$] $b=$b/2 next next $a=join($a,$s) endif $y=$a endfunction")
; validating the subnetmask $c=z($c) ; validating the ip address $a=z($a) ; validating the networkid $b=z($b)
; exit if incorrect formatting if $a=a or $b=a or $c=a exit endif
; z is being used in two placed ; to calculate networkid and to check whether ip is in network defined by networkid and subnetmask $z='' for $=1 to 32 $z=$z+(val(substr($a,$,1))&val(substr($c,$,1))) next
select case $a='' ; return array of ips based on networkid and subnetmask $b=left($b,31)+1 $=instrrev($c,'1') $=split(y(left($b,$)+substr($n,$+2)+0),$s) $b=split(y($b),$s) for $1=$b[0] to $[0] for $2=$b[1] to $[1] for $3=$b[2] to $[2] for $4=$b[3] to $[3] $r=ubound($a)+1 redim preserve $a[$r] $a[$r]=''+$1+$s+$2+$s+$3+$s+$4 next next next next $r=join($a,',') case $b='' ; return networkid based on ip and subnetmask ; ip AND subnetmask = networkid if $z<>$a $r=y($z) endif case $c='' ; return subnetmask based on ip and networkid ; ip AND networkid = subnetmask $=instrrev($b,'1') if left($a,$)=left($b,$) for $r=$ to 30 $c=$c+'/'+$r+',' next $c=left($c,len($c)-1) else $c='' endif $r=$c case $a and $b and $c ; check whether ip is part of network defined by networkid and subnetmask $r=$z=$b case 1 ; at least two empty inputs endselect $moan=$r EndFunction ;! ;!
; end MOAN
And here's the results:code:
KiXtart KiXtart Version = 4.21 KiXGolf Script = kixgolf_moan.KIX
Computer OS = Windows 2000 Professional CPU = Intel Pentium III Speed = 448 MHz Memory = 448 MB
KiXGolf Scoring Engine Scoring Engine = 3.0.3
KiXtart Golf Score Tournament = KiXtart Golf: MOAN - Mother Of All Networks Processing Start = 2003/05/30 15:00:27.791 Processing End = 2003/05/30 15:00:27.991 Duration = 0000/00/00 00:00:00.200 # Loops = 1 KiXGolf Result = passed (9 out of 9 correct) KiXGolf Score = 1265 Thank you for participating in KiXtart Golf!
[ 30. May 2003, 21:39: Message edited by: sealeopard ]
_________________________
There are two types of vessels, submarines and targets.
|
Top
|
|
|
|
#101675 - 2003-05-30 09:26 PM
Re: KIXgolf: MOAN(), Part II
|
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
code:
; begin MOAN ; ;! Function moan($a,$b,$c) Dim $,$e,$f,$g,$h,$m[32]
; Convert all input data to numeric array elements. ; The array is 12 elements each element is value (0-255). ;'$' hold the input data and later is the array version of the input data
;Build Subnet Mask ARARRY $f=0,128,192,224,240,248,252,254,255 for $e=8 to 31 Dim $h $=$e for $g=1 to 4 $h=$h+iif($/8,"255",$f[$ mod 8])+iif($g<4,".","") $=iif($>8,$-8,0) next $m[$e]=$h next ;End build mask array
$h=$c if $c ;Convert mask bits to 1.2.3.4 format if len($c)<3 $c=$m[$c] endif ; End convert bits else $c="..." endif
;build array of input values by splitting a string. Then convert to integers ;IPaddr = $[0..3] NetID = $[4..7] Mask = $[8..11] $="..." $=split(IIF($a,$a,$)+"."+IIF($b,$b,$)+"."+$c,".") for $e=0 to 11 $[$e]=0+$[$e] next ;End build array
if $a and $h ;Output: Network ID or empty string if no match
;Calculate NetworkID using $ array $e="" for $c=0 to 3 $e=$e+"."+($[$c] & $[$c+8]) next $=substr($e,2)
if join(split($a),"")=$ $="" endif
;Output: 1 if the IP is a member of a nework defined by network ID and subnet mask, otherwise 0 ;Input NetworkID = Calculated NetworkID if $b $=IIF(join(split($b),"")=$,1,0) endif else
;Output: Array of all potential subnet masks or empty string if no match if $a and $b $a="" for $f=8 to 31 $b=1 for $e=0 to 3 $b=$b & $[$e+4]=($[$e] & 0+split($m[$f],".")[$e]) next if $b $a=$a+" /"+$f endif next
$=IIF($a,split(substr($a,2)),"") else ;Output: Array of all potential IP addresses or empty string if no match $a="" for $b=$[4] to $[4] + 255 - $[8] for $c=$[5] to $[5] + 255 - $[9] for $e=$[6] to $[6] + 255 - $[10] for $f=$[7]+1 to $[7] + 254 - $[11] $a = $a+" "+$b+"."+$c+"."+$e+"."+$f next next next next $=IIF($a,split(substr($a,2)),"") endif endif
$moan=$ EndFunction ;! ;!
; end MOAN
code:
KiXtart KiXtart Version = 4.21 KiXGolf Script = kixgolf_moan.kix
Computer OS = Windows 2000 Professional CPU = Intel Pentium III Speed = 848 MHz Memory = 512 MB
KiXGolf Scoring Engine Scoring Engine = 3.0.3
KiXtart Golf Score Tournament = KiXtart Golf: MOAN - Mother Of All Networks Processing Start = 2003/05/30 15:30:53.505 Processing End = 2003/05/30 15:30:53.595 Duration = 0000/00/00 00:00:00.090 # Loops = 1 KiXGolf Result = passed (10 out of 10 correct) KiXGolf Score = 836 Thank you for participating in KiXtart Golf!
[ 30. May 2003, 21:27: Message edited by: Howard Bullock ]
|
Top
|
|
|
|
#101676 - 2003-05-30 10:13 PM
Re: KIXgolf: MOAN(), Part II
|
MightyR1
MM club member
Registered: 1999-09-09
Posts: 1264
Loc: The Netherlands
|
Here's mine, result=1033.
Passes the test, but not useable for determining class A / B ip numbers...
code:
; begin MOAN ; ;! Function MOAN($i,$n,$s) Dim $d,$e,$f,$x,$y,$,$t,$m $d=Split('0 0 0 0') $e=Split(' ') $f=Split('0 128 192 224 240 248 252 254')
$i=Split($i,'.') $n=Split($n,'.')
If $s If InStr($s,'.') $s=Split($s,'.') Else $s=Val($s) $x=$s/8 $y=$s mod 8 $s=$d For $=0 to $x-1 $s[$]=255 Next If $y $s[$x]=$f[$y] EndIf EndIf If $s[3]>252 $s=$e EndIf Else $s=$e EndIf
$m=''
$t=$d $x=0 If $i[0] AND $s[0] For $=0 to 3 $t[$]=Val(LTrim($i[$])) & Val(LTrim($s[$])) Next If $n[0] ;InRange If Join($t)=Join($n) $m=1 Else $m=0 EndIf Else ;NetID $m=Join($t,'.') EndIf
Else
If $i[0] AND $n[0] ;Subnets For $=0 to 3 If Val(LTrim($i[$]))=Val(LTrim($n[$])) $t[$]=255 $x=1+$x Else $=3 EndIf Next
If $x < 4 For $=0 to 7 If (Val(LTrim($i[$x])) & Val($f[$])) = Val(LTrim($n[$x])) $t[$x]=$f[$] $m=$m+Join($t,'.')+',' EndIf Next $m=SubStr($m,1,Len($m)-1) EndIf EndIf
If $n[0] AND $s[0] ;IPs For $=0 to 3 If Val(LTrim($s[$]))=255 $t[$]=Val(LTrim($n[$])) $x=1+$x EndIf Next $y='' For $=0 to 255 If (Val(LTrim($s[$x])) & $) = Val(LTrim($n[$x])) $y=$y+$+' ' EndIf Next $y=Split(SubStr($y,1,Len($y)-1))
If $x=3 For $=1+$y[0] to Val($y[Ubound($y)])-1 $t[3]=$ $m=$m+Join($t,'.')+',' Next $m=SubStr($m,1,Len($m)-1) EndIf EndIf EndIf
$moan=$m
EndFunction
;! ;!
; end MOAN
_________________________
Greetz, Patrick Rutten
- We'll either find a way or make one... - Knowledge is power; knowing how to find it is more powerful... - Problems don't exist; they are challenges...
|
Top
|
|
|
|
#101678 - 2003-05-31 02:07 AM
Re: KIXgolf: MOAN(), Part II
|
Lonkero
KiX Master Guru
Registered: 2001-06-05
Posts: 22346
Loc: OK
|
hoby, I saw right away a way to make your udf shorter than mine. anyway, will not crack it before posting my code, thus:
code:
Function MOAN($a, $n, $m) dim $i,$s,$,$!,$z
for each $! in split($a+"."+$n+"."+$m,".") $=256 do $=$/2 if 0<=0+$!-$ $z=$z+"1" $!=-$+$! else $z=$z+"0" endif until 1=$ next $i=left($z,32) $s=substr($z,33,32)
if 2<len($m) $m=instr(right($z,32),0)-1 endif select
case 0=$m for $=instrrev($s,1) to 31 if moan($a,$n,$) $m=$m+" /"+$ endif next $=substr($m,2)
case 0=$n $s=0 for $!=1 to $m if 2>$s $s=256 $n=$n+$+"." $=0 endif $s=$s/2 if 1=substr($i,$!,1) $=$+$s endif next $=iif($m>30,"",substr($n+$,3))
case 0=$a $!=0,128,192,224,240,248,252,254,255 $=$! $n=split($n,".") for $s=0 to 3 $i=-8*$s+$m $i=iif($i>8,8,$i) $[$s]=255+$n[$s]-iif($i,$![$i],0) next for $s=$n[0] to $[0] for $!=$n[1] to $[1] for $m=$n[2] to $[2] for $i=1+$n[3] to $[3]-1 $a=$a+" "+$s+"."+$!+"."+$m+"."+$i next next next next $=substr($a,2)
case 1 $=iif(left($i,$m)=left($s,$m) & $i=>$s ,1,0) ;and instr(right($i,32-$m),0),1,0) ; x.x.x.255 proper ip? endselect $moan=iif(instr($," "),split($),$)
EndFunction
this comes to 807. added one stroke to remove a problem with special subnet-masks. it probably still has one... at least I feel like it does
_________________________
!download KiXnet
|
Top
|
|
|
|
#101679 - 2003-05-31 02:33 AM
Re: KIXgolf: MOAN(), Part II
|
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
You are a better programmer than I. I stared at the code for 30 minutes and did not see anything. I know there has to be something, but these eyes just see it.
|
Top
|
|
|
|
#101683 - 2003-05-31 06:06 PM
Re: KIXgolf: MOAN(), Part II
|
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Go to it guys. I don't have a lot of time today. I woudl be interested in seeing your ideas.
|
Top
|
|
|
|
#101685 - 2003-05-31 07:58 PM
Re: KIXgolf: MOAN(), Part II
|
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
I just copied the program from the board and it ran just fine.
|
Top
|
|
|
|
#101687 - 2003-05-31 08:20 PM
Re: KIXgolf: MOAN(), Part II
|
Lonkero
KiX Master Guru
Registered: 2001-06-05
Posts: 22346
Loc: OK
|
k, hoby...
code:
; begin MOAN ; ;! Function moan($a,$b,$c) Dim $,$e,$f,$g,$h,$m[32]
; Convert all input data to numeric array elements. ; The array is 12 elements each element is value (0-255). ;'$' hold the input data and later is the array version of the input data
;Build Subnet Mask ARARRY $f=0,128,192,224,240,248,252,254,255 $h=$c for $e=8 to 31 ; Dim $h $=$e for $g=1 to 4 $m[$e]=$m[$e]+iif($/8,"255",$f[$ mod 8])+iif($g<4,".","") $=iif($>8,$-8,0) next ; $m[$e]=$h if $e=$c $c=$m[$e] endif next ;End build mask array
;Convert mask bits to 1.2.3.4 format $c=iif($c,$c,"...")
;build array of input values by splitting a string. Then convert to integers ;IPaddr = $[0..3] NetID = $[4..7] Mask = $[8..11] $="..." $=split(IIF($a,$a,$)+"."+IIF($b,$b,$)+"."+$c,".") for $e=0 to 11 $[$e]=0+$[$e] next ;End build array
if $a and $h ;Output: Network ID or empty string if no match
;Calculate NetworkID using $ array $e="" for $c=0 to 3 $e=$e+"."+($[$c] & $[$c+8]) next $=substr($e,2)
if join(split($a),"")=$ $="" endif
;Output: 1 if the IP is a member of a nework defined by network ID and subnet mask, otherwise 0 ;Input NetworkID = Calculated NetworkID if $b $=IIF(join(split($b),"")=$,1,0) endif else
;Output: Array of all potential subnet masks or empty string if no match if $a and $b $a="" for $f=8 to 31 $b=1 for $e=0 to 3 $b=$b & $[$e+4]=($[$e] & 0+split($m[$f],".")[$e]) next if $b $a=$a+" /"+$f endif next
$=IIF($a,split(substr($a,2)),"") else ;Output: Array of all potential IP addresses or empty string if no match $a="" for $b=$[4] to $[4] + 255 - $[8] for $c=$[5] to $[5] + 255 - $[9] for $e=$[6] to $[6] + 255 - $[10] for $f=$[7]+1 to $[7] + 254 - $[11] $a = $a+" "+$b+"."+$c+"."+$e+"."+$f next next next next $=IIF($a,split(substr($a,2)),"") endif endif
$moan=$ EndFunction ;! ;!
; end MOAN
there is a start for you that brings it to 824... I bet we can make your code shorter than mine
_________________________
!download KiXnet
|
Top
|
|
|
|
#101688 - 2003-05-31 08:42 PM
Re: KIXgolf: MOAN(), Part II
|
Lonkero
KiX Master Guru
Registered: 2001-06-05
Posts: 22346
Loc: OK
|
still silent? 811 with your code: {edit} well, 807... won't take the bet yet?
code:
; begin MOAN ; ;! Function moan($a,$b,$c) Dim $,$e,$f,$g,$m[32]
; Convert all input data to numeric array elements. ; The array is 12 elements each element is value (0-255). ;'$' hold the input data and later is the array version of the input data
;Build Subnet Mask ARARRY $f=0,128,192,224,240,248,252,254,255 for $e=8 to 31 $=$e for $g=1 to 4 $m[$e]=$m[$e]+iif($/8,"255",$f[$ mod 8])+iif($g<4,".","") $=iif($>8,$-8,0) next if $e=$c $c=$m[$e] endif next ;End build mask array
;build array of input values by splitting a string. Then convert to integers ;IPaddr = $[0..3] NetID = $[4..7] Mask = $[8..11] $="..." $=split(IIF($a,$a,$)+"."+IIF($b,$b,$)+"."+iif($c,$c,$),".") for $e=0 to 11 $[$e]=0+$[$e] next ;End build array
if $a and $c ;Output: Network ID or empty string if no match
;Calculate NetworkID using $ array $e="" for $c=0 to 3 $e=$e+"."+($[$c] & $[$c+8]) next $=substr($e,2)
if join(split($a),"")=$ $="" endif
;Output: 1 if the IP is a member of a nework defined by network ID and subnet mask, otherwise 0 ;Input NetworkID = Calculated NetworkID if $b $=IIF(join(split($b),"")=$,1,0) endif else
;Output: Array of all potential subnet masks or empty string if no match if $a and $b $a="" for $f=8 to 31 $b=1 for $e=0 to 3 $b=$b & $[$e+4]=($[$e] & 0+split($m[$f],".")[$e]) next if $b $a=$a+" /"+$f endif next
$=IIF($a,split(substr($a,2)),"") else ;Output: Array of all potential IP addresses or empty string if no match $a="" for $b=$[4] to $[4] + 255 - $[8] for $c=$[5] to $[5] + 255 - $[9] for $e=$[6] to $[6] + 255 - $[10] for $f=$[7]+1 to $[7] + 254 - $[11] $a = $a+" "+$b+"."+$c+"."+$e+"."+$f next next next next $=IIF($a,split(substr($a,2)),"") endif endif
$moan=$ EndFunction ;! ;!
; end MOAN
[ 31. May 2003, 20:46: Message edited by: Lonkero ]
_________________________
!download KiXnet
|
Top
|
|
|
|
#101689 - 2003-05-31 08:46 PM
Re: KIXgolf: MOAN(), Part II
|
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Doh!
I can't believe I missed that.
|
Top
|
|
|
|
#101690 - 2003-05-31 08:59 PM
Re: KIXgolf: MOAN(), Part II
|
Lonkero
KiX Master Guru
Registered: 2001-06-05
Posts: 22346
Loc: OK
|
k, 775 with your code. please fight back!
code:
; begin MOAN ; ;! Function moan($a,$b,$c) Dim $,$e,$f,$g,$m[32]
; Convert all input data to numeric array elements. ; The array is 12 elements each element is value (0-255). ;'$' hold the input data and later is the array version of the input data
;Build Subnet Mask ARARRY $f=0,128,192,224,240,248,252,254,255 for $e=8 to 31 $=$e for $g=1 to 4 $m[$e]=$m[$e]+iif($/8,"255",$f[$ mod 8])+iif($g<4,".","") $=iif($>8,$-8,0) next if $e=$c $c=$m[$e] endif next ;End build mask array
;build array of input values by splitting a string. Then convert to integers ;IPaddr = $[0..3] NetID = $[4..7] Mask = $[8..11] $="..." $=split(IIF($a,$a,$)+"."+IIF($b,$b,$)+"."+iif($c,$c,$),".") for $e=0 to 11 $[$e]=0+$[$e] next ;End build array
if $a and $c ;Output: Network ID or empty string if no match
;Calculate NetworkID using $ array $e="" for $c=0 to 3 $e=$e+"."+($[$c] & $[$c+8]) next $=substr($e,2)
if join(split($a),"")=$ $="" endif
;Output: 1 if the IP is a member of a nework defined by network ID and subnet mask, otherwise 0 ;Input NetworkID = Calculated NetworkID if $b $=IIF(join(split($b),"")=$,1,0) endif else
;Output: Array of all potential subnet masks or empty string if no match if $a and $b $a="" for $f=8 to 31 $b=1 for $e=0 to 3 $b=$b & $[$e+4]=($[$e] & 0+split($m[$f],".")[$e]) next if $b $a=$a+" /"+$f endif next
else ;Output: Array of all potential IP addresses or empty string if no match $a="" for $b=$[4] to $[4] + 255 - $[8] for $c=$[5] to $[5] + 255 - $[9] for $e=$[6] to $[6] + 255 - $[10] for $f=$[7]+1 to $[7] + 254 - $[11] $a = $a+" "+$b+"."+$c+"."+$e+"."+$f next next next next endif $=IIF($a,split(substr($a,2)),"") endif $moan=$ EndFunction ;! ;!
; end MOAN
_________________________
!download KiXnet
|
Top
|
|
|
|
#101692 - 2003-05-31 09:13 PM
Re: KIXgolf: MOAN(), Part II
|
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Very good coding Jooel. Don't know why I did not see some of those good optimizations. Good job so far. I now have a few minutes to see if I can regain ownership of my code and best score.
|
Top
|
|
|
|
Moderator: Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 208 anonymous users online.
|
|
|