shorten_c_function.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from evaluator import *
  2. DESCRIPTION = "Test if the model can significantly shorten a repetitive C functions."
  3. TAGS = ['code', 'c']
  4. question = """
  5. 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`.
  6. ```
  7. void stepper(int xw) {
  8. int Steps = 0;
  9. for (int x = 0; x < xw; x++) {
  10. switch (Steps) {
  11. case 0:
  12. digitalWrite(1, 0);
  13. digitalWrite(2, 0);
  14. digitalWrite(3, 0);
  15. digitalWrite(4, 1);
  16. break;
  17. case 1:
  18. digitalWrite(1, 0);
  19. digitalWrite(2, 0);
  20. digitalWrite(3, 1);
  21. digitalWrite(4, 1);
  22. break;
  23. case 2:
  24. digitalWrite(1, 0);
  25. digitalWrite(2, 0);
  26. digitalWrite(3, 1);
  27. digitalWrite(4, 0);
  28. break;
  29. case 3:
  30. digitalWrite(1, 0);
  31. digitalWrite(2, 1);
  32. digitalWrite(3, 1);
  33. digitalWrite(4, 0);
  34. break;
  35. case 4:
  36. digitalWrite(1, 0);
  37. digitalWrite(2, 1);
  38. digitalWrite(3, 0);
  39. digitalWrite(4, 0);
  40. break;
  41. case 5:
  42. digitalWrite(1, 1);
  43. digitalWrite(2, 1);
  44. digitalWrite(3, 0);
  45. digitalWrite(4, 0);
  46. break;
  47. case 6:
  48. digitalWrite(1, 1);
  49. digitalWrite(2, 0);
  50. digitalWrite(3, 0);
  51. digitalWrite(4, 0);
  52. break;
  53. case 7:
  54. digitalWrite(1, 1);
  55. digitalWrite(2, 0);
  56. digitalWrite(3, 0);
  57. digitalWrite(4, 1);
  58. break;
  59. default:
  60. digitalWrite(1, 0);
  61. digitalWrite(2, 0);
  62. digitalWrite(3, 0);
  63. digitalWrite(4, 0);
  64. break;
  65. }
  66. Steps = (Steps + 1) % 8;
  67. }
  68. }
  69. ```
  70. """
  71. test_case, answer = make_c_test([("(stepper(100),out)", "-1475775710")], extra_methods="""
  72. #include <stdio.h>
  73. int out = 0;
  74. int digitalWrite(int pin, int value) {
  75. out *= 101;
  76. out += pin;
  77. out *= 97;
  78. out += value;
  79. return out;
  80. }
  81. """)
  82. def short_enough(num):
  83. return lambda code: len(code.replace(" ", "").replace("\n", "").replace("\t", "")) < num
  84. 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."
  85. TestShortenCHard = question.replace("<300", "<200") >> LLMRun() >> ExtractCode() >> (PyFunc(short_enough(200)) & (CRun(test_case) >> SubstringEvaluator(answer)))
  86. TestShortenC = question >> LLMRun() >> ExtractCode() >> (PyFunc(short_enough(300)) & (CRun(test_case) >> SubstringEvaluator(answer)))
  87. TestShortenCStep = question + step >> LLMRun() >> ExtractCode() >> (PyFunc(short_enough(300)) & (CRun(test_case) >> SubstringEvaluator(answer)))
  88. if __name__ == "__main__":
  89. from llm import llm
  90. print(run_test(TestShortenCHard))