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; } } |
To significantly reduce the length of the `stepper` function, we can leverage bit manipulation based on the pattern observed in the `digitalWrite` sequences. Let's start by analyzing the pattern and then implement a more concise version. 1. **Creating a Table of Values:**
1 2 3 4 5 6 7 8 9 10 | Steps | 1 2 3 4 ----------------- 0 | 0 0 0 1 1 | 0 0 1 1 2 | 0 0 1 0 3 | 0 1 1 0 4 | 0 1 0 0 5 | 1 1 0 0 6 | 1 0 0 0 7 | 1 0 0 1 |
1 2 3 4 5 6 7 8 | void stepper(int xw) { for (int x = 0; x < xw; x++) { int pattern[] = {1, 3, 2, 6, 4, 12, 8, 9}; for (int pin = 1; pin <= 4; pin++) { digitalWrite(pin, (pattern[x % 8] >> (pin - 1)) & 1); } } } |
1 2 3 4 5 | void stepper(int xw) { for (int x = 0; x < xw; x++) for (int pin = 1; pin <= 4; pin++) digitalWrite(pin, ((int[]){1,3,2,6,4,12,8,9}[x%8]>>(pin-1))&1); } |
I extracted the following code from that output:
1 2 3 4 5 | void stepper(int xw) { for (int x = 0; x < xw; x++) for (int pin = 1; pin <= 4; pin++) digitalWrite(pin, ((int[]){1,3,2,6,4,12,8,9}[x%8]>>(pin-1))&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 | void stepper(int xw) { for (int x = 0; x < xw; x++) for (int pin = 1; pin <= 4; pin++) digitalWrite(pin, ((int[]){1,3,2,6,4,12,8,9}[x%8]>>(pin-1))&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 370724802 instead of -1475775710.
main.c: In function 'stepper':
main.c:5:7: warning: implicit declaration of function 'digitalWrite' [-Wimplicit-function-declaration]
5 | digitalWrite(pin, ((int[]){1,3,2,6,4,12,8,9}[x%8]>>(pin-1))&1);
| ^~~~~~~~~~~~
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; } } |
To code golf the given function, let's follow the steps outlined: 1. **Create a table of values:**
1 2 3 4 5 6 7 8 9 10 | Steps | Pin 1 | Pin 2 | Pin 3 | Pin 4 ------------------------------------- 0 | 0 | 0 | 0 | 1 1 | 0 | 0 | 1 | 1 2 | 0 | 0 | 1 | 0 3 | 0 | 1 | 1 | 0 4 | 0 | 1 | 0 | 0 5 | 1 | 1 | 0 | 0 6 | 1 | 0 | 0 | 0 7 | 1 | 0 | 0 | 1 |
1 2 3 4 5 6 7 8 | void stepper(int xw) { for (int x = 0; x < xw; x++) { digitalWrite(1, x % 8 >= 5 || x % 8 == 0 || x % 8 == 7); digitalWrite(2, x % 8 >= 3 && x % 8 <= 5 || x % 8 == 7); digitalWrite(3, x % 8 >= 1 && x % 8 <= 3 || x % 8 == 5); digitalWrite(4, x % 8 % 2 == 0 && x % 8 <= 2 || x % 8 == 7); } } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 | void stepper(int xw) { for (int x = 0; x < xw; x++) { digitalWrite(1, x % 8 >= 5 || x % 8 == 0 || x % 8 == 7); digitalWrite(2, x % 8 >= 3 && x % 8 <= 5 || x % 8 == 7); digitalWrite(3, x % 8 >= 1 && x % 8 <= 3 || x % 8 == 5); digitalWrite(4, x % 8 % 2 == 0 && x % 8 <= 2 || x % 8 == 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 | void stepper(int xw) { for (int x = 0; x < xw; x++) { digitalWrite(1, x % 8 >= 5 || x % 8 == 0 || x % 8 == 7); digitalWrite(2, x % 8 >= 3 && x % 8 <= 5 || x % 8 == 7); digitalWrite(3, x % 8 >= 1 && x % 8 <= 3 || x % 8 == 5); digitalWrite(4, x % 8 % 2 == 0 && x % 8 <= 2 || x % 8 == 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 1390203459 instead of -1475775710.
main.c: In function 'stepper':
main.c:4:5: warning: implicit declaration of function 'digitalWrite' [-Wimplicit-function-declaration]
4 | digitalWrite(1, x % 8 >= 5 || x % 8 == 0 || x % 8 == 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; } } |
To significantly reduce the size of the `stepper` function, we can observe the patterns in the `digitalWrite` calls within each case. Let's start by creating a table of the values being written to each pin for each step: | Step | Pin 1 | Pin 2 | Pin 3 | Pin 4 | |------|-------|-------|-------|-------| | 0 | 0 | 0 | 0 | 1 | | 1 | 0 | 0 | 1 | 1 | | 2 | 0 | 0 | 1 | 0 | | 3 | 0 | 1 | 1 | 0 | | 4 | 0 | 1 | 0 | 0 | | 5 | 1 | 1 | 0 | 0 | | 6 | 1 | 0 | 0 | 0 | | 7 | 1 | 0 | 0 | 1 | Notice that the pattern is essentially a rotation of bits. We can use this observation to simplify the implementation drastically. Here is a more concise version of the `stepper` function:
1 2 3 4 5 6 7 8 9 | void stepper(int xw) { for (int x = 0; x < xw; x++) { int s = (0b0001 << (x % 4)) | (0b1000 >> (x % 4)); digitalWrite(1, s & 0b0001); digitalWrite(2, s & 0b0010); digitalWrite(3, s & 0b0100); digitalWrite(4, s & 0b1000); } } |
1 2 3 4 5 6 7 8 9 | void stepper(int xw) { for (int x=0; x<xw; x++) { int s=0b10011001>>x%4; digitalWrite(1,s&1); digitalWrite(2,s>>1&1); digitalWrite(3,s>>2&1); digitalWrite(4,s>>3&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; x<xw; x++) { int s=0b10011001>>x%4; digitalWrite(1,s&1); digitalWrite(2,s>>1&1); digitalWrite(3,s>>2&1); digitalWrite(4,s>>3&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; x<xw; x++) { int s=0b10011001>>x%4; digitalWrite(1,s&1); digitalWrite(2,s>>1&1); digitalWrite(3,s>>2&1); digitalWrite(4,s>>3&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 408397632 instead of -1475775710.
main.c: In function 'stepper':
main.c:5:5: warning: implicit declaration of function 'digitalWrite' [-Wimplicit-function-declaration]
5 | digitalWrite(1,s&1);
| ^~~~~~~~~~~~
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; } } |
To code golf the `stepper` function, let's start by creating a table for the values being written to the pins for each step, from 0 to 7:
1 2 3 4 5 6 7 8 9 | Step | digitalWrite(1) | digitalWrite(2) | digitalWrite(3) | digitalWrite(4) 0 | 0 | 0 | 0 | 1 1 | 0 | 0 | 1 | 1 2 | 0 | 0 | 1 | 0 3 | 0 | 1 | 1 | 0 4 | 0 | 1 | 0 | 0 5 | 1 | 1 | 0 | 0 6 | 1 | 0 | 0 | 0 7 | 1 | 0 | 0 | 1 |
1 2 3 4 5 6 7 8 | void stepper(int xw) { for (int x = 0, Steps = 0; x < xw; x++, Steps = (Steps + 1) % 8) { digitalWrite(1, Steps >= 5); digitalWrite(2, Steps >= 3 && Steps <= 5); digitalWrite(3, Steps >= 1 && Steps <= 3); digitalWrite(4, Steps == 0 || Steps == 1 || Steps == 7); } } |
1 2 3 4 5 6 7 8 | void stepper(int xw) { for (int x = 0, s = 0; x < xw; x++, s = (s + 1) & 7) { digitalWrite(1, s > 4); digitalWrite(2, s > 2 && s < 6); digitalWrite(3, s > 0 && s < 4); digitalWrite(4, s < 2 || s == 7); } } |
1 2 3 4 5 6 7 8 | void stepper(int xw) { for (int x = 0, s = 0; x < xw; x++, s = (s + 1) & 7) { digitalWrite(1, s > 4); digitalWrite(2, s > 2 && s < 6); digitalWrite(3, s > 0 && s < 4); digitalWrite(4, s < 2 || s == 7); } } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 | void stepper(int xw) { for (int x = 0, s = 0; x < xw; x++, s = (s + 1) & 7) { digitalWrite(1, s > 4); digitalWrite(2, s > 2 && s < 6); digitalWrite(3, s > 0 && s < 4); digitalWrite(4, s < 2 || s == 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 | void stepper(int xw) { for (int x = 0, s = 0; x < xw; x++, s = (s + 1) & 7) { digitalWrite(1, s > 4); digitalWrite(2, s > 2 && s < 6); digitalWrite(3, s > 0 && s < 4); digitalWrite(4, s < 2 || s == 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"); } |
All tests passed
main.c: In function 'stepper':
main.c:4:5: warning: implicit declaration of function 'digitalWrite' [-Wimplicit-function-declaration]
4 | digitalWrite(1, s > 4);
| ^~~~~~~~~~~~
Testing if the previous output contains the string All tests passed
: True
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; } } |
To golf the `stepper` function, we first analyze the pattern in the calls to `digitalWrite` within the switch-case structure. The goal is to find a more compact way to represent this repetitive structure. ### Step 1: Create a Table of Values | Steps | Pin 1 | Pin 2 | Pin 3 | Pin 4 | |-------|-------|-------|-------|-------| | 0 | 0 | 0 | 0 | 1 | | 1 | 0 | 0 | 1 | 1 | | 2 | 0 | 0 | 1 | 0 | | 3 | 0 | 1 | 1 | 0 | | 4 | 0 | 1 | 0 | 0 | | 5 | 1 | 1 | 0 | 0 | | 6 | 1 | 0 | 0 | 0 | | 7 | 1 | 0 | 0 | 1 | ### Step 2: Find an Expression Observing the table, we can see that the pattern repeats every 8 steps. Each pin can be controlled by a specific bitwise operation on the `Steps` variable. ### Step 3 & 4: Write the Function as Short as Possible We can use bitwise operations to directly calculate the values for each pin based on the current step. The final golfed version of the stepper function is:
1 2 3 4 5 6 7 8 | void stepper(int xw) { for (int x = 0, Steps = 0; x < xw; x++, Steps = (Steps + 1) & 7) { digitalWrite(1, Steps < 2 || Steps == 7); digitalWrite(2, Steps > 2 && Steps < 6); digitalWrite(3, Steps > 0 && Steps < 4 || Steps == 5); digitalWrite(4, Steps % 4 < 2); } } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 | void stepper(int xw) { for (int x = 0, Steps = 0; x < xw; x++, Steps = (Steps + 1) & 7) { digitalWrite(1, Steps < 2 || Steps == 7); digitalWrite(2, Steps > 2 && Steps < 6); digitalWrite(3, Steps > 0 && Steps < 4 || Steps == 5); digitalWrite(4, Steps % 4 < 2); } } |
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 | void stepper(int xw) { for (int x = 0, Steps = 0; x < xw; x++, Steps = (Steps + 1) & 7) { digitalWrite(1, Steps < 2 || Steps == 7); digitalWrite(2, Steps > 2 && Steps < 6); digitalWrite(3, Steps > 0 && Steps < 4 || Steps == 5); digitalWrite(4, Steps % 4 < 2); } } #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 221689492 instead of -1475775710.
main.c: In function 'stepper':
main.c:4:5: warning: implicit declaration of function 'digitalWrite' [-Wimplicit-function-declaration]
4 | digitalWrite(1, Steps < 2 || Steps == 7);
| ^~~~~~~~~~~~
Testing if the previous output contains the string All tests passed
: False
Resulting in output: True