-
Notifications
You must be signed in to change notification settings - Fork 2
/
05.c
108 lines (93 loc) · 1.98 KB
/
05.c
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <assert.h>
#include <err.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Seat {
int row;
int column;
int seat;
};
static
int decode_row(const char* v) {
int l = 0;
int h = 127;
for (int8_t i = 0; i < 6; i++) {
int d = ((h + 1) - l) / 2;
if (v[i] == 'F') {
h -= d;
} else {
l += d;
}
}
int row = v[6] == 'F' ? l : h;
return row;
}
static
int decode_column(const char* v) {
int l = 0;
int h = 7;
for (int8_t i = 0; i < 2; i++) {
int d = ((h + 1) - l) / 2;
if (v[i] == 'L') {
h -= d;
} else {
l += d;
}
}
int column = v[2] == 'L' ? l : h;
return column;
}
static
struct Seat decode(const char* v) {
int r = decode_row(v);
int c = decode_column(v + 7);
struct Seat s = {
.column = c,
.row = r,
};
s.seat = (r * 8) + c;
return s;
}
int day5(void) {
assert(decode_row("FBFBBFFRLR") == 44);
assert(decode_column("RLR") == 5);
assert(decode("FBFBBFFRLR").seat == 357);
assert(decode("BFFFBBFRRR").seat == 567);
assert(decode("FFFBBBFRRR").seat == 119);
assert(decode("BBFFBBFRLL").seat == 820);
FILE* f = fopen("inputs/05.txt", "r");
if (!f) {
err(EXIT_FAILURE, "error reading input file");
}
char buf[BUFSIZ] = {0};
// find seat with highest seat ID
struct Seat s;
int max_seat_id = 0;
int8_t seats[128][8] = {0};
while ((fgets(buf, BUFSIZ, f) != NULL)) {
s = decode(buf);
if (s.seat > max_seat_id) {
max_seat_id = s.seat;
}
// mark seat as occupied for pt 2
seats[s.row][s.column] = 1;
}
fclose(f);
printf("%d\n", max_seat_id);
assert(max_seat_id == 933);
// find our seat (missing from list, not at (unknown) front or back)
int result = 0;
for (int8_t r = 10; r < 127; r++) {
for (int8_t c = 0; c < 8; c++) {
if (seats[r][c] == 0) {
result = r * 8 + c;
printf("%d\n", result);
assert(result == 711);
return 0;
}
}
}
return 1;
}