Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2012
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/tclsh
  2. namespace path {::tcl::mathop ::tcl::mathfunc}
  3.  
  4. proc lsum L {expr [join $L +]+0}
  5. proc random range {expr int(rand()*$range)}
  6.  
  7. proc genAnsList {n_max lPair lAns} {
  8.  upvar $lPair lPair1
  9.  upvar $lAns lAns1
  10.  set lPair {{0 0}}
  11.  set lPair1 {{0 0}}
  12.  
  13.  for {set i 1} {[<= $i $n_max]} {incr i} {
  14.   set q [/ [+ [sqrt 5] 1 ] 2]
  15.   set a [int [* $q $i]]
  16.   set b [+ $a $i]
  17.   lappend lPair1 "$a $b"
  18.   lappend lPair "$a $b"
  19.   lappend lPair "$b $a"
  20.  }
  21.  
  22.  set lPairSorted [lsort -integer -index 0 $lPair]
  23.  for {set i 0} {[<= $i $n_max]} {incr i} {
  24.   lappend lAns1 [lindex [lindex $lPairSorted $i] 1]
  25.  }
  26. }
  27.  
  28. proc turn {st lAns lPair} {
  29.  set st_cur_sum [lsum $st]
  30.  if {[== 0 $st_cur_sum]} {return {you win}}
  31.  
  32.  set a [lindex $st 0]
  33.  set b [lindex $st 1]
  34.  set c [abs [- $a $b]]
  35.  
  36.  set a1 [min $a [lindex $lAns $b]]
  37.  set b1 [min $b [lindex $lAns $a]]
  38.  set cPair1 [lindex $lPair $c]
  39.  
  40.  set a2 [random $a]
  41.  set b2 [random $b]
  42.  set c2 [random [+ 1 [min $a $b]]]
  43.  set cPair2 [list [- $a $c2] [- $b $c2]]
  44.  
  45.  set st_next1 [list "$a $b1" [+ $a $b1]]
  46.  set st_next2 [list "$a1 $b" [+ $a1 $b]]
  47.  set st_next3 [list "$cPair1" [lsum $cPair1]]
  48.  set lst_next [list $st_next1 $st_next2 $st_next3]
  49.  set st_next [lindex [lsort -integer -index 1 $lst_next] 0]
  50.  set st_next_sum [lindex $st_next 1]
  51.  
  52.  if {[== 0 $st_next_sum]} {return {i won}}
  53.  
  54.  if {[> $st_cur_sum  $st_next_sum]} {
  55.    return [lindex $st_next 0]
  56.  } else {
  57.   switch [random 3] {
  58.    0 {return "$a $b2"}
  59.    1 {return "$a2 $b"}
  60.    2 {return "$cPair2"}
  61.   }
  62.  }
  63. }
  64.  
  65. puts {Enter heap sizes:}
  66. if {[< 0 [gets stdin state]]} {
  67.  set n_max [lindex [lsort -integer $state] end]
  68.  genAnsList $n_max lPair lAns
  69.  set state [turn $state $lAns $lPair]
  70.  puts $state
  71.  
  72.  while {[< 0 [gets stdin state]]} {
  73.   set state [turn $state $lAns $lPair]
  74.   puts $state
  75.  }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement