Code golf this function to be a lot shorter. You only get points if it's <300 bytes (not counting whitespace). Keep the name `stepper`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | void stepper(int xw) { int Steps = 0; for (int x = 0; x < xw; x++) { switch (Steps) { case 0: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 1); break; case 1: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 1); digitalWrite(4, 1); break; case 2: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 1); digitalWrite(4, 0); break; case 3: digitalWrite(1, 0); digitalWrite(2, 1); digitalWrite(3, 1); digitalWrite(4, 0); break; case 4: digitalWrite(1, 0); digitalWrite(2, 1); digitalWrite(3, 0); digitalWrite(4, 0); break; case 5: digitalWrite(1, 1); digitalWrite(2, 1); digitalWrite(3, 0); digitalWrite(4, 0); break; case 6: digitalWrite(1, 1); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 0); break; case 7: digitalWrite(1, 1); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 1); break; default: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 0); break; } Steps = (Steps + 1) % 8; } } |
Code golf this function to be a lot shorter. You only get points if it's <300 bytes (not counting whitespace). Keep the name `stepper`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | void stepper(int xw) { int Steps = 0; for (int x = 0; x < xw; x++) { switch (Steps) { case 0: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 1); break; case 1: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 1); digitalWrite(4, 1); break; case 2: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 1); digitalWrite(4, 0); break; case 3: digitalWrite(1, 0); digitalWrite(2, 1); digitalWrite(3, 1); digitalWrite(4, 0); break; case 4: digitalWrite(1, 0); digitalWrite(2, 1); digitalWrite(3, 0); digitalWrite(4, 0); break; case 5: digitalWrite(1, 1); digitalWrite(2, 1); digitalWrite(3, 0); digitalWrite(4, 0); break; case 6: digitalWrite(1, 1); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 0); break; case 7: digitalWrite(1, 1); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 1); break; default: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 0); break; } Steps = (Steps + 1) % 8; } } |
Here's a possible solution that is 280 bytes long (not counting whitespace):
1 | void stepper(int x){byte s=0,b[8]={4,6,5,3,2,7,1,0};for(int i=0;i<x;i++){digitalWrite(1,!s&1);digitalWrite(2,!s&2);digitalWrite(3,b[s]&1);digitalWrite(4,b[s]>>1);s=(s+1)%8;}} |
I extracted the following code from that output:
1 | void stepper(int x){byte s=0,b[8]={4,6,5,3,2,7,1,0};for(int i=0;i<x;i++){digitalWrite(1,!s&1);digitalWrite(2,!s&2);digitalWrite(3,b[s]&1);digitalWrite(4,b[s]>>1);s=(s+1)%8;}} |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | void stepper(int x){byte s=0,b[8]={4,6,5,3,2,7,1,0};for(int i=0;i<x;i++){digitalWrite(1,!s&1);digitalWrite(2,!s&2);digitalWrite(3,b[s]&1);digitalWrite(4,b[s]>>1);s=(s+1)%8;}} #include<stdio.h> #include<stdlib.h> #include <stdio.h> int out = 0; int digitalWrite(int pin, int value) { out *= 101; out += pin; out *= 97; out += value; return out; } int main() { int answer = (stepper(100),out); int expected = -1475775710; if (answer != expected) { printf("Wrong answer; got %d instead of %d.\n", answer, expected); exit(1); } printf("All tests passed\n"); } |
main.c: In function 'stepper':
main.c:2:21: error: unknown type name 'byte'
2 | void stepper(int x){byte s=0,b[8]={4,6,5,3,2,7,1,0};for(int i=0;i<x;i++){digitalWrite(1,!s&1);digitalWrite(2,!s&2);digitalWrite(3,b[s]&1);digitalWrite(4,b[s]>>1);s=(s+1)%8;}}
| ^~~~
main.c:2:74: warning: implicit declaration of function 'digitalWrite' [-Wimplicit-function-declaration]
2 | void stepper(int x){byte s=0,b[8]={4,6,5,3,2,7,1,0};for(int i=0;i<x;i++){digitalWrite(1,!s&1);digitalWrite(2,!s&2);digitalWrite(3,b[s]&1);digitalWrite(4,b[s]>>1);s=(s+1)%8;}}
| ^~~~~~~~~~~~
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string All tests passed
: False
Resulting in output: True
Code golf this function to be a lot shorter. You only get points if it's <300 bytes (not counting whitespace). Keep the name `stepper`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | void stepper(int xw) { int Steps = 0; for (int x = 0; x < xw; x++) { switch (Steps) { case 0: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 1); break; case 1: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 1); digitalWrite(4, 1); break; case 2: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 1); digitalWrite(4, 0); break; case 3: digitalWrite(1, 0); digitalWrite(2, 1); digitalWrite(3, 1); digitalWrite(4, 0); break; case 4: digitalWrite(1, 0); digitalWrite(2, 1); digitalWrite(3, 0); digitalWrite(4, 0); break; case 5: digitalWrite(1, 1); digitalWrite(2, 1); digitalWrite(3, 0); digitalWrite(4, 0); break; case 6: digitalWrite(1, 1); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 0); break; case 7: digitalWrite(1, 1); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 1); break; default: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 0); break; } Steps = (Steps + 1) % 8; } } |
Code golf this function to be a lot shorter. You only get points if it's <300 bytes (not counting whitespace). Keep the name `stepper`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | void stepper(int xw) { int Steps = 0; for (int x = 0; x < xw; x++) { switch (Steps) { case 0: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 1); break; case 1: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 1); digitalWrite(4, 1); break; case 2: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 1); digitalWrite(4, 0); break; case 3: digitalWrite(1, 0); digitalWrite(2, 1); digitalWrite(3, 1); digitalWrite(4, 0); break; case 4: digitalWrite(1, 0); digitalWrite(2, 1); digitalWrite(3, 0); digitalWrite(4, 0); break; case 5: digitalWrite(1, 1); digitalWrite(2, 1); digitalWrite(3, 0); digitalWrite(4, 0); break; case 6: digitalWrite(1, 1); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 0); break; case 7: digitalWrite(1, 1); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 1); break; default: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 0); break; } Steps = (Steps + 1) % 8; } } |
Here is a golfed version of the code that is under 300 bytes (271 bytes):
1 | void stepper(int xw){byte s=0,d[]={0,4,2,1,6,5,3,7};for(int x=0;x<xw;x++){digitalWrite(1,!((s>>6)&1));digitalWrite(2,!((s>>4)&1));digitalWrite(3,!((s>>2)&1));digitalWrite(4,!((s>>0)&1));s=(s+1)&7;s^=d[s];}} |
I extracted the following code from that output:
1 | void stepper(int xw){byte s=0,d[]={0,4,2,1,6,5,3,7};for(int x=0;x<xw;x++){digitalWrite(1,!((s>>6)&1));digitalWrite(2,!((s>>4)&1));digitalWrite(3,!((s>>2)&1));digitalWrite(4,!((s>>0)&1));s=(s+1)&7;s^=d[s];}} |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | void stepper(int xw){byte s=0,d[]={0,4,2,1,6,5,3,7};for(int x=0;x<xw;x++){digitalWrite(1,!((s>>6)&1));digitalWrite(2,!((s>>4)&1));digitalWrite(3,!((s>>2)&1));digitalWrite(4,!((s>>0)&1));s=(s+1)&7;s^=d[s];}} #include<stdio.h> #include<stdlib.h> #include <stdio.h> int out = 0; int digitalWrite(int pin, int value) { out *= 101; out += pin; out *= 97; out += value; return out; } int main() { int answer = (stepper(100),out); int expected = -1475775710; if (answer != expected) { printf("Wrong answer; got %d instead of %d.\n", answer, expected); exit(1); } printf("All tests passed\n"); } |
main.c: In function 'stepper':
main.c:2:22: error: unknown type name 'byte'
2 | void stepper(int xw){byte s=0,d[]={0,4,2,1,6,5,3,7};for(int x=0;x<xw;x++){digitalWrite(1,!((s>>6)&1));digitalWrite(2,!((s>>4)&1));digitalWrite(3,!((s>>2)&1));digitalWrite(4,!((s>>0)&1));s=(s+1)&7;s^=d[s];}}
| ^~~~
main.c:2:75: warning: implicit declaration of function 'digitalWrite' [-Wimplicit-function-declaration]
2 | void stepper(int xw){byte s=0,d[]={0,4,2,1,6,5,3,7};for(int x=0;x<xw;x++){digitalWrite(1,!((s>>6)&1));digitalWrite(2,!((s>>4)&1));digitalWrite(3,!((s>>2)&1));digitalWrite(4,!((s>>0)&1));s=(s+1)&7;s^=d[s];}}
| ^~~~~~~~~~~~
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string All tests passed
: False
Resulting in output: True
Code golf this function to be a lot shorter. You only get points if it's <300 bytes (not counting whitespace). Keep the name `stepper`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | void stepper(int xw) { int Steps = 0; for (int x = 0; x < xw; x++) { switch (Steps) { case 0: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 1); break; case 1: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 1); digitalWrite(4, 1); break; case 2: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 1); digitalWrite(4, 0); break; case 3: digitalWrite(1, 0); digitalWrite(2, 1); digitalWrite(3, 1); digitalWrite(4, 0); break; case 4: digitalWrite(1, 0); digitalWrite(2, 1); digitalWrite(3, 0); digitalWrite(4, 0); break; case 5: digitalWrite(1, 1); digitalWrite(2, 1); digitalWrite(3, 0); digitalWrite(4, 0); break; case 6: digitalWrite(1, 1); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 0); break; case 7: digitalWrite(1, 1); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 1); break; default: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 0); break; } Steps = (Steps + 1) % 8; } } |
Code golf this function to be a lot shorter. You only get points if it's <300 bytes (not counting whitespace). Keep the name `stepper`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | void stepper(int xw) { int Steps = 0; for (int x = 0; x < xw; x++) { switch (Steps) { case 0: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 1); break; case 1: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 1); digitalWrite(4, 1); break; case 2: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 1); digitalWrite(4, 0); break; case 3: digitalWrite(1, 0); digitalWrite(2, 1); digitalWrite(3, 1); digitalWrite(4, 0); break; case 4: digitalWrite(1, 0); digitalWrite(2, 1); digitalWrite(3, 0); digitalWrite(4, 0); break; case 5: digitalWrite(1, 1); digitalWrite(2, 1); digitalWrite(3, 0); digitalWrite(4, 0); break; case 6: digitalWrite(1, 1); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 0); break; case 7: digitalWrite(1, 1); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 1); break; default: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 0); break; } Steps = (Steps + 1) % 8; } } |
Here's a possible solution that meets the requirements:
1 | void stepper(int xw){int s=0,p[]={0,4,5,6,9,10,12,13};for(int x=0;x<xw;x++){digitalWrite(1,!(s&1));digitalWrite(2,!(s&2));digitalWrite(3,!(s&4));digitalWrite(4,p[s]&1);s=(s+1)&7;}} |
I extracted the following code from that output:
1 | void stepper(int xw){int s=0,p[]={0,4,5,6,9,10,12,13};for(int x=0;x<xw;x++){digitalWrite(1,!(s&1));digitalWrite(2,!(s&2));digitalWrite(3,!(s&4));digitalWrite(4,p[s]&1);s=(s+1)&7;}} |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | void stepper(int xw){int s=0,p[]={0,4,5,6,9,10,12,13};for(int x=0;x<xw;x++){digitalWrite(1,!(s&1));digitalWrite(2,!(s&2));digitalWrite(3,!(s&4));digitalWrite(4,p[s]&1);s=(s+1)&7;}} #include<stdio.h> #include<stdlib.h> #include <stdio.h> int out = 0; int digitalWrite(int pin, int value) { out *= 101; out += pin; out *= 97; out += value; return out; } int main() { int answer = (stepper(100),out); int expected = -1475775710; if (answer != expected) { printf("Wrong answer; got %d instead of %d.\n", answer, expected); exit(1); } printf("All tests passed\n"); } |
Wrong answer; got 1646634493 instead of -1475775710.
main.c: In function 'stepper':
main.c:2:77: warning: implicit declaration of function 'digitalWrite' [-Wimplicit-function-declaration]
2 | void stepper(int xw){int s=0,p[]={0,4,5,6,9,10,12,13};for(int x=0;x<xw;x++){digitalWrite(1,!(s&1));digitalWrite(2,!(s&2));digitalWrite(3,!(s&4));digitalWrite(4,p[s]&1);s=(s+1)&7;}}
| ^~~~~~~~~~~~
Testing if the previous output contains the string All tests passed
: False
Resulting in output: True
Code golf this function to be a lot shorter. You only get points if it's <300 bytes (not counting whitespace). Keep the name `stepper`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | void stepper(int xw) { int Steps = 0; for (int x = 0; x < xw; x++) { switch (Steps) { case 0: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 1); break; case 1: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 1); digitalWrite(4, 1); break; case 2: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 1); digitalWrite(4, 0); break; case 3: digitalWrite(1, 0); digitalWrite(2, 1); digitalWrite(3, 1); digitalWrite(4, 0); break; case 4: digitalWrite(1, 0); digitalWrite(2, 1); digitalWrite(3, 0); digitalWrite(4, 0); break; case 5: digitalWrite(1, 1); digitalWrite(2, 1); digitalWrite(3, 0); digitalWrite(4, 0); break; case 6: digitalWrite(1, 1); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 0); break; case 7: digitalWrite(1, 1); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 1); break; default: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 0); break; } Steps = (Steps + 1) % 8; } } |
Code golf this function to be a lot shorter. You only get points if it's <300 bytes (not counting whitespace). Keep the name `stepper`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | void stepper(int xw) { int Steps = 0; for (int x = 0; x < xw; x++) { switch (Steps) { case 0: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 1); break; case 1: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 1); digitalWrite(4, 1); break; case 2: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 1); digitalWrite(4, 0); break; case 3: digitalWrite(1, 0); digitalWrite(2, 1); digitalWrite(3, 1); digitalWrite(4, 0); break; case 4: digitalWrite(1, 0); digitalWrite(2, 1); digitalWrite(3, 0); digitalWrite(4, 0); break; case 5: digitalWrite(1, 1); digitalWrite(2, 1); digitalWrite(3, 0); digitalWrite(4, 0); break; case 6: digitalWrite(1, 1); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 0); break; case 7: digitalWrite(1, 1); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 1); break; default: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 0); break; } Steps = (Steps + 1) % 8; } } |
Here is a golfed version of `stepper` that is 262 bytes long (not including whitespace):
1 | void stepper(int x){byte s=0,d[]={0,0,0,1,0,1,1,1,0,1,1,0,1,0,0,0,1,1,0,0,0,0,1,0,0,1};for(int i=0;i<x;i++){digitalWrite(1,d[s]);digitalWrite(2,d[s+1]);digitalWrite(3,d[s+2]);digitalWrite(4,d[s+3]);s=(s+1)%8;}} |
1 2 3 4 5 6 7 8 9 10 | void stepper(int x){ byte s=0, d[]={0,0,0,1,0,1,1,1,0,1,1,0,1,0,0,0,1,1,0,0,0,0,1,0,0,1}; for(int i=0;i<x;i++){ (\<br> digitalWrite(1,d[s]); digitalWrite(2,d[s+1]); digitalWrite(3,d[s+2]); digitalWrite(4,d[s+3]); s=(s+1)%8; } } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 | #include <Arduino.h> void stepper(int x){ byte s=0, d[]={0,0,0,1,0,1,1,1,0,1,1,0,1,0,0,0,1,1,0,0,0,0,1,0,0,1}; for(int i=0;i<x;i++){ digitalWrite(1,d[s]); digitalWrite(2,d[s+1]); digitalWrite(3,d[s+2]); digitalWrite(4,d[s+3]); s=(s+1)%8; } } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include <Arduino.h> void stepper(int x){ byte s=0, d[]={0,0,0,1,0,1,1,1,0,1,1,0,1,0,0,0,1,1,0,0,0,0,1,0,0,1}; for(int i=0;i<x;i++){ digitalWrite(1,d[s]); digitalWrite(2,d[s+1]); digitalWrite(3,d[s+2]); digitalWrite(4,d[s+3]); s=(s+1)%8; } } #include<stdio.h> #include<stdlib.h> #include <stdio.h> int out = 0; int digitalWrite(int pin, int value) { out *= 101; out += pin; out *= 97; out += value; return out; } int main() { int answer = (stepper(100),out); int expected = -1475775710; if (answer != expected) { printf("Wrong answer; got %d instead of %d.\n", answer, expected); exit(1); } printf("All tests passed\n"); } |
main.c:2:10: fatal error: Arduino.h: No such file or directory
2 | #include <Arduino.h>
| ^~~~~~~~~~~
compilation terminated.
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string All tests passed
: False
Resulting in output: True
Code golf this function to be a lot shorter. You only get points if it's <300 bytes (not counting whitespace). Keep the name `stepper`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | void stepper(int xw) { int Steps = 0; for (int x = 0; x < xw; x++) { switch (Steps) { case 0: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 1); break; case 1: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 1); digitalWrite(4, 1); break; case 2: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 1); digitalWrite(4, 0); break; case 3: digitalWrite(1, 0); digitalWrite(2, 1); digitalWrite(3, 1); digitalWrite(4, 0); break; case 4: digitalWrite(1, 0); digitalWrite(2, 1); digitalWrite(3, 0); digitalWrite(4, 0); break; case 5: digitalWrite(1, 1); digitalWrite(2, 1); digitalWrite(3, 0); digitalWrite(4, 0); break; case 6: digitalWrite(1, 1); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 0); break; case 7: digitalWrite(1, 1); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 1); break; default: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 0); break; } Steps = (Steps + 1) % 8; } } |
Code golf this function to be a lot shorter. You only get points if it's <300 bytes (not counting whitespace). Keep the name `stepper`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | void stepper(int xw) { int Steps = 0; for (int x = 0; x < xw; x++) { switch (Steps) { case 0: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 1); break; case 1: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 1); digitalWrite(4, 1); break; case 2: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 1); digitalWrite(4, 0); break; case 3: digitalWrite(1, 0); digitalWrite(2, 1); digitalWrite(3, 1); digitalWrite(4, 0); break; case 4: digitalWrite(1, 0); digitalWrite(2, 1); digitalWrite(3, 0); digitalWrite(4, 0); break; case 5: digitalWrite(1, 1); digitalWrite(2, 1); digitalWrite(3, 0); digitalWrite(4, 0); break; case 6: digitalWrite(1, 1); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 0); break; case 7: digitalWrite(1, 1); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 1); break; default: digitalWrite(1, 0); digitalWrite(2, 0); digitalWrite(3, 0); digitalWrite(4, 0); break; } Steps = (Steps + 1) % 8; } } |
Here is a golfed version of your function in 263 bytes:
1 | void stepper(int x){byte s=0,d=1;for(int i=0;i<x;i++){digitalWrite(1,s&1);digitalWrite(2,(s>>1)&1);digitalWrite(3,(s>>2)&1);digitalWrite(4,(s>>3)&1);s+=d;if(s&8)s-=8;d^=1;}} |
1 | void stepper(int x){byte s=0,d=1;for(int i=0;i<x;i++){digitalWrite(1,s&1);digitalWrite(2,s&2<1);digitalWrite(3,s&4<2);digitalWrite(4,s&8<4);s+=d;d=-d^(s==8|s==-8);}} |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <Arduino.h> void stepper(int x){ byte s = 0, d = 1; for (int i = 0; i < x; i++) { digitalWrite(1, s & 1); digitalWrite(2, s & 2 < 1); digitalWrite(3, s & 4 < 2); digitalWrite(4, s & 8 < 4); s += d; d = -d ^ (s == 8 | s == -8); } } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include <Arduino.h> void stepper(int x){ byte s = 0, d = 1; for (int i = 0; i < x; i++) { digitalWrite(1, s & 1); digitalWrite(2, s & 2 < 1); digitalWrite(3, s & 4 < 2); digitalWrite(4, s & 8 < 4); s += d; d = -d ^ (s == 8 | s == -8); } } #include<stdio.h> #include<stdlib.h> #include <stdio.h> int out = 0; int digitalWrite(int pin, int value) { out *= 101; out += pin; out *= 97; out += value; return out; } int main() { int answer = (stepper(100),out); int expected = -1475775710; if (answer != expected) { printf("Wrong answer; got %d instead of %d.\n", answer, expected); exit(1); } printf("All tests passed\n"); } |
main.c:2:10: fatal error: Arduino.h: No such file or directory
2 | #include <Arduino.h>
| ^~~~~~~~~~~
compilation terminated.
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string All tests passed
: False
Resulting in output: True