aboutsummaryrefslogtreecommitdiff
path: root/src/2016/day6/aoc.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/2016/day6/aoc.cpp')
-rw-r--r--src/2016/day6/aoc.cpp36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/2016/day6/aoc.cpp b/src/2016/day6/aoc.cpp
index 55f4247..521ca55 100644
--- a/src/2016/day6/aoc.cpp
+++ b/src/2016/day6/aoc.cpp
@@ -2,4 +2,40 @@
namespace aoc2016 {
+struct counter {
+ int az[26] = {0};
+
+ void add(char c) { az[c - 'a'] += 1; }
+ char most() {
+ int max{INT32_MIN};
+ int index{0};
+ for (int i = 0; i < 26; i++) {
+ if (az[i] > max) {
+ max = az[i];
+ index = i;
+ }
+ }
+ return 'a' + index;
+ }
+};
+
+void day6(line_view file, char msg[]) {
+ counter cs[8];
+ per_line(file, [&cs](line_view lv) {
+ const char* p = lv.line;
+ int index{0};
+ while (p < lv.line + lv.length) {
+ if (*p >= 'a' && *p <= 'z') {
+ cs[index++].add(*p);
+ }
+ p++;
+ }
+ return true;
+ });
+
+ for (size_t i = 0; i < ARRAY_SIZE(cs); i++) {
+ msg[i] = cs[i].most();
+ }
}
+
+} // namespace aoc2016