aboutsummaryrefslogtreecommitdiff
path: root/2020/05/seatids.c
blob: 9c3b79713d85c4b513bdf16d8b8a296ad8e0b072 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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;
}