55 lines
1.3 KiB
C
55 lines
1.3 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdbool.h>
|
|
#include "../lib/util.h"
|
|
|
|
int seat_id_from_boarding_pass(char *boarding_pass) {
|
|
int seat_id = 0;
|
|
while(*boarding_pass) {
|
|
if (*boarding_pass == 'B' || *boarding_pass == 'R') seat_id++;
|
|
seat_id <<= 1;
|
|
boarding_pass++;
|
|
}
|
|
seat_id >>= 1;
|
|
return seat_id;
|
|
}
|
|
|
|
int part_1() {
|
|
char *data_buffer = load_input();
|
|
char *boarding_pass = strtok(data_buffer, "\n");
|
|
int max_seat_id = 0;
|
|
do {
|
|
int seat_id = seat_id_from_boarding_pass(boarding_pass);
|
|
if (seat_id > max_seat_id) max_seat_id = seat_id;
|
|
} while (boarding_pass = strtok(NULL, "\n"));
|
|
|
|
free(data_buffer);
|
|
return max_seat_id;
|
|
}
|
|
|
|
int part_2() {
|
|
bool occupied_seats[1028] = {0};
|
|
char *data_buffer = load_input();
|
|
char *boarding_pass = strtok(data_buffer, "\n");
|
|
int max_seat_id = 0;
|
|
do {
|
|
int seat_id = seat_id_from_boarding_pass(boarding_pass);
|
|
occupied_seats[seat_id] = true;
|
|
if (seat_id > max_seat_id) max_seat_id = seat_id;
|
|
} while (boarding_pass = strtok(NULL, "\n"));
|
|
|
|
bool occupied_seat = occupied_seats[max_seat_id];
|
|
while(occupied_seat) {
|
|
max_seat_id--;
|
|
occupied_seat = occupied_seats[max_seat_id];
|
|
}
|
|
|
|
free(data_buffer);
|
|
return max_seat_id;
|
|
}
|
|
|
|
int main() {
|
|
printf("Part 1: %d\n", part_1());
|
|
printf("Part 2: %d\n", part_2());
|
|
}
|