aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-05-09 18:22:21 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-05-09 18:22:21 +0800
commit2e5392ba4c93d5b9d5eb2a560418b13e76f2059f (patch)
tree5e3f4e40196eea0778a34a358f20ab8a1b396caf /src
parentdca2e70a10b7928c8a73f73d5dc7ef74f9001005 (diff)
downloadadvent-of-code-2e5392ba4c93d5b9d5eb2a560418b13e76f2059f.tar.gz
advent-of-code-2e5392ba4c93d5b9d5eb2a560418b13e76f2059f.zip
2016 day8
Diffstat (limited to 'src')
-rw-r--r--src/2016/day8/README.md3
-rw-r--r--src/2016/day8/aoc.cpp41
-rw-r--r--src/2016/day8/aoc.h10
3 files changed, 50 insertions, 4 deletions
diff --git a/src/2016/day8/README.md b/src/2016/day8/README.md
index 33334dc..d664496 100644
--- a/src/2016/day8/README.md
+++ b/src/2016/day8/README.md
@@ -42,4 +42,7 @@ As you can see, this display technology is extremely powerful, and will soon dom
There seems to be an intermediate check of the voltage used by the display: after you swipe your card, if the screen did work, how many pixels should be lit?
+--- Part Two ---
+You notice that the screen is only capable of displaying capital letters; in the font it uses, each letter is 5 pixels wide and 6 tall.
+After you swipe your card, what code is the screen trying to display?
diff --git a/src/2016/day8/aoc.cpp b/src/2016/day8/aoc.cpp
index fd56a51..573e7c1 100644
--- a/src/2016/day8/aoc.cpp
+++ b/src/2016/day8/aoc.cpp
@@ -2,10 +2,45 @@
namespace aoc2016 {
+static void get_number(const char** pp, int* d) {
+ const char* p = *pp;
+ *d = 0;
+ while (*p >= '0' && *p <= '9') {
+ *d = *d * 10 + *p - '0';
+ p++;
+ }
+ *pp = p;
+}
+
+static void get_number(const char* p1, const char* p2, int* ds) {
+ int index{0};
+ while (p1 < p2) {
+ if (*p1 >= '0' && *p1 <= '9') {
+ get_number(&p1, &ds[index++]);
+ }
+ p1++;
+ }
+}
+
int day8(line_view file) {
- grid<5, 6> g;
- g.set(2, 2);
- return 0;
+ grid<50, 6> g;
+ per_line(file, [&g](line_view lv) {
+ const char* p = lv.line;
+ int ds[2] = {0};
+ get_number(p, p + lv.length, ds);
+ if (p[1] == 'e') {
+ g.set(ds[0], ds[1]);
+ }
+ if (p[1] == 'o' && p[7] == 'c') {
+ g.rotate_v(ds[0], ds[1]);
+ }
+ if (p[1] == 'o' && p[7] == 'r') {
+ g.rotate_h(ds[0], ds[1]);
+ }
+ // g.print();
+ return true;
+ });
+ return g.count();
}
} // namespace aoc2016
diff --git a/src/2016/day8/aoc.h b/src/2016/day8/aoc.h
index d92a8f7..2313bc9 100644
--- a/src/2016/day8/aoc.h
+++ b/src/2016/day8/aoc.h
@@ -7,9 +7,17 @@ template <size_t W, size_t H>
struct grid {
char cells[W * H] = {0};
+ int count() const noexcept {
+ int total{0};
+ for (size_t i = 0; i < ARRAY_SIZE(cells); i++) {
+ total += int(cells[i] == 1);
+ }
+ return total;
+ }
+
void print() {
for (size_t i = 0; i < ARRAY_SIZE(cells); i++) {
- printf("%d", cells[i]);
+ printf("%s", cells[i] == 0 ? "." : "#");
if (i % W == W - 1) {
printf("\n");
}