|
| 1 | +This file describes the path that the Piet program takes in a more human readable form. |
| 2 | +Two functions are implemented: |
| 3 | +- GCD with subtraction |
| 4 | +- GCD with the modulo operator |
| 5 | + |
| 6 | +------------------- |
| 7 | +SUBTRACTION |
| 8 | +------------------- |
| 9 | + |
| 10 | +Pseudo code: |
| 11 | + |
| 12 | +function gcd(a, b) |
| 13 | + while a ≠ b |
| 14 | + if a > b |
| 15 | + a := a − b; |
| 16 | + else |
| 17 | + b := b − a; |
| 18 | + return a; |
| 19 | + |
| 20 | + |
| 21 | +Piet code: |
| 22 | + |
| 23 | +COMMAND STATE OF STACK |
| 24 | +in(number) A // Take A as an input |
| 25 | +duplicate AA // Start to take the absolute value of A |
| 26 | +push 1 1AA |
| 27 | +duplicate 11AA |
| 28 | +subtract 0AA |
| 29 | +greater 0/1A // 1 if A > 0, 0 if A <= 0 |
| 30 | +not 1/0A // 0 if A > 0, 1 if A <= 0 |
| 31 | +push 1 1 1/0 A |
| 32 | +push 3 31 1/0 A |
| 33 | +subtract -2 1/0 A |
| 34 | +multiply -2/0 A |
| 35 | +push 1 1 -2/0 A |
| 36 | +add -1/1 A |
| 37 | +multiply A // A should now be an absolute value |
| 38 | + |
| 39 | +in(number) BA // Take B as an input |
| 40 | +duplicate BBA // Start to take the absolute value of B |
| 41 | +push 1 1BBA |
| 42 | +duplicate 11BBA |
| 43 | +subtract 0BBA |
| 44 | +greater 0/1BA // 1 if B > 0, 0 if B <= 0 |
| 45 | +not 1/0BA // 0 if B > 0, 1 if B <= 0 |
| 46 | +push 1 1 1/0 BA |
| 47 | +push 3 31 1/0 BA |
| 48 | +subtract -2 1/0 BA |
| 49 | +multiply -2/0 BA |
| 50 | +push 1 1 -2/0 BA |
| 51 | +add -1/1 BA |
| 52 | +multiply BA // B should now be an absolute value |
| 53 | + |
| 54 | +// Start of the main loop while a ≠ b |
| 55 | +duplicate BBA |
| 56 | +push 3 3BBA |
| 57 | +push 2 23BBA |
| 58 | +roll ABB |
| 59 | +duplicate AABB |
| 60 | +push 4 4AABB |
| 61 | +push 1 14AABB |
| 62 | +roll ABBA |
| 63 | +subtract 0/x BA |
| 64 | +not 1/0 BA // 1 if a = b and 0 if a ≠ b |
| 65 | +not 0/1 BA // 1 if a ≠ b and 0 if a = b |
| 66 | +pointer BA // If a ≠ b, the DP should change one clockwise, otherwise, go straight ahead. |
| 67 | + |
| 68 | + // Go left if a ≠ b (DP changed one clockwise) |
| 69 | + duplicate BBA |
| 70 | + push 3 3BBA |
| 71 | + push 2 23BBA |
| 72 | + roll ABB |
| 73 | + duplicate AABB |
| 74 | + push 4 4AABB |
| 75 | + push 1 14AABB |
| 76 | + roll ABBA |
| 77 | + push 2 2ABBA |
| 78 | + push 1 12ABBA |
| 79 | + roll BABA |
| 80 | + greater 0/1 BA // A > B; 1 if true; 0 if false |
| 81 | + pointer BA // If A > B, DP goes one clockwise, otherwise, DP stays the same. |
| 82 | + |
| 83 | + // If A > B (DP has changed 1 clockwise) |
| 84 | + duplicate BBA |
| 85 | + push 3 3BBA |
| 86 | + push 1 13BBA |
| 87 | + roll BAB |
| 88 | + subtract AB // A = A - B |
| 89 | + push 2 2AB |
| 90 | + push 1 12AB |
| 91 | + roll BA |
| 92 | + // Go back to start of loop |
| 93 | + |
| 94 | + // If B > A (DP stayed the same) |
| 95 | + push 2 2BA |
| 96 | + push 1 12BA |
| 97 | + roll AB |
| 98 | + duplicate AAB |
| 99 | + push 3 3AAB |
| 100 | + push 1 13AAB |
| 101 | + roll ABA |
| 102 | + subtract BA // B = B - A |
| 103 | + // Go back to start of loop |
| 104 | + |
| 105 | +// Go down if a = b (end of while loop) |
| 106 | +pop A |
| 107 | +out(number) - // Print out A when done. |
| 108 | + |
| 109 | +--------------------------------------------------------------------------- |
| 110 | + |
| 111 | +------------------- |
| 112 | +MODULO |
| 113 | +------------------- |
| 114 | + |
| 115 | +Pseudo code: |
| 116 | + |
| 117 | +function gcd(a, b) |
| 118 | + while b ≠ 0 |
| 119 | + t := b; |
| 120 | + b := a mod b; |
| 121 | + a := t; |
| 122 | + return a; |
| 123 | + |
| 124 | +Piet code: |
| 125 | + |
| 126 | +COMMAND STATE OF STACK |
| 127 | +in(number) A |
| 128 | +in(number) BA |
| 129 | + |
| 130 | +// Start of loop |
| 131 | +duplicate BBA |
| 132 | +not 0/1 BA |
| 133 | +not 1/0 BA |
| 134 | +pointer BA |
| 135 | + |
| 136 | + // Go down if b ≠ 0 |
| 137 | + duplicate TBA |
| 138 | + push 3 3TBA |
| 139 | + push 1 13TBA |
| 140 | + roll BAT |
| 141 | + mod BA // b = a mod b; a = t |
| 142 | + // Go back to the start of the loop |
| 143 | + |
| 144 | +// Go right if b = 0 |
| 145 | +pop A |
| 146 | +out(number) - // Print out A when done. |
0 commit comments