day5.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define ROW_SIZE 11
  5. #define ROW_LIMIT 7
  6. #define COLUMN_LIMIT 10
  7. #define ROW_MAX 127
  8. #define COLUMN_MAX 7
  9. int arraySize = 0;
  10. char **readInput(char *path) {
  11. char **array = NULL;
  12. FILE *fp = fopen(path, "r");
  13. if (!fp) {
  14. puts("Can't read");
  15. return NULL;
  16. }
  17. int index = 0;
  18. while (1) {
  19. if (feof(fp)) break;
  20. char *p = malloc(ROW_SIZE);
  21. if (!p) return NULL;
  22. if (1 > fscanf(fp, "%s\n", p))
  23. return NULL;
  24. arraySize++;
  25. char **newArray = realloc(array, sizeof(char*)*arraySize);
  26. if (!newArray) return NULL;
  27. array = newArray;
  28. array[index] = p;
  29. index++;
  30. }
  31. fclose(fp);
  32. return array;
  33. }
  34. int establishRow(char *code) {
  35. int rowMin = 0;
  36. int current = 0;
  37. int rowMax = ROW_MAX;
  38. for (int i = 0; i < ROW_LIMIT; i++) {
  39. if (code[i] == 'F') {
  40. rowMax = ((rowMax - rowMin) / 2) + rowMin;
  41. current = rowMin;
  42. }
  43. if (code[i] == 'B') {
  44. rowMin = ((rowMax - rowMin) / 2) + rowMin + 1;
  45. current = rowMax;
  46. }
  47. }
  48. return current;
  49. }
  50. int establishColumn(char *code) {
  51. int columnMin = 0;
  52. int current = 0;
  53. int columnMax = COLUMN_MAX;
  54. for (int i = ROW_LIMIT; i < COLUMN_LIMIT; i++) {
  55. if (code[i] == 'L') {
  56. columnMax = ((columnMax - columnMin) / 2) + columnMin;
  57. current = columnMin;
  58. }
  59. if (code[i] == 'R') {
  60. columnMin = ((columnMax - columnMin) / 2) + columnMin + 1;
  61. current = columnMax;
  62. }
  63. }
  64. return current;
  65. }
  66. int highestID(char **array) {
  67. int highest = 0;
  68. for (int i = 0; i < arraySize; i++) {
  69. int current = (establishRow(array[i]) * 8) + establishColumn(array[i]);
  70. if (current > highest) highest = current;
  71. }
  72. return highest;
  73. }
  74. int **calculateIDs(char **array) {
  75. int **IDS = malloc(sizeof(int*)*arraySize);
  76. if (!IDS) return NULL;
  77. for (int i = 0; i < arraySize; i++) {
  78. int current = (establishRow(array[i]) * 8) + establishColumn(array[i]);
  79. IDS[i] = malloc(sizeof(int));
  80. if (!IDS[i]) return NULL;
  81. *IDS[i] = current;
  82. }
  83. return IDS;
  84. }
  85. int isTaken(int id, int **IDS) {
  86. for (int i = 0; i < arraySize; i++)
  87. if (*IDS[i] == id) return 1;
  88. return 0;
  89. }
  90. int findSeat(int **IDS) {
  91. for (int i = 0; i < arraySize; i++) {
  92. for (int j = i + 1; j < arraySize; j++) {
  93. if ((*IDS[i] - *IDS[j] == 2) || (*IDS[i] - *IDS[j] == -2)) {
  94. int min = *IDS[i] < *IDS[j] ? *IDS[i] : *IDS[j];
  95. int seat = min + 1;
  96. if (!isTaken(seat, IDS)) return seat;
  97. }
  98. }
  99. }
  100. return 0;
  101. }
  102. void freeArray(char **array) {
  103. for (int i = 0; i < arraySize; i++)
  104. free(array[i]);
  105. free(array);
  106. }
  107. void freeIDS(int **IDS) {
  108. for (int i = 0; i < arraySize; i++)
  109. free(IDS[i]);
  110. free(IDS);
  111. }
  112. int main(int argc, char **argv)
  113. {
  114. if (argc < 2) {
  115. puts("You need to specify path to file!");
  116. return -1;
  117. }
  118. char **array = readInput(argv[1]);
  119. if (!array) {
  120. puts("Can't read array!");
  121. return -1;
  122. }
  123. printf("Part1: %d\n", highestID(array));
  124. int **IDS = calculateIDs(array);
  125. if (!IDS) return -1;
  126. printf("Part2: %d\n", findSeat(IDS));
  127. freeArray(array);
  128. freeIDS(IDS);
  129. }