ziglings 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/bin/bash
  2. # ziglings takes one parameter: the exercise number to jump to
  3. jump_to=${1:-0}
  4. echo
  5. echo " _ _ _ "
  6. echo " ___(_) __ _| (_)_ __ __ _ ___ "
  7. echo " |_ | |/ _' | | | '_ \ / _' / __| "
  8. echo " / /| | (_| | | | | | | (_| \__ \ "
  9. echo " /___|_|\__, |_|_|_| |_|\__, |___/ "
  10. echo " |___/ |___/ "
  11. echo
  12. # Capture terminal escape sequences (ANSI) for formatting
  13. fmt_err=$( tput setaf 1 ) # red foreground
  14. fmt_yay=$( tput setaf 2 ) # green foreground
  15. fmt_off=$( tput sgr0 ) # reset colors/effects
  16. exercise_num=0
  17. function check_it {
  18. source_file="exercises/$1"
  19. correct_output=$2
  20. hint=$3
  21. # If the current exercise is less than the requested one, skip it
  22. let exercise_num+=1
  23. if [[ $exercise_num -lt $jump_to ]]
  24. then
  25. return
  26. fi
  27. # Compile/run the source and capture the result and exit value
  28. cmd="zig run $source_file"
  29. echo "$ $cmd"
  30. result=$($cmd 2>&1)
  31. result_status=$?
  32. # Echo the result to the screen so user can see what their program does
  33. echo "$result"
  34. if [[ $result_status -ne 0 ]]
  35. then
  36. echo
  37. printf "${fmt_err}Uh oh! Looks like there was an error.${fmt_off}\n"
  38. if [[ ! -z "$hint" ]]
  39. then
  40. echo "$hint"
  41. fi
  42. echo
  43. echo "Edit '$source_file' and run me again."
  44. echo
  45. exit 1
  46. fi
  47. # Wildcards to be lenient with anything AROUND the correct output
  48. if [[ "$result" == *"$correct_output"* ]]
  49. then
  50. printf "${fmt_yay}** PASSED **${fmt_off}\n"
  51. else
  52. printf "${fmt_err}It seems to compile, but I wanted to see '$correct_output'.${fmt_off}\n"
  53. if [[ ! -z "$hint" ]]
  54. then
  55. echo "$hint"
  56. fi
  57. echo
  58. exit 1
  59. fi
  60. }
  61. # I've chosen to explicitly number AND list each exercise rather than rely
  62. # on sorting. Though it does mean manually renaming things to remove/insert,
  63. # it's worked out well so far.
  64. check_it 01_hello.zig "Hello world" "Note the error: the source file has a hint for fixing 'main'."
  65. check_it 02_std.zig "Standard Library"
  66. check_it 03_assignment.zig "55 314159 -11" "There are three mistakes in this one!"
  67. check_it 04_arrays.zig "Fourth: 7, Length: 8" "There are two things to complete here."
  68. check_it 05_arrays2.zig "LEET: 1337, Bits: 100110011001" "Fill in the two arrays."
  69. check_it 06_strings.zig "d=d ha ha ha Major Tom" "Each '???' needs something filled in."
  70. check_it 07_strings2.zig "Ziggy" "Please fix the lyrics!"
  71. check_it 08_quiz.zig "Program in Zig" "See if you can fix the program!"
  72. check_it 09_if.zig "Foo is 1!"
  73. check_it 10_if2.zig "price is \$17"
  74. check_it 11_while.zig "n=1024" "You probably want a 'less than' condition."
  75. check_it 12_while2.zig "n=1024" "It might help to look back at the previous exercise."
  76. check_it 13_while3.zig "1 2 4 7 8 11 13 14 16 17 19"
  77. check_it 14_while4.zig "n=4"
  78. check_it 15_for.zig "A Dramatic Story: :-) :-) :-( :-| :-) The End."
  79. check_it 16_for2.zig "13"
  80. check_it 17_quiz2.zig "8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16" "This is a famous game!"
  81. check_it 18_functions.zig "Question: 42" "Can you help write the function?"
  82. check_it 19_functions2.zig "2 4 8 16"
  83. check_it 20_quiz3.zig "32 64 128 256" "Unexpected pop quiz! Help!"
  84. check_it 21_errors.zig "2<4. 3<4. 4=4. 5>4. 6>4." "What's the deal with fours?"
  85. check_it 22_errors2.zig "I compiled" "Get the error union type right to allow this to compile."
  86. check_it 23_errors3.zig "a=64, b=22"
  87. check_it 24_errors4.zig "a=20, b=14, c=10"
  88. check_it 25_errors5.zig "a=0, b=19, c=0"
  89. check_it 26_hello2.zig "Hello world" "Try using a try!"
  90. check_it 27_defer.zig "One Two"
  91. check_it 28_defer2.zig "(Goat) (Cat) (Dog) (Dog) (Goat) (Unknown) done."
  92. check_it 29_errdefer.zig "Getting number...got 5. Getting number...failed!"
  93. check_it 30_switch.zig "ZIG?"
  94. check_it 31_switch2.zig "ZIG!"
  95. check_it 32_unreachable.zig "1 2 3 9 8 7"
  96. check_it 33_iferror.zig "2<4. 3<4. 4=4. 5>4. 6>4." "Seriously, what's the deal with fours?"
  97. check_it 34_quiz4.zig "my_num=42" "Can you make this work?"
  98. check_it 35_enums.zig "1 2 3 9 8 7" "This problem seems familiar..."
  99. check_it 36_enums2.zig "#0000ff" "I'm feeling blue about this."
  100. check_it 37_structs.zig "Your wizard has 90 health and 25 gold."
  101. check_it 38_structs2.zig "Character 2 - G:10 H:100 XP:20"
  102. check_it 39_pointers.zig "num1: 5, num2: 5" "Pointers aren't so bad."
  103. check_it 40_pointers2.zig "a: 12, b: 12"
  104. check_it 41_pointers3.zig "foo=6, bar=11"
  105. check_it 42_pointers4.zig "num: 5, more_nums: 1 1 5 1"
  106. check_it 43_pointers5.zig "Wizard (G:10 H:100 XP:20)"
  107. check_it 44_quiz5.zig "Elephant A. Elephant B. Elephant C." "Oh no! We forgot Elephant B!"
  108. echo
  109. echo " __ __ _ "
  110. echo " \ \ / __ _ _ _| | "
  111. echo " \ V / _' | | | | | "
  112. echo " | | (_| | |_| |_| "
  113. echo " |_|\__,_|\__, (_) "
  114. echo " |___/ "
  115. echo
  116. echo "You've completed all of the Ziglings exercises!"
  117. echo " (That have been written so far.)"
  118. echo