1.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include <str.h>
  6. int strength = 1;
  7. int sum = 0;
  8. int time = 1;
  9. int process() {
  10. if (time == 20 || time == 60 || time == 100
  11. || time == 140 || time == 180 || time == 220) {
  12. sum += strength * time;
  13. }
  14. }
  15. void tick(int value) {
  16. process();
  17. time++;
  18. strength += value;
  19. }
  20. int main() {
  21. FILE *fp = fopen("input", "r");
  22. while (true) {
  23. char *rawline = fgetline(fp);
  24. if (rawline == NULL) break;
  25. char *line = str_strip(rawline);
  26. free(rawline);
  27. char** words = str_split(line, ' ');
  28. if (words == NULL) {
  29. free(line);
  30. continue;
  31. }
  32. if (words[0] == NULL) {
  33. free(line); free(words);
  34. continue;
  35. }
  36. if (strcmp(words[0], "noop") == 0) {
  37. tick(0);
  38. } else if (strcmp(words[0], "addx") == 0) {
  39. tick(0);
  40. char *ptr;
  41. int value = strtol(words[1], &ptr, 10);
  42. tick(value);
  43. }
  44. str_list_free(words);
  45. free(line);
  46. }
  47. printf("%d\n", sum);
  48. return 0;
  49. }