Code golf this function to be a lot shorter. You only get points if it's <200 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 <200 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 shorter version of the `stepper` function in 177 bytes:
1 | void stepper(int xw){byte s=0,d[]={B11110000,B11011000,B10011000,B00111000,B01110000,B11100000,B11000000,B10000001},i=8;for(int x=0;x<xw;x++){digitalWrite(1,d[s]&128>0);digitalWrite(2,(d[s]>>4)&16>0);digitalWrite(3,(d[s]>>1)&32>0);digitalWrite(4,d[s]&1>0);s=(s+1)%i;}} |
I extracted the following code from that output:
1 | void stepper(int xw){byte s=0,d[]={B11110000,B11011000,B10011000,B00111000,B01110000,B11100000,B11000000,B10000001},i=8;for(int x=0;x<xw;x++){digitalWrite(1,d[s]&128>0);digitalWrite(2,(d[s]>>4)&16>0);digitalWrite(3,(d[s]>>1)&32>0);digitalWrite(4,d[s]&1>0);s=(s+1)%i;}} |
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[]={B11110000,B11011000,B10011000,B00111000,B01110000,B11100000,B11000000,B10000001},i=8;for(int x=0;x<xw;x++){digitalWrite(1,d[s]&128>0);digitalWrite(2,(d[s]>>4)&16>0);digitalWrite(3,(d[s]>>1)&32>0);digitalWrite(4,d[s]&1>0);s=(s+1)%i;}} #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[]={B11110000,B11011000,B10011000,B00111000,B01110000,B11100000,B11000000,B10000001},i=8;for(int x=0;x<xw;x++){digitalWrite(1,d[s]&128>0);digitalWrite(2,(d[s]>>4)&16>0);digitalWrite(3,(d[s]>>1)&32>0);digitalWrite(4,d[s]&1>0);s=(s+1)%i;}}
| ^~~~
main.c:2:36: error: 'B11110000' undeclared (first use in this function)
2 | void stepper(int xw){byte s=0,d[]={B11110000,B11011000,B10011000,B00111000,B01110000,B11100000,B11000000,B10000001},i=8;for(int x=0;x<xw;x++){digitalWrite(1,d[s]&128>0);digitalWrite(2,(d[s]>>4)&16>0);digitalWrite(3,(d[s]>>1)&32>0);digitalWrite(4,d[s]&1>0);s=(s+1)%i;}}
| ^~~~~~~~~
main.c:2:36: note: each undeclared identifier is reported only once for each function it appears in
main.c:2:46: error: 'B11011000' undeclared (first use in this function)
2 | void stepper(int xw){byte s=0,d[]={B11110000,B11011000,B10011000,B00111000,B01110000,B11100000,B11000000,B10000001},i=8;for(int x=0;x<xw;x++){digitalWrite(1,d[s]&128>0);digitalWrite(2,(d[s]>>4)&16>0);digitalWrite(3,(d[s]>>1)&32>0);digitalWrite(4,d[s]&1>0);s=(s+1)%i;}}
| ^~~~~~~~~
main.c:2:56: error: 'B10011000' undeclared (first use in this function)
2 | void stepper(int xw){byte s=0,d[]={B11110000,B11011000,B10011000,B00111000,B01110000,B11100000,B11000000,B10000001},i=8;for(int x=0;x<xw;x++){digitalWrite(1,d[s]&128>0);digitalWrite(2,(d[s]>>4)&16>0);digitalWrite(3,(d[s]>>1)&32>0);digitalWrite(4,d[s]&1>0);s=(s+1)%i;}}
| ^~~~~~~~~
main.c:2:66: error: 'B00111000' undeclared (first use in this function)
2 | void stepper(int xw){byte s=0,d[]={B11110000,B11011000,B10011000,B00111000,B01110000,B11100000,B11000000,B10000001},i=8;for(int x=0;x<xw;x++){digitalWrite(1,d[s]&128>0);digitalWrite(2,(d[s]>>4)&16>0);digitalWrite(3,(d[s]>>1)&32>0);digitalWrite(4,d[s]&1>0);s=(s+1)%i;}}
| ^~~~~~~~~
main.c:2:76: error: 'B01110000' undeclared (first use in this function)
2 | void stepper(int xw){byte s=0,d[]={B11110000,B11011000,B10011000,B00111000,B01110000,B11100000,B11000000,B10000001},i=8;for(int x=0;x<xw;x++){digitalWrite(1,d[s]&128>0);digitalWrite(2,(d[s]>>4)&16>0);digitalWrite(3,(d[s]>>1)&32>0);digitalWrite(4,d[s]&1>0);s=(s+1)%i;}}
| ^~~~~~~~~
main.c:2:86: error: 'B11100000' undeclared (first use in this function)
2 | void stepper(int xw){byte s=0,d[]={B11110000,B11011000,B10011000,B00111000,B01110000,B11100000,B11000000,B10000001},i=8;for(int x=0;x<xw;x++){digitalWrite(1,d[s]&128>0);digitalWrite(2,(d[s]>>4)&16>0);digitalWrite(3,(d[s]>>1)&32>0);digitalWrite(4,d[s]&1>0);s=(s+1)%i;}}
| ^~~~~~~~~
main.c:2:96: error: 'B11000000' undeclared (first use in this function)
2 | void stepper(int xw){byte s=0,d[]={B11110000,B11011000,B10011000,B00111000,B01110000,B11100000,B11000000,B10000001},i=8;for(int x=0;x<xw;x++){digitalWrite(1,d[s]&128>0);digitalWrite(2,(d[s]>>4)&16>0);digitalWrite(3,(d[s]>>1)&32>0);digitalWrite(4,d[s]&1>0);s=(s+1)%i;}}
| ^~~~~~~~~
main.c:2:106: error: 'B10000001' undeclared (first use in this function)
2 | void stepper(int xw){byte s=0,d[]={B11110000,B11011000,B10011000,B00111000,B01110000,B11100000,B11000000,B10000001},i=8;for(int x=0;x<xw;x++){digitalWrite(1,d[s]&128>0);digitalWrite(2,(d[s]>>4)&16>0);digitalWrite(3,(d[s]>>1)&32>0);digitalWrite(4,d[s]&1>0);s=(s+1)%i;}}
| ^~~~~~~~~
main.c:2:143: warning: implicit declaration of function 'digitalWrite' [-Wimplicit-function-declaration]
2 | void stepper(int xw){byte s=0,d[]={B11110000,B11011000,B10011000,B00111000,B01110000,B11100000,B11000000,B10000001},i=8;for(int x=0;x<xw;x++){digitalWrite(1,d[s]&128>0);digitalWrite(2,(d[s]>>4)&16>0);digitalWrite(3,(d[s]>>1)&32>0);digitalWrite(4,d[s]&1>0);s=(s+1)%i;}}
| ^~~~~~~~~~~~
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: False
Code golf this function to be a lot shorter. You only get points if it's <200 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 <200 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 possible solution that is shorter than 200 bytes (181 bytes without whitespace):
1 | void stepper(int xw){byte s=0,d[]={0,4,2,6,1,3,7,5};for(int x=0;x<xw;x++){digitalWrite(1,s&1);digitalWrite(2,(s>>1)&1);digitalWrite(3,(s>>2)&1);digitalWrite(4,d[s]&1);s=(s+1)%8;}} |
I extracted the following code from that output:
1 | void stepper(int xw){byte s=0,d[]={0,4,2,6,1,3,7,5};for(int x=0;x<xw;x++){digitalWrite(1,s&1);digitalWrite(2,(s>>1)&1);digitalWrite(3,(s>>2)&1);digitalWrite(4,d[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 xw){byte s=0,d[]={0,4,2,6,1,3,7,5};for(int x=0;x<xw;x++){digitalWrite(1,s&1);digitalWrite(2,(s>>1)&1);digitalWrite(3,(s>>2)&1);digitalWrite(4,d[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:22: error: unknown type name 'byte'
2 | void stepper(int xw){byte s=0,d[]={0,4,2,6,1,3,7,5};for(int x=0;x<xw;x++){digitalWrite(1,s&1);digitalWrite(2,(s>>1)&1);digitalWrite(3,(s>>2)&1);digitalWrite(4,d[s]&1);s=(s+1)%8;}}
| ^~~~
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,6,1,3,7,5};for(int x=0;x<xw;x++){digitalWrite(1,s&1);digitalWrite(2,(s>>1)&1);digitalWrite(3,(s>>2)&1);digitalWrite(4,d[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 <200 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 <200 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 possible golfed version of the function, written in C, that is 135 bytes long (not counting whitespace):
1 | void stepper(int xw){int s,S[]={0,4,2,6,1,5,3,7},i;for(i=0;i<xw;i++)for(s=0;s<8;s++){digitalWrite(s+1,S[s]&1<<3-s);S[s]=(S[s]+1)&7;} |
1 | S[s]&1<<3-s |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 | #include <Arduino.h> void stepper(int xw){ int s, S[]={0,4,2,6,1,5,3,7}, i; for(i=0;i<xw;i++) for(s=0;s<8;s++){ digitalWrite(s+1,S[s]&1<<3-s); S[s]=(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 30 31 32 33 34 35 36 37 38 | #include <Arduino.h> void stepper(int xw){ int s, S[]={0,4,2,6,1,5,3,7}, i; for(i=0;i<xw;i++) for(s=0;s<8;s++){ digitalWrite(s+1,S[s]&1<<3-s); S[s]=(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"); } |
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 <200 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 <200 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 possible solution that is 162 bytes long (not counting whitespace):
1 | void stepper(int x){byte s=0,d=1,b[8]={1,3,2,6,7,5,4,0};for(int i=0;i<x;i++){digitalWrite(1,b[s]&1);digitalWrite(2,(b[s]>>1)&1);digitalWrite(3,(b[s]>>2)&1);digitalWrite(4,(b[s]>>3)&1);s=(s+d)&7;} |
I extracted the following code from that output:
1 | void stepper(int x){byte s=0,d=1,b[8]={1,3,2,6,7,5,4,0};for(int i=0;i<x;i++){digitalWrite(1,b[s]&1);digitalWrite(2,(b[s]>>1)&1);digitalWrite(3,(b[s]>>2)&1);digitalWrite(4,(b[s]>>3)&1);s=(s+d)&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 x){byte s=0,d=1,b[8]={1,3,2,6,7,5,4,0};for(int i=0;i<x;i++){digitalWrite(1,b[s]&1);digitalWrite(2,(b[s]>>1)&1);digitalWrite(3,(b[s]>>2)&1);digitalWrite(4,(b[s]>>3)&1);s=(s+d)&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"); } |
main.c: In function 'stepper':
main.c:2:21: error: unknown type name 'byte'
2 | void stepper(int x){byte s=0,d=1,b[8]={1,3,2,6,7,5,4,0};for(int i=0;i<x;i++){digitalWrite(1,b[s]&1);digitalWrite(2,(b[s]>>1)&1);digitalWrite(3,(b[s]>>2)&1);digitalWrite(4,(b[s]>>3)&1);s=(s+d)&7;}
| ^~~~
main.c:2:78: warning: implicit declaration of function 'digitalWrite' [-Wimplicit-function-declaration]
2 | void stepper(int x){byte s=0,d=1,b[8]={1,3,2,6,7,5,4,0};for(int i=0;i<x;i++){digitalWrite(1,b[s]&1);digitalWrite(2,(b[s]>>1)&1);digitalWrite(3,(b[s]>>2)&1);digitalWrite(4,(b[s]>>3)&1);s=(s+d)&7;}
| ^~~~~~~~~~~~
In file included from /usr/include/endian.h:35,
from /usr/include/x86_64-linux-gnu/sys/types.h:176,
from /usr/include/stdlib.h:395,
from main.c:7:
/usr/include/x86_64-linux-gnu/bits/byteswap.h:34:1: error: invalid storage class for function '__bswap_16'
34 | __bswap_16 (__uint16_t __bsx)
| ^~~~~~~~~~
/usr/include/x86_64-linux-gnu/bits/byteswap.h:49:1: error: invalid storage class for function '__bswap_32'
49 | __bswap_32 (__uint32_t __bsx)
| ^~~~~~~~~~
/usr/include/x86_64-linux-gnu/bits/byteswap.h:70:1: error: invalid storage class for function '__bswap_64'
70 | __bswap_64 (__uint64_t __bsx)
| ^~~~~~~~~~
In file included from /usr/include/endian.h:36,
from /usr/include/x86_64-linux-gnu/sys/types.h:176,
from /usr/include/stdlib.h:395,
from main.c:7:
/usr/include/x86_64-linux-gnu/bits/uintn-identity.h:33:1: error: invalid storage class for function '__uint16_identity'
33 | __uint16_identity (__uint16_t __x)
| ^~~~~~~~~~~~~~~~~
/usr/include/x86_64-linux-gnu/bits/uintn-identity.h:39:1: error: invalid storage class for function '__uint32_identity'
39 | __uint32_identity (__uint32_t __x)
| ^~~~~~~~~~~~~~~~~
/usr/include/x86_64-linux-gnu/bits/uintn-identity.h:45:1: error: invalid storage class for function '__uint64_identity'
45 | __uint64_identity (__uint64_t __x)
| ^~~~~~~~~~~~~~~~~
main.c:30:1: error: expected declaration or statement at end of input
30 | }
| ^
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 <200 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 <200 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 with 174 bytes:
1 | void stepper(int x){byte s=0,d=1;for(int xi=0;xi<x;xi++){digitalWrite(1,s&1);digitalWrite(2,(s>>1)&1);digitalWrite(3,(s>>2)&1);digitalWrite(4,(s>>3)&d);s=(s+1)&7;d^=1;}} |
I extracted the following code from that output:
1 | void stepper(int x){byte s=0,d=1;for(int xi=0;xi<x;xi++){digitalWrite(1,s&1);digitalWrite(2,(s>>1)&1);digitalWrite(3,(s>>2)&1);digitalWrite(4,(s>>3)&d);s=(s+1)&7;d^=1;}} |
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,d=1;for(int xi=0;xi<x;xi++){digitalWrite(1,s&1);digitalWrite(2,(s>>1)&1);digitalWrite(3,(s>>2)&1);digitalWrite(4,(s>>3)&d);s=(s+1)&7;d^=1;}} #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,d=1;for(int xi=0;xi<x;xi++){digitalWrite(1,s&1);digitalWrite(2,(s>>1)&1);digitalWrite(3,(s>>2)&1);digitalWrite(4,(s>>3)&d);s=(s+1)&7;d^=1;}}
| ^~~~
main.c:2:58: warning: implicit declaration of function 'digitalWrite' [-Wimplicit-function-declaration]
2 | void stepper(int x){byte s=0,d=1;for(int xi=0;xi<x;xi++){digitalWrite(1,s&1);digitalWrite(2,(s>>1)&1);digitalWrite(3,(s>>2)&1);digitalWrite(4,(s>>3)&d);s=(s+1)&7;d^=1;}}
| ^~~~~~~~~~~~
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