-
-
Save mustafaquraish/1ce20abb07cf6f1733ae1d2d524ec6cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
void parse(char *line, char entries[10], char outs[4]) { | |
for (int i = 0; i < 10; i++) { | |
char *word = strsep(&line, " "); | |
for (char *c = word; *c; c++) | |
entries[i] |= 1 << (*c - 'a'); | |
} | |
line += 2; | |
for (int i = 0; i < 4; i++) { | |
char *word = strsep(&line, " "); | |
for (char *c = word; *c; c++) | |
outs[i] |= 1 << (*c - 'a'); | |
} | |
} | |
char num_bits(char x) { | |
char count = 0; | |
for (; x; x >>= 1) | |
count += x & 1; | |
return count; | |
} | |
char find_where(char entries[10], char len, char cmp, char match_bits, char na, char nb) { | |
for (int i = 0; i < 10; i++) { | |
if (num_bits(entries[i]) == len && | |
num_bits(entries[i] & cmp) == match_bits && | |
entries[i] != na && entries[i] != nb) { | |
return entries[i]; | |
} | |
} | |
return 0; | |
} | |
void solve_digits(char entries[10], char outs[4], char solns[4]) { | |
char D[10]; | |
D[1] = find_where(entries, 2, 0, 0, 0, 0); | |
D[4] = find_where(entries, 4, 0, 0, 0, 0); | |
D[7] = find_where(entries, 3, 0, 0, 0, 0); | |
D[8] = find_where(entries, 7, 0, 0, 0, 0); | |
D[3] = find_where(entries, 5, D[7], 3, 0, 0); | |
D[9] = find_where(entries, 6, D[3], 5, 0, 0); | |
D[6] = find_where(entries, 6, D[1], 1, 0, 0); | |
D[5] = find_where(entries, 5, D[6], 5, 0, 0); | |
D[0] = find_where(entries, 6, 0, 0, D[6], D[9]); | |
D[2] = find_where(entries, 5, 0, 0, D[5], D[3]); | |
for (int j = 0; j < 4; ++j) | |
for (int i = 0; i < 10; ++i) | |
if (outs[j] == D[i]) { | |
solns[j] = i; | |
break; | |
} | |
} | |
void part1(char outs[4], long *res) { | |
for (int i = 0; i < 4; i++) { | |
char num = num_bits(outs[i]); | |
*res += (num == 2 || num == 3 || num == 4 || num == 7); | |
} | |
} | |
void part2(char entries[10], char outs[4], long *res) { | |
long val; | |
char solns[4]; | |
solve_digits(entries, outs, solns); | |
for (int i = val = 0; i < 4; ++i) | |
val = val * 10 + solns[i]; | |
*res += val; | |
} | |
int main() { | |
FILE *f = fopen("08.txt", "r"); | |
char buffer[256]; | |
long res1 = 0, res2 = 0; | |
while (fgets(buffer, 256, f)) { | |
char entries[10] = {0}, outs[4] = {0}; | |
parse(buffer, entries, outs); | |
part1(outs, &res1); | |
part2(entries, outs, &res2); | |
} | |
printf("Part 1: %lu, Part 2: %lu\n", res1, res2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's so short and looks so nice. I hope to code like that in the future