diff options
-rw-r--r-- | src/2016/day8/README.md | 3 | ||||
-rw-r--r-- | src/2016/day8/aoc.cpp | 41 | ||||
-rw-r--r-- | src/2016/day8/aoc.h | 10 | ||||
-rw-r--r-- | test/test_2016.cpp | 2 |
4 files changed, 51 insertions, 5 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"); } diff --git a/test/test_2016.cpp b/test/test_2016.cpp index c61f26f..071a150 100644 --- a/test/test_2016.cpp +++ b/test/test_2016.cpp @@ -65,5 +65,5 @@ TEST_CASE("Internet Protocol Version 7", "[2016]") { TEST_CASE("Two-Factor Authentication", "[2016]") { line_view lv = load_file("../src/2016/day8/input"); auto p = aoc2016::day8(lv); - REQUIRE(0 == p); + REQUIRE(128 == p); } |