From aef7873e53f9aa2eba3912148b71868211d84770 Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Sat, 5 Dec 2020 14:42:45 +0100 Subject: Add alternative solution for day 2 --- 5/part2_fast.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 5/part2_fast.c (limited to '5/part2_fast.c') diff --git a/5/part2_fast.c b/5/part2_fast.c new file mode 100644 index 0000000..fe47ec2 --- /dev/null +++ b/5/part2_fast.c @@ -0,0 +1,57 @@ +#include + +#define COLUMNS 8 +#define ROWS 128 + +int get_seat_id(const char *seat) +{ + int end_res = 0; + int i = 0; + for (; i < 7; i++) { + if (seat[i] == 'B') { + end_res |= 0x200 >> i; + } + } + + for (; i < 10; i++) { + if (seat[i] == 'R') { + end_res |= 0x200 >> i; + } + } + + return end_res; +} + +int main(int argc, char *argv[]) +{ + FILE *file = fopen(argv[1], "r"); + + char table[COLUMNS * ROWS] = { 0 }; + + // Include space for newline and string terminator + char buffer[16] = { 0 }; + + int min = __INT_MAX__; + int max = 0; + while (fgets(buffer, 16, file)) { + int tmp = get_seat_id(buffer); + if (tmp > max) + { + max = tmp; + } + + if (tmp < min) + { + min = tmp; + } + + table[tmp] = 1; + } + + for (int i = min + 1; i < max; i++) { + if (table[i] == 0) { + printf("%i", i); + break; + } + } +} -- cgit v1.2.3