1.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct listnode {
  4. struct listnode *next;
  5. char val;
  6. };
  7. typedef struct {
  8. struct listnode *head;
  9. } Stack;
  10. void stack_push(Stack *s, char val) {
  11. struct listnode *n = malloc(sizeof(struct listnode));
  12. n->next = s->head;
  13. n->val = val;
  14. s->head = n;
  15. }
  16. char* stack_top(Stack *s) {
  17. if (s->head == NULL) return NULL;
  18. return &(s->head->val);
  19. }
  20. void stack_pop(Stack *s) {
  21. if (s->head == NULL) return;
  22. struct listnode *next = s->head->next;
  23. free(s->head);
  24. s->head = next;
  25. }
  26. char buf[4096];
  27. int main() {
  28. Stack s[9];
  29. FILE *fp = fopen("input", "r");
  30. fread(buf, 1, 36 * 8, fp);
  31. for (int i = 0; i < 9; i++) {
  32. for (int j = 0; j < 8; j++) {
  33. char c = buf[(7 - j) * 36 + i * 4 + 1];
  34. if (c != ' ') {
  35. stack_push(&s[i], c);
  36. }
  37. }
  38. }
  39. fgets(buf, 4096, fp);
  40. fgets(buf, 4096, fp);
  41. int amount, from, to;
  42. while (fscanf(fp, "move %d from %d to %d\n", &amount, &from, &to) > 0) {
  43. for (int i = 0; i < amount; i++) {
  44. char c = *stack_top(&s[from - 1]);
  45. stack_pop(&s[from - 1]);
  46. stack_push(&s[to - 1], c);
  47. }
  48. }
  49. for (int i = 0; i < 9; i++) {
  50. printf("%c", *stack_top(&s[i]));
  51. }
  52. printf("\n");
  53. return 0;
  54. }