diff options
Diffstat (limited to '2015/src/day06.cpp')
| -rw-r--r-- | 2015/src/day06.cpp | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/2015/src/day06.cpp b/2015/src/day06.cpp new file mode 100644 index 0000000..b7f15b5 --- /dev/null +++ b/2015/src/day06.cpp | |||
| @@ -0,0 +1,114 @@ | |||
| 1 | #include <algorithm> | ||
| 2 | #include <cstdio> | ||
| 3 | #include <fstream> | ||
| 4 | #include <iostream> | ||
| 5 | #include <map> | ||
| 6 | #include <numeric> | ||
| 7 | #include <string> | ||
| 8 | #include <tuple> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | using namespace std; | ||
| 12 | |||
| 13 | enum Operation { | ||
| 14 | On, | ||
| 15 | Off, | ||
| 16 | Toggle | ||
| 17 | }; | ||
| 18 | |||
| 19 | using instruction = tuple<Operation, int, int, int, int>; | ||
| 20 | |||
| 21 | auto | ||
| 22 | read_file(string_view filename) | ||
| 23 | { | ||
| 24 | fstream input{ filename }; | ||
| 25 | vector<instruction> instructions; | ||
| 26 | |||
| 27 | for ( string line; getline(input, line); ) { | ||
| 28 | int x0, y0, x1, y1; // NOLINT | ||
| 29 | |||
| 30 | if ( sscanf(line.c_str(), "turn on %d,%d through %d,%d", &x0, &y0, &x1, &y1) == 4 ) { // NOLINT | ||
| 31 | instructions.emplace_back(Operation::On, x0, y0, x1, y1); | ||
| 32 | } | ||
| 33 | else if ( sscanf(line.c_str(), "toggle %d,%d through %d,%d", &x0, &y0, &x1, &y1) == 4 ) { // NOLINT | ||
| 34 | instructions.emplace_back(Operation::Toggle, x0, y0, x1, y1); | ||
| 35 | } | ||
| 36 | else if ( sscanf(line.c_str(), "turn off %d,%d through %d,%d", &x0, &y0, &x1, &y1) == 4 ) { // NOLINT | ||
| 37 | instructions.emplace_back(Operation::Off, x0, y0, x1, y1); | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | return instructions; | ||
| 42 | } | ||
| 43 | |||
| 44 | // clang-format off | ||
| 45 | static void turn_on(int& value) { value = 1; } | ||
| 46 | static void turn_off(int& value) { value = 0; } | ||
| 47 | static void toggle(int& value) { value ^= 1; } | ||
| 48 | // clang-format on | ||
| 49 | |||
| 50 | void | ||
| 51 | part1(const vector<instruction>& instructions) | ||
| 52 | { | ||
| 53 | map<Operation, function<void(int&)>> ops = { | ||
| 54 | { Operation::On, turn_on }, | ||
| 55 | { Operation::Toggle, toggle }, | ||
| 56 | { Operation::Off, turn_off } | ||
| 57 | }; | ||
| 58 | |||
| 59 | map<tuple<int, int>, int> lights; | ||
| 60 | |||
| 61 | for ( const auto& [operation, x0, y0, x1, y1]: instructions ) { | ||
| 62 | for ( int x = x0; x <= x1; ++x ) { | ||
| 63 | for ( int y = y0; y <= y1; ++y ) { | ||
| 64 | ops[operation](lights[{ x, y }]); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | auto sum = count_if(begin(lights), end(lights), [](const auto& light) { | ||
| 70 | return light.second == 1; | ||
| 71 | }); | ||
| 72 | |||
| 73 | cout << sum << endl; | ||
| 74 | } | ||
| 75 | |||
| 76 | // clang-format off | ||
| 77 | static void increase(int& value) { ++value; } | ||
| 78 | static void decrease(int& value) { if (value > 0) { --value; } } | ||
| 79 | static void increase_by_2(int& value) { value += 2; } | ||
| 80 | // clang-format on | ||
| 81 | |||
| 82 | void | ||
| 83 | part2(const vector<instruction>& instructions) | ||
| 84 | { | ||
| 85 | map<Operation, function<void(int&)>> ops = { | ||
| 86 | { Operation::On, increase }, | ||
| 87 | { Operation::Toggle, increase_by_2 }, | ||
| 88 | { Operation::Off, decrease } | ||
| 89 | }; | ||
| 90 | |||
| 91 | map<tuple<int, int>, int> lights; | ||
| 92 | |||
| 93 | for ( const auto& [operation, x0, y0, x1, y1]: instructions ) { | ||
| 94 | for ( int x = x0; x <= x1; ++x ) { | ||
| 95 | for ( int y = y0; y <= y1; ++y ) { | ||
| 96 | ops[operation](lights[{ x, y }]); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | auto sum = accumulate(begin(lights), end(lights), 0, [](int value, const auto& light) { | ||
| 102 | return value + light.second; | ||
| 103 | }); | ||
| 104 | |||
| 105 | cout << sum << endl; | ||
| 106 | } | ||
| 107 | |||
| 108 | int | ||
| 109 | main() | ||
| 110 | { | ||
| 111 | auto instructions = read_file("data/day06.txt"); | ||
| 112 | part1(instructions); | ||
| 113 | part2(instructions); | ||
| 114 | } | ||
