summaryrefslogtreecommitdiff
path: root/5/part2_fast.c
diff options
context:
space:
mode:
authorBond_009 <bond.009@outlook.com>2020-12-05 14:42:45 +0100
committerBond_009 <bond.009@outlook.com>2020-12-05 14:42:45 +0100
commitaef7873e53f9aa2eba3912148b71868211d84770 (patch)
tree209aebb81a727e09d1101ab12fefb23457c22a35 /5/part2_fast.c
parent518658a0a1541953594e440996c0d914f0128464 (diff)
Add alternative solution for day 2
Diffstat (limited to '5/part2_fast.c')
-rw-r--r--5/part2_fast.c57
1 files changed, 57 insertions, 0 deletions
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 <stdio.h>
+
+#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;
+ }
+ }
+}