diff options
Diffstat (limited to '2020/05/seatids.c')
-rw-r--r-- | 2020/05/seatids.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/2020/05/seatids.c b/2020/05/seatids.c new file mode 100644 index 0000000..9c3b797 --- /dev/null +++ b/2020/05/seatids.c @@ -0,0 +1,37 @@ +#include <math.h> +#include <stdio.h> + +/* Parse the input file `input` and print out all of the seat IDs which can be piped into both awk + * scripts + */ +int +main(void) +{ + FILE *fpt = fopen("input", "r"); + /* +2 for \n and \0 */ + char bpass[12]; + while (fgets(bpass, 12, fpt) != NULL) { + int lower_r = 0; + int upper_r = 127; + for (int i = 0; i < 7; i++) { + if (bpass[i] == 'F') + upper_r = (upper_r + lower_r) / 2; + else + lower_r = round((upper_r + lower_r) / 2); + } + + int lower_c = 0; + int upper_c = 7; + for (int i = 7; i < 10; i++) { + if (bpass[i] == 'L') + upper_c = (upper_c + lower_c) / 2; + else + lower_c = round((upper_c + lower_c) / 2); + } + + int seat_id = upper_r * 8 + upper_c; + printf("%d\n", seat_id); + } + + return 0; +} |