diff options
-rw-r--r-- | src/2015/day6/README.md | 19 | ||||
-rw-r--r-- | src/2015/day6/aoc.cpp | 9 | ||||
-rw-r--r-- | src/2015/day6/aoc.h | 40 | ||||
-rw-r--r-- | test/test_2015.cpp | 5 |
4 files changed, 70 insertions, 3 deletions
diff --git a/src/2015/day6/README.md b/src/2015/day6/README.md index e7207a2..3829ed2 100644 --- a/src/2015/day6/README.md +++ b/src/2015/day6/README.md @@ -16,4 +16,23 @@ For example: After following the instructions, how many lights are lit? +--- Part Two --- + +You just finish implementing your winning light pattern when you realize you mistranslated Santa's message from Ancient Nordic Elvish. + +The light grid you bought actually has individual brightness controls; each light can have a brightness of zero or more. The lights all start at zero. + +The phrase turn on actually means that you should increase the brightness of those lights by 1. + +The phrase turn off actually means that you should decrease the brightness of those lights by 1, to a minimum of zero. + +The phrase toggle actually means that you should increase the brightness of those lights by 2. + +What is the total brightness of all lights combined after following Santa's instructions? + +For example: + + turn on 0,0 through 0,0 would increase the total brightness by 1. + toggle 0,0 through 999,999 would increase the total brightness by 2000000. + diff --git a/src/2015/day6/aoc.cpp b/src/2015/day6/aoc.cpp index 480a050..deb812f 100644 --- a/src/2015/day6/aoc.cpp +++ b/src/2015/day6/aoc.cpp @@ -1,5 +1,12 @@ #include "aoc.h" namespace aoc2015 { -int day6(line_view lv) { return 0; } +int day6(line_view file) { + grid<1000> grid; + per_line(file, [&grid](line_view lv) { + grid.parse(lv); + return true; + }); + return grid.count(); +} } // namespace aoc2015 diff --git a/src/2015/day6/aoc.h b/src/2015/day6/aoc.h index 96fecca..70d69d9 100644 --- a/src/2015/day6/aoc.h +++ b/src/2015/day6/aoc.h @@ -45,7 +45,7 @@ struct grid { int count() const noexcept { int total = 0; for (uint64_t i : pool) { - total += __builtin_popcountll(i); + total += __builtin_popcountll(i); } return total; } @@ -68,6 +68,44 @@ struct grid { void toggle(unit u1, unit u2) { traverse(u1, u2, [this](int i, int j) { toggle(i, j); }); } + + std::pair<unit, unit> parse(const char* p1, const char* p2) { + unit u1; + unit u2; + int* is[] = {&u1.x, &u1.y, &u2.x, &u2.y}; + int i = 0; + const char* p = p1; + while (i < 4 && p < p2) { + int x = 0; + while (*p >= '0' && *p <= '9') { + x = x * 10 + *p - '0'; + p++; + } + *is[i++] = x; + while (*p < '0' || *p > '9') { + p++; + } + } + + return {u1, u2}; + } + + void parse(line_view lv) { + static struct _ { + void (grid::*action)(unit, unit); + const char* key; + } keywords[] = {{&grid::toggle, "toggle"}, {&grid::turn_off, "turn off"}, {&grid::turn_on, "turn on"}}; + + for (auto k : keywords) { + if (lv.contains(k.key)) { + const char* p1 = lv.line + strlen(k.key) + 1; + const char* p2 = lv.line + lv.length; + auto p = parse(p1, p2); + (this->*k.action)(p.first, p.second); + break; + } + } + } }; int day6(line_view); diff --git a/test/test_2015.cpp b/test/test_2015.cpp index f1d643b..151d84e 100644 --- a/test/test_2015.cpp +++ b/test/test_2015.cpp @@ -75,6 +75,9 @@ TEST_CASE("Doesn't He Have Intern-Elves For This?", "[day5]") { TEST_CASE("Probably a Fire Hazard", "[day6]") { aoc2015::grid<1000> grid; - grid.turn_on({0,0}, {0,999}); + grid.turn_on({0, 0}, {0, 999}); REQUIRE(grid.count() == 1000); + + line_view lv = load_file("../src/2015/day6/input"); + REQUIRE(543903 == aoc2015::day6(lv)); } |