aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-04-14 16:59:54 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-04-14 16:59:54 +0800
commitd94968612147691923e6b1dc5488414702f5ad8e (patch)
tree1ea929db7a11d2bd75df8e5647b7f7284c338906 /src
parentf04ebd945a7ef4480adee30dc1a48e6bfdb3a932 (diff)
downloadadvent-of-code-d94968612147691923e6b1dc5488414702f5ad8e.tar.gz
advent-of-code-d94968612147691923e6b1dc5488414702f5ad8e.zip
2016 day6 part1
Diffstat (limited to 'src')
-rw-r--r--src/2016/day6/aoc.cpp36
-rw-r--r--src/2016/day6/aoc.h1
-rw-r--r--src/2016/day6/input016
3 files changed, 53 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
diff --git a/src/2016/day6/aoc.h b/src/2016/day6/aoc.h
index 95823fe..2ab2c4c 100644
--- a/src/2016/day6/aoc.h
+++ b/src/2016/day6/aoc.h
@@ -3,4 +3,5 @@
namespace aoc2016 {
+void day6(line_view, char[]);
}
diff --git a/src/2016/day6/input0 b/src/2016/day6/input0
new file mode 100644
index 0000000..32ba518
--- /dev/null
+++ b/src/2016/day6/input0
@@ -0,0 +1,16 @@
+eedadn
+drvtee
+eandsr
+raavrd
+atevrs
+tsrnev
+sdttsa
+rasrtv
+nssdts
+ntnada
+svetve
+tesnvt
+vntsnd
+vrdear
+dvrsen
+enarar