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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
#include "aoc.h"
#include <algorithm>
#include <unordered_map>
#include <vector>
namespace aoc2021 {
struct bnum {
int i;
int v;
bool operator<(const bnum& b) const noexcept { return v < b.v; }
};
static void get_number(const char** pp, int* d) {
const char* p = *pp;
while (*p >= '0' && *p <= '9') {
*d = *d * 10 + *p - '0';
p++;
}
*pp = p;
}
struct bingo {
std::unordered_map<int, int> ns;
std::vector<bnum> vs;
void parse(line_view lv) {
const char* p = lv.line;
int index{0};
while (p < lv.line + lv.length) {
if (*p >= '0' && *p <= '9') {
int d{0};
get_number(&p, &d);
vs.push_back({index++, d});
}
p++;
}
std::sort(vs.begin(), vs.end());
}
};
int search(int n, bingo& b) {
auto it = b.ns.find(n);
if (it != b.ns.end()) {
return it->second;
}
int left = 0;
int right = b.vs.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (b.vs[mid].v == n) {
int i = b.vs[mid].i;
b.ns.insert({n, i});
return i;
} else if (b.vs[mid].v < n) {
left = mid + 1;
} else if (b.vs[mid].v > n) {
right = mid - 1;
}
}
return INT32_MAX;
}
struct board {
bnum is[25];
bnum at;
board(line_view lv, bingo* b) {
const char* p = lv.line;
int index{0};
while (p < lv.line + lv.length) {
if (*p >= '0' && *p <= '9') {
is[index].v = 0;
get_number(&p, &(is[index].v));
is[index].i = search(is[index].v, *b);
index++;
}
p++;
}
at = bingo_at();
// print();
}
void print() const noexcept {
int i{0};
for (auto& b : is) {
printf("{%02d,%02d}", b.v, b.i);
if (++i % 5 == 0) {
printf("\n");
} else {
printf(" ");
}
}
printf("-> %d %d\n", at.v, at.i);
}
int sum_unmarked(int x) {
int s{0};
for (auto& n : is) {
if (n.i > x) {
// printf("%d ", n.v);
s += n.v;
}
}
return s;
}
bool operator<(const board& b) const noexcept { return at.i < b.at.i; }
bnum bingo_at() const noexcept {
auto is_bingo_r = [this](int y) -> bnum {
int m{INT32_MIN};
int v{0};
for (int x : {0, 1, 2, 3, 4}) {
auto p = is[y * 5 + x].i;
if (p == INT32_MAX) {
return {INT32_MAX, v};
} else {
if (p > m) {
m = p;
v = is[y * 5 + x].v;
}
}
}
// printf("row %d is {%d, %d}\n", y, v, m);
return {m, v};
};
auto is_bingo_c = [this](int x) -> bnum {
int m{INT32_MIN};
int v{0};
for (int y : {0, 1, 2, 3, 4}) {
auto p = is[y * 5 + x].i;
if (p == INT32_MAX) {
return {INT32_MAX, v};
} else {
if (p > m) {
m = p;
v = is[y * 5 + x].v;
}
}
}
// printf("col %d is {%d, %d}\n", x, v, m);
return {m, v};
};
bnum b{INT32_MAX, 0};
for (int i : {0, 1, 2, 3, 4}) {
bnum b1 = is_bingo_r(i);
bnum b2 = is_bingo_c(i);
bnum bx{0, 0};
bx.i = b1.i < b2.i ? b1.i : b2.i;
bx.v = b1.i < b2.i ? b1.v : b2.v;
if (bx.i < b.i) {
b = bx;
}
}
return b;
}
};
std::pair<int, int> day4(line_view file) {
const char* p1 = file.line;
const char* p2 = file.line + file.length;
const char* p = p1;
bingo b;
std::vector<board> bs;
while (p < p2) {
if (*p == '\n' && *(p + 1) == '\n') {
if (p1 == file.line) {
b.parse({p1, p});
} else {
bs.emplace_back(line_view{p1, p}, &b);
}
p1 = p + 2;
}
p++;
}
bs.emplace_back(line_view{p1, p}, &b);
std::sort(bs.begin(), bs.end());
// for (auto& b : bs) {
// printf("%d, %d\n", b.at.i, b.sum_unmarked(b.at.i) * b.at.v);
// }
auto score = [&bs](int x) { return bs[x].sum_unmarked(bs[x].at.i) * bs[x].at.v; };
return {score(0), score(bs.size() - 1)};
}
} // namespace aoc2021
|