SHARE
TWEET

Untitled

a guest Dec 5th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "strings"
  6. )
  7.  
  8. const TargetSize int = 1000
  9. const Seed string = "W"
  10. var currentToken rune
  11. var currentTokenIndex int
  12. var currentExpression string
  13. func main() {
  14.     replacements := []string{"F", "T", "A", "B", "C", "W=W", "W>W", "W&W", "W+W", "~W", "(W)"}
  15.     queue := []string{Seed}
  16.     wffs := make([]string, 0)
  17.  
  18.     numberGenerated := 0
  19.     for len(queue) > 0 && numberGenerated < TargetSize {
  20.         // take the first off the queue
  21.         candidate := queue[0]
  22.  
  23.         // find the first W
  24.         for index, character := range candidate {
  25.             if character == 'W' {
  26.                 // replace W with each replacement
  27.                 for _, replacement := range replacements {
  28.                     candidate := candidate[:index] + replacement + candidate[index+1:]
  29.  
  30.                     // if there is more to be replaced, put it back on the queue
  31.                     if strings.Contains(candidate, "W") {
  32.                         queue = append(queue, candidate)
  33.                     } else if isUnique(&wffs, candidate) {
  34.                         wffs = append(wffs, candidate)
  35.  
  36.                         // replace A's and B's with T's and F's
  37.                         var expression1 string
  38.                         expression1 = strings.Replace(candidate, "A", "T", -1)
  39.                         expression1 = strings.Replace(expression1, "B", "T", -1)
  40.  
  41.                         var expression2 string
  42.                         expression2 = strings.Replace(candidate, "A", "T", -1)
  43.                         expression2 = strings.Replace(expression2, "B", "F", -1)
  44.  
  45.                         var expression3 string
  46.                         expression3 = strings.Replace(candidate, "A", "F", -1)
  47.                         expression3 = strings.Replace(expression3, "B", "T", -1)
  48.  
  49.                         var expression4 string
  50.                         expression4 = strings.Replace(candidate, "A", "F", -1)
  51.                         expression4 = strings.Replace(expression4, "B", "F", -1)
  52.  
  53.                         // parse each and check if a tautology
  54.                         if isTrue(expression1) && isTrue(expression2) && isTrue(expression3) && isTrue(expression4) {
  55.                             numberGenerated++
  56.                             fmt.Print(candidate, "\n")
  57.                         }
  58.                     }
  59.                 }
  60.                 queue = queue[1:] // remove the first element of the queue
  61.                 break
  62.             }
  63.         }
  64.  
  65.  
  66.     }
  67.     fmt.Print("Queue was ", len(queue), " elements long\n")
  68. }
  69.  
  70. func isUnique(list *[]string, formula string) bool {
  71.     // if already in the list
  72.     for _, val := range *list {
  73.         if val == formula {
  74.             return false;
  75.         }
  76.     }
  77.  
  78.     aFound := false
  79.     bFound := false
  80.     for _, character := range formula {
  81.         if character == 'A' {
  82.             aFound = true
  83.         }
  84.  
  85.         if character == 'B' {
  86.             bFound = true
  87.         }
  88.  
  89.         // if there is a B without an A
  90.         if character == 'B' && !aFound {
  91.             return false;
  92.         }
  93.  
  94.         // if there is a C without a B
  95.         if character == 'C' && !bFound {
  96.             return false;
  97.         }
  98.     }
  99.  
  100.     return true
  101. }
  102.  
  103. func isTrue(candidate string) bool {
  104.     currentExpression = candidate
  105.     currentTokenIndex = 0
  106.     currentToken = rune(currentExpression[currentTokenIndex])
  107.     return B()
  108. }
  109.  
  110. func getNextToken() {
  111.     currentToken = rune(currentExpression[currentTokenIndex+1])
  112. }
  113.  
  114. func peekNextToken() rune {
  115.     return rune(currentExpression[currentTokenIndex+1])
  116. }
  117.  
  118. func B() bool {
  119.     return E()
  120. }
  121.  
  122. func E() bool {
  123.     temp := I()
  124.     return EPrime(temp)
  125. }
  126.  
  127. func EPrime(incoming bool) bool {
  128.     if currentToken == '=' {
  129.         getNextToken();
  130.         temp := I();
  131.         return incoming == temp;
  132.     }
  133.     return incoming;
  134. }
  135.  
  136. func I() bool {
  137.     temp := D()
  138.     return IPrime(temp)
  139. }
  140.  
  141. func IPrime(incoming bool) bool {
  142.     if currentToken == '>' {
  143.         getNextToken()
  144.         temp := D()
  145.         return incoming == temp;
  146.     }
  147.     return incoming
  148. }
  149.  
  150. func D() bool {
  151.     temp := C()
  152.     return DPrime(temp)
  153. }
  154.  
  155. func DPrime(incoming bool) bool {
  156.     if currentToken == '+' {
  157.         getNextToken()
  158.         temp := C()
  159.         return incoming == temp
  160.     }
  161.     return incoming
  162. }
  163.  
  164. func C() bool {
  165.     temp := A()
  166.     return CPrime(temp)
  167. }
  168.  
  169. func CPrime(incoming bool) bool {
  170.     if currentToken == '&' {
  171.         getNextToken()
  172.         temp := A()
  173.         return incoming == temp
  174.     }
  175.     return incoming
  176. }
  177.  
  178. func A() bool {
  179.     if currentToken == '~' {
  180.         getNextToken()
  181.         temp := E()
  182.         return !temp
  183.     } else if currentToken == 'T' {
  184.         return true
  185.     } else if currentToken == 'F' {
  186.         return false
  187.     } else if currentToken == '(' {
  188.         getNextToken()
  189.         temp := B()
  190.         if peekNextToken() == ')' {
  191.             getNextToken()
  192.             return temp
  193.         }
  194.     }
  195.     return false
  196. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top