From e7c9108b95e39d7ea5a29ae06d619c4727f11027 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Fri, 29 Oct 2021 23:02:39 +0200 Subject: Initial commit --- 2020/05/seatids.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 2020/05/seatids.c (limited to '2020/05/seatids.c') 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 +#include + +/* 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; +} -- cgit v1.2.3