# Variable calculations should be done in this file.
# It is read and interpreted by wims for all valid calls to the module.

wims_print_precision=16
!if $cmd=reply and $status!=waiting
 error=double_reply
 !exit
!endif

now=$wims_nowseconds
laps=$[$now-$start]
remain=$[max(0,$allow-$laps)]

!if $cmd=reply and $status=waiting
 !if $abandon=yes
  module_score=0
  wims_module_log=Abandon
  status=done
  !exit
 !endif
 tot=$[$cols*$rows]
 !for i=1 to $tot
  !bound x$i between integer 1 and $tot default $
 !next i
 got=
 !for i=1 to $tot
  !if $(x$i)=$empty
   error=empty_data
   !exit
  !endif
  !if $(x$i) iswordof $got
   e=$(x$i)
   error=double_number
   !advance badrep
   !exit
  !endif
  got=$got $(x$i)
 !next i 
 !if $kind=sum
  !distribute item +,0 into sign,beg
 !else
  !distribute item *,1 into sign,beg
 !endif
 badcols=
 !for c=1 to $cols
  test=$beg
  !for r=1 to $rows
   test=$[$test$sign$(x$[$cols*($r-1)+$c])]
  !next r
  !ifval $test!=$(colsum$c)
   badcols=$badcols $c
   colgot$c=$test
  !endif
 !next c
 badrows=
 !for r=1 to $rows
  test=$beg
  !for c=1 to $cols
   test=$[$test$sign$(x$[$cols*($r-1)+$c])]
  !next c
  !ifval $test!=$(rowsum$r)
   badrows=$badrows $r
   rowgot$r=$test
  !endif
 !next r
 !if $badcols$badrows!=$empty
  error=bad_sum
  !advance badrep
  !exit
 !endif

 status=done
 ta=$[min(1,(($allow+5)/$laps)^2)]
 wims_print_precision=6
 module_score=$[round(100*$ta/(1+$badrep))/10]
 wims_print_precision=16
 wims_module_log=score $module_score/10
!endif

