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
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
from evaluator import *

DESCRIPTION = "Test if the model can significantly shorten a repetitive C functions."

TAGS = ['code', 'c']

question = """
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`.

```
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;
  }
}
```
"""

test_case, answer = make_c_test([("(stepper(100),out)", "-1475775710")], extra_methods="""
#include <stdio.h>

int out = 0;
int digitalWrite(int pin, int value) {
  out *= 101;
  out += pin;
  out *= 97;
  out += value;
  return out;
}
""")

def short_enough(num):
    return lambda code: len(code.replace(" ", "").replace("\n", "").replace("\t", "")) < num

step = "\nTo do this follow these steps: \n1. start out by creating a table of the values that are being written to the pins.\n2. Then find an expression for this.\n3. Write the function as short as possible.\n4. See if you can make it any shorter. Return this as the final answer and call it your final answer. Make sure to name it stepper still."

TestShortenCHard = question.replace("<300", "<200") >> LLMRun() >> ExtractCode() >> (PyFunc(short_enough(200)) & (CRun(test_case) >> SubstringEvaluator(answer)))
TestShortenC = question >> LLMRun() >> ExtractCode() >> (PyFunc(short_enough(300)) & (CRun(test_case) >> SubstringEvaluator(answer)))
TestShortenCStep = question + step >> LLMRun() >> ExtractCode() >> (PyFunc(short_enough(300)) & (CRun(test_case) >> SubstringEvaluator(answer)))


if __name__ == "__main__":
    from llm import llm
    print(run_test(TestShortenCHard))