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; } } |
1 2 3 4 5 6 7 8 9 | void stepper(int xw) { for (int x = 0, Steps = 0; x < xw; x++, Steps = (Steps + 1) % 8) { int state = (Steps == 0 || Steps == 7) * 8 + (Steps == 1 || Steps == 2) * 4 + (Steps == 3 || Steps == 4) * 2 + (Steps == 5 || Steps == 6); digitalWrite(1, state & 8); digitalWrite(2, state & 4); digitalWrite(3, state & 2); digitalWrite(4, state & 1); } } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 | void stepper(int xw) { for (int x = 0, Steps = 0; x < xw; x++, Steps = (Steps + 1) % 8) { int state = (Steps == 0 || Steps == 7) * 8 + (Steps == 1 || Steps == 2) * 4 + (Steps == 3 || Steps == 4) * 2 + (Steps == 5 || Steps == 6); digitalWrite(1, state & 8); digitalWrite(2, state & 4); digitalWrite(3, state & 2); digitalWrite(4, state & 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 30 31 32 33 34 35 36 37 | void stepper(int xw) { for (int x = 0, Steps = 0; x < xw; x++, Steps = (Steps + 1) % 8) { int state = (Steps == 0 || Steps == 7) * 8 + (Steps == 1 || Steps == 2) * 4 + (Steps == 3 || Steps == 4) * 2 + (Steps == 5 || Steps == 6); digitalWrite(1, state & 8); digitalWrite(2, state & 4); digitalWrite(3, state & 2); digitalWrite(4, state & 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"); } |
Wrong answer; got 1579308906 instead of -1475775710.
main.c: In function 'stepper':
main.c:5:5: warning: implicit declaration of function 'digitalWrite' [-Wimplicit-function-declaration]
5 | digitalWrite(1, state & 8);
| ^~~~~~~~~~~~
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; } } |
void stepper(int xw) { int S = 0; for (int x = 0; x < xw; x++) { switch (S) { 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; } S = (S + 1) % 8; } }
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <iostream> void stepper(int xw) { int S = 0; for (int x = 0; x < xw; x++) { switch (S) { 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; } 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 41 42 43 44 45 46 | #include <iostream> void stepper(int xw) { int S = 0; for (int x = 0; x < xw; x++) { switch (S) { 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; } 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:1:10: fatal error: iostream: No such file or directory
1 | #include <iostream>
| ^~~~~~~~~~
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: 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; } } |
1 2 | void stepper(int xw) { int S=0;for(int x=0;x<xw;x++){switch(S){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;}S=(S+1)%8;}} |
I extracted the following code from that output:
1 2 | void stepper(int xw) { int S=0;for(int x=0;x<xw;x++){switch(S){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;}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 | void stepper(int xw) { int S=0;for(int x=0;x<xw;x++){switch(S){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;}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"); } |
All tests passed
main.c: In function 'stepper':
main.c:3:50: warning: implicit declaration of function 'digitalWrite' [-Wimplicit-function-declaration]
3 | int S=0;for(int x=0;x<xw;x++){switch(S){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;}S=(S+1)%8;}}
| ^~~~~~~~~~~~
Testing if the previous output contains the string All tests passed
: True
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; } } |
void stepper(int xw) { int Steps = 0; for (int x = 0; x < xw; x++) { int vals[] = {0b0001, 0b0011, 0b0010, 0b0110, 0b0100, 0b1100, 0b1000, 0b1001}; int val = vals[Steps]; for (int i = 1; i <= 4; i++) { digitalWrite(i, val % 2); val >>= 1; } Steps = (Steps + 1) % 8; } }
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <iostream> #include <vector> void stepper(int xw) { int Steps = 0; for (int x = 0; x < xw; x++) { int vals[] = {0b0001, 0b0011, 0b0010, 0b0110, 0b0100, 0b1100, 0b1000, 0b1001}; int val = vals[Steps]; for (int i = 1; i <= 4; i++) { digitalWrite(i, val % 2); val >>= 1; } Steps = (Steps + 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 41 42 | #include <iostream> #include <vector> void stepper(int xw) { int Steps = 0; for (int x = 0; x < xw; x++) { int vals[] = {0b0001, 0b0011, 0b0010, 0b0110, 0b0100, 0b1100, 0b1000, 0b1001}; int val = vals[Steps]; for (int i = 1; i <= 4; i++) { digitalWrite(i, val % 2); val >>= 1; } Steps = (Steps + 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:1:10: fatal error: iostream: No such file or directory
1 | #include <iostream>
| ^~~~~~~~~~
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: 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; } } |
1 2 3 4 5 6 | void stepper(int xw) { for (int x = 0, Steps = 0; x < xw; x++, Steps = (Steps + 1) % 8) { int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}}; for (int i = 0; i < 4; i++) digitalWrite(i+1, vals[Steps][i]); } } |
I extracted the following code from that output:
1 2 3 4 5 6 | void stepper(int xw) { for (int x = 0, Steps = 0; x < xw; x++, Steps = (Steps + 1) % 8) { int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}}; for (int i = 0; i < 4; i++) digitalWrite(i+1, vals[Steps][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 30 31 32 33 34 | void stepper(int xw) { for (int x = 0, Steps = 0; x < xw; x++, Steps = (Steps + 1) % 8) { int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}}; for (int i = 0; i < 4; i++) digitalWrite(i+1, vals[Steps][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:4:5: warning: braces around scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^~~
main.c:4:5: note: (near initialization for 'vals[0]')
main.c:4:22: warning: excess elements in scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^
main.c:4:22: note: (near initialization for 'vals[0]')
main.c:4:24: warning: excess elements in scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^
main.c:4:24: note: (near initialization for 'vals[0]')
main.c:4:26: warning: excess elements in scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^
main.c:4:26: note: (near initialization for 'vals[0]')
main.c:4:5: warning: braces around scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^~~
main.c:4:5: note: (near initialization for 'vals[1]')
main.c:4:32: warning: excess elements in scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^
main.c:4:32: note: (near initialization for 'vals[1]')
main.c:4:34: warning: excess elements in scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^
main.c:4:34: note: (near initialization for 'vals[1]')
main.c:4:36: warning: excess elements in scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^
main.c:4:36: note: (near initialization for 'vals[1]')
main.c:4:5: warning: braces around scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^~~
main.c:4:5: note: (near initialization for 'vals[2]')
main.c:4:42: warning: excess elements in scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^
main.c:4:42: note: (near initialization for 'vals[2]')
main.c:4:44: warning: excess elements in scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^
main.c:4:44: note: (near initialization for 'vals[2]')
main.c:4:46: warning: excess elements in scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^
main.c:4:46: note: (near initialization for 'vals[2]')
main.c:4:5: warning: braces around scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^~~
main.c:4:5: note: (near initialization for 'vals[3]')
main.c:4:52: warning: excess elements in scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^
main.c:4:52: note: (near initialization for 'vals[3]')
main.c:4:54: warning: excess elements in scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^
main.c:4:54: note: (near initialization for 'vals[3]')
main.c:4:56: warning: excess elements in scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^
main.c:4:56: note: (near initialization for 'vals[3]')
main.c:4:5: warning: braces around scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^~~
main.c:4:5: note: (near initialization for 'vals[4]')
main.c:4:62: warning: excess elements in scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^
main.c:4:62: note: (near initialization for 'vals[4]')
main.c:4:64: warning: excess elements in scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^
main.c:4:64: note: (near initialization for 'vals[4]')
main.c:4:66: warning: excess elements in scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^
main.c:4:66: note: (near initialization for 'vals[4]')
main.c:4:5: warning: braces around scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^~~
main.c:4:5: note: (near initialization for 'vals[5]')
main.c:4:72: warning: excess elements in scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^
main.c:4:72: note: (near initialization for 'vals[5]')
main.c:4:74: warning: excess elements in scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^
main.c:4:74: note: (near initialization for 'vals[5]')
main.c:4:76: warning: excess elements in scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^
main.c:4:76: note: (near initialization for 'vals[5]')
main.c:4:5: warning: braces around scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^~~
main.c:4:5: note: (near initialization for 'vals[6]')
main.c:4:82: warning: excess elements in scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^
main.c:4:82: note: (near initialization for 'vals[6]')
main.c:4:84: warning: excess elements in scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^
main.c:4:84: note: (near initialization for 'vals[6]')
main.c:4:86: warning: excess elements in scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^
main.c:4:86: note: (near initialization for 'vals[6]')
main.c:4:5: warning: braces around scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^~~
main.c:4:5: note: (near initialization for 'vals[7]')
main.c:4:92: warning: excess elements in scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^
main.c:4:92: note: (near initialization for 'vals[7]')
main.c:4:94: warning: excess elements in scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^
main.c:4:94: note: (near initialization for 'vals[7]')
main.c:4:96: warning: excess elements in scalar initializer
4 | int vals[] = {{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,1,1,0},{0,1,0,0},{1,1,0,0},{1,0,0,0},{1,0,0,1}};
| ^
main.c:4:96: note: (near initialization for 'vals[7]')
main.c:5:33: warning: implicit declaration of function 'digitalWrite' [-Wimplicit-function-declaration]
5 | for (int i = 0; i < 4; i++) digitalWrite(i+1, vals[Steps][i]);
| ^~~~~~~~~~~~
main.c:5:62: error: subscripted value is neither array nor pointer nor vector
5 | for (int i = 0; i < 4; i++) digitalWrite(i+1, vals[Steps][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