diff options
author | Marvin Borner | 2020-12-16 16:47:48 +0100 |
---|---|---|
committer | Marvin Borner | 2020-12-16 16:47:48 +0100 |
commit | bdaa78883984f648bafa57fd61227ee1f30f05c5 (patch) | |
tree | 1d25f84d07deaa993e27f6010d7d0857edd9e55a /2020/16/solve.c | |
parent | 505bf2753662b56aa840ee388179ed97dfe8e46b (diff) |
Semi-working B
Diffstat (limited to '2020/16/solve.c')
-rw-r--r-- | 2020/16/solve.c | 51 |
1 files changed, 44 insertions, 7 deletions
diff --git a/2020/16/solve.c b/2020/16/solve.c index 119791b..0dd7665 100644 --- a/2020/16/solve.c +++ b/2020/16/solve.c @@ -16,8 +16,9 @@ int solve(FILE *fp) char *line = NULL; size_t len = 0; int paragraph = 0; - char name[64]; - char valid[256][128]; + char name[64] = { 0 }; + int ticket[64] = { 0 }; + int valid[256][128] = { 0 }; int invalid[4096] = { 0 }; struct rule rules[64] = { 0 }; int rule_cnt = 0, invalid_cnt = 0, valid_cnt = 0; @@ -33,6 +34,15 @@ int solve(FILE *fp) .low1 = low1, .high1 = high1, .low2 = low2, .high2 = high2 }; rules[rule_cnt++] = rule; + } else if (paragraph == 1) { + if (line[0] == 'y') + continue; + int num, ind = 0; + int bytes_now, bytes_consumed = 0; + while (sscanf(line + bytes_consumed, "%d,%n", &num, &bytes_now) == 1) { + ticket[ind++] = num; + bytes_consumed += bytes_now; + } } else if (paragraph == 2) { if (line[0] == 'n') continue; @@ -40,7 +50,8 @@ int solve(FILE *fp) int num; int bytes_now, bytes_consumed = 0; - int invalid_ticket = 0; + int valid_nums[128] = { 0 }; + int invalid_ticket = 0, valid_nums_index = 0; while (sscanf(line + bytes_consumed, "%d,%n", &num, &bytes_now) > 0) { int inc = 0; for (int i = 0; i < rule_cnt; i++) { @@ -58,10 +69,13 @@ int solve(FILE *fp) invalid_ticket = 1; } + if (!invalid_ticket) + valid_nums[valid_nums_index++] = num; bytes_consumed += bytes_now; } if (!invalid_ticket) - strcpy(valid[valid_cnt++], line); + memcpy(&valid[valid_cnt++], valid_nums, + valid_nums_index * sizeof(int)); } } @@ -72,11 +86,34 @@ int solve(FILE *fp) printf("%d\n", cnt); // Part two - for (int i = 0; i < valid_cnt; i++) { - //printf("%s", valid[i]); + int used_rules[256] = { 0 }; + int rule_used[64] = { 0 }; + for (int x = 0; x < rule_cnt; x++) { + for (int j = 0; j < rule_cnt; j++) { + cnt = 0; + struct rule rule = rules[j]; + for (int y = 0; y < valid_cnt; y++) { + int num = valid[y][x]; + if ((num >= rule.low1 && num <= rule.high1) || + (num >= rule.low2 && num <= rule.high2)) { + cnt++; + } + } + if (cnt == valid_cnt && !rule_used[j]) { + rule_used[j] = 1; + used_rules[x] = j; + break; + } + } } - return 0; + int res = 1; + for (int i = 0; i < rule_cnt; i++) { + if (used_rules[i] < 6) + res *= ticket[i]; + } + + return res; } int main(int argc, char *argv[]) |