#include #include #include #include #include #define ROW_LENGTH 120 // array of card multipliers? // for multiplier: next hit multipliers ++ int main () { FILE *input; input = fopen("./input", "r"); int offset = 0; char c; int score = 0; int cardId = 0; int cardMultipliers[250] = {0}; char row[ROW_LENGTH]; // for each card (row in file) (numbers start after :) while (fgets(row, ROW_LENGTH, input) != NULL) { cardMultipliers[cardId]++; int winningNumbers[10] = {0}; int hits = 0; printf("%s\n", row); // offset = (strchr(row, '|') - &row[0]) - 2; offset = (strchr(row, ':') - &row[0] + 1); int foundNumbers = 0; for (int i = 0; row[offset + i] != '|'; i++) { if (isdigit(row[offset + i])) { while (isdigit(c = row[offset + i])) { winningNumbers[foundNumbers] = winningNumbers[foundNumbers] * 10 + (c - '0'); i++; } i--; printf("number %d: %d\n", foundNumbers, winningNumbers[foundNumbers]); foundNumbers++; } } offset = (strchr(row, '|') - &row[0] + 1); for (int i = 0; row[offset + i] != '\n'; i++) { if (isdigit(row[offset + i])) { int num = 0; while (isdigit(c = row[offset + i])) { num = num * 10 + (c - '0'); i++; } for (int i = 0; i < 10; i++) { if (num == winningNumbers[i]) hits++; } i--; } } printf("hits = %d\n", hits); for (int i = 0; i < cardMultipliers[cardId]; i++) { for (int n = 1; n <= hits; n++) { cardMultipliers[cardId + n]++; } } cardId++; } for (int i = 0; i < 250; i++) score += cardMultipliers[i]; printf("total cards: %d\n", score); }