day10.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int arraySize = 0;
  4. int **readAllJolts(FILE *fp) {
  5. int **allNumbers = NULL;
  6. int index = 0;
  7. while (1) {
  8. int *p = malloc(sizeof(int));
  9. if (!p) return NULL;
  10. if (1 > fscanf(fp, "%d\n", p))
  11. return NULL;
  12. arraySize++;
  13. int **newNumbers = realloc(allNumbers, sizeof(int*)*arraySize);
  14. if (!newNumbers) return NULL;
  15. allNumbers = newNumbers;
  16. allNumbers[index] = p;
  17. index++;
  18. if (feof(fp)) break;
  19. }
  20. return allNumbers;
  21. }
  22. int compare(const void *el1, const void *el2) {
  23. int *value1 = *(int**)el1;
  24. int *value2 = *(int**)el2;
  25. return (*value1 < *value2) ? -1 : (*value1 > *value2) ? 1 : 0;
  26. }
  27. char *calculateDistribution(int **array) {
  28. char *sequence = malloc(arraySize+1);
  29. if (!sequence) return NULL;
  30. int sequenceIndex = 0;
  31. int start = 0;
  32. int oneJolt = 0;
  33. int threeJolt = 0;
  34. int smallestIndex = 0;
  35. for (int i = 0; i < arraySize; i++) {
  36. if (*array[i] <= (start+3)) {
  37. int difference = i > 0 ? *array[i] - *array[i-1] : *array[i] - start;
  38. switch (difference) {
  39. case 1:
  40. sequence[sequenceIndex] = '1';
  41. oneJolt++;
  42. break;
  43. case 2:
  44. sequence[sequenceIndex] = '1';
  45. sequenceIndex++;
  46. sequence[sequenceIndex] = '1';
  47. break;
  48. case 3:
  49. sequence[sequenceIndex] = 'd';
  50. threeJolt++;
  51. break;
  52. }
  53. sequenceIndex++;
  54. if (smallestIndex != i) {
  55. if (*array[smallestIndex] > *array[i]) {
  56. smallestIndex = i;
  57. }
  58. }
  59. } else {
  60. start = *array[smallestIndex];
  61. smallestIndex = i;
  62. i--;
  63. }
  64. }
  65. threeJolt++;
  66. printf("Part1: %d\n", oneJolt * threeJolt);
  67. sequence[arraySize] = '\0';
  68. return sequence;
  69. }
  70. void freeAllNumbers(int **array) {
  71. for (int i = 0; i < arraySize; i++)
  72. free(array[i]);
  73. free(array);
  74. }
  75. long int processSequence(char *sequence) {
  76. long int result = 1;
  77. int ones = 0;
  78. for (int i = 0; i < arraySize+1; i++) {
  79. if (sequence[i] == '1') ones++;
  80. if (sequence[i] == 'd' || sequence[i] == '\0') {
  81. switch (ones) {
  82. case 2:
  83. result *= 2;
  84. break;
  85. case 3:
  86. result *= 4;
  87. break;
  88. case 4:
  89. result *= 7;
  90. break;
  91. }
  92. ones = 0;
  93. }
  94. }
  95. return result;
  96. }
  97. int main(int argc, char **argv)
  98. {
  99. if (argc < 2) {
  100. puts("You need to specify path to file!");
  101. return -1;
  102. }
  103. FILE *fp = fopen(argv[1], "r");
  104. if (!fp) {
  105. puts("Can't open file!");
  106. return -1;
  107. }
  108. int **allJolts = readAllJolts(fp);
  109. if (!allJolts) {
  110. puts("Can't read numbers!");
  111. fclose(fp);
  112. return -1;
  113. }
  114. qsort(allJolts, arraySize, sizeof(int*), compare);
  115. char *sequence = calculateDistribution(allJolts);
  116. printf("Part2: %ld\n", processSequence(sequence));
  117. free(sequence);
  118. freeAllNumbers(allJolts);
  119. fclose(fp);
  120. }