2.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include <str.h>
  6. char screen[6][40];
  7. int regx = 0;
  8. int time = 0;
  9. void process() {
  10. if (time >= 240) return;
  11. int delta = time % 40 - regx;
  12. if (delta <= 2 && delta >= 0) {
  13. screen[time / 40][time % 40] = '#';
  14. }
  15. }
  16. void tick(int value) {
  17. process();
  18. time++;
  19. regx += value;
  20. }
  21. int main() {
  22. memset(screen, '.', 240);
  23. FILE *fp = fopen("input", "r");
  24. while (true) {
  25. char *rawline = fgetline(fp);
  26. if (rawline == NULL) break;
  27. char *line = str_strip(rawline);
  28. free(rawline);
  29. char** words = str_split(line, ' ');
  30. if (words == NULL) {
  31. free(line);
  32. continue;
  33. }
  34. if (words[0] == NULL) {
  35. free(line); free(words);
  36. continue;
  37. }
  38. if (strcmp(words[0], "noop") == 0) {
  39. tick(0);
  40. } else if (strcmp(words[0], "addx") == 0) {
  41. tick(0);
  42. char *ptr;
  43. int value = strtol(words[1], &ptr, 10);
  44. tick(value);
  45. }
  46. str_list_free(words);
  47. free(line);
  48. }
  49. for (int i = 0; i < 6; i++) {
  50. for (int j = 0; j < 40; j++) {
  51. printf("%c", screen[i][j]);
  52. }
  53. printf("\n");
  54. }
  55. return 0;
  56. }