|
| 1 | +#include <assert.h> |
| 2 | + |
| 3 | +#include <numeric> |
| 4 | +#include <string> |
| 5 | +#include <vector> |
| 6 | + |
| 7 | +#include "../../aoc/aoc.hpp" |
| 8 | +#include "../../aoc/grid/grid.hpp" |
| 9 | +#include "../../aoc/ocr/ocr.hpp" |
| 10 | + |
| 11 | +using namespace std; |
| 12 | + |
| 13 | +const char FILL = '#'; |
| 14 | +const char EMPTY = ' '; |
| 15 | + |
| 16 | +vector<pair<int, int>> program(const vector<string>& input) { |
| 17 | + int x = 1; |
| 18 | + int cycles = 0; |
| 19 | + vector<pair<int, int>> v; |
| 20 | + for (const string& line : input) { |
| 21 | + const auto splits = aoc::split(line); |
| 22 | + if (splits[0] == "noop") { |
| 23 | + v.push_back(make_pair(cycles, x)); |
| 24 | + cycles++; |
| 25 | + } else if (splits[0] == "addx") { |
| 26 | + v.push_back(make_pair(cycles, x)); |
| 27 | + cycles++; |
| 28 | + v.push_back(make_pair(cycles, x)); |
| 29 | + cycles++; |
| 30 | + x += stoi(splits[1]); |
| 31 | + } |
| 32 | + } |
| 33 | + return v; |
| 34 | +} |
| 35 | + |
| 36 | +inline int check(const int cycles, const int x) { |
| 37 | + return cycles % 40 == 20 ? x * cycles : 0; |
| 38 | +} |
| 39 | + |
| 40 | +inline char draw(const int cycles, const int x) { |
| 41 | + return abs(x - cycles % 40) <= 1 ? FILL : EMPTY; |
| 42 | +} |
| 43 | + |
| 44 | +vector<string> getPixels(const vector<string>& input) { |
| 45 | + const auto& p = program(input); |
| 46 | + vector<char> pixels; |
| 47 | + for (const pair<int, int>& state : p) { |
| 48 | + pixels.push_back(draw(state.first, state.second)); |
| 49 | + } |
| 50 | + vector<string> grid; |
| 51 | + for (int i : aoc::Range::range(0, 240, 40)) { |
| 52 | + grid.push_back(string(pixels.begin() + i, pixels.begin() + i + 40)); |
| 53 | + } |
| 54 | + return grid; |
| 55 | +} |
| 56 | + |
| 57 | +string part1(const vector<string>& input) { |
| 58 | + const auto& p = program(input); |
| 59 | + int ans = accumulate(p.begin(), p.end(), 0, |
| 60 | + [](const int a, const pair<int, int>& p) { |
| 61 | + return a + check(p.first + 1, p.second); |
| 62 | + }); |
| 63 | + return to_string(ans); |
| 64 | +} |
| 65 | + |
| 66 | +string part2(const vector<string>& input) { |
| 67 | + const Grid<char> drawing = Grid<char>::from(getPixels(input)); |
| 68 | + DEBUG(drawing.toString()); |
| 69 | + return ocr::convert6(drawing, FILL, EMPTY); |
| 70 | +} |
| 71 | + |
| 72 | +// clang-format off |
| 73 | +const vector<string> TEST = { |
| 74 | + "addx 15", |
| 75 | + "addx -11", |
| 76 | + "addx 6", |
| 77 | + "addx -3", |
| 78 | + "addx 5", |
| 79 | + "addx -1", |
| 80 | + "addx -8", |
| 81 | + "addx 13", |
| 82 | + "addx 4", |
| 83 | + "noop", |
| 84 | + "addx -1", |
| 85 | + "addx 5", |
| 86 | + "addx -1", |
| 87 | + "addx 5", |
| 88 | + "addx -1", |
| 89 | + "addx 5", |
| 90 | + "addx -1", |
| 91 | + "addx 5", |
| 92 | + "addx -1", |
| 93 | + "addx -35", |
| 94 | + "addx 1", |
| 95 | + "addx 24", |
| 96 | + "addx -19", |
| 97 | + "addx 1", |
| 98 | + "addx 16", |
| 99 | + "addx -11", |
| 100 | + "noop", |
| 101 | + "noop", |
| 102 | + "addx 21", |
| 103 | + "addx -15", |
| 104 | + "noop", |
| 105 | + "noop", |
| 106 | + "addx -3", |
| 107 | + "addx 9", |
| 108 | + "addx 1", |
| 109 | + "addx -3", |
| 110 | + "addx 8", |
| 111 | + "addx 1", |
| 112 | + "addx 5", |
| 113 | + "noop", |
| 114 | + "noop", |
| 115 | + "noop", |
| 116 | + "noop", |
| 117 | + "noop", |
| 118 | + "addx -36", |
| 119 | + "noop", |
| 120 | + "addx 1", |
| 121 | + "addx 7", |
| 122 | + "noop", |
| 123 | + "noop", |
| 124 | + "noop", |
| 125 | + "addx 2", |
| 126 | + "addx 6", |
| 127 | + "noop", |
| 128 | + "noop", |
| 129 | + "noop", |
| 130 | + "noop", |
| 131 | + "noop", |
| 132 | + "addx 1", |
| 133 | + "noop", |
| 134 | + "noop", |
| 135 | + "addx 7", |
| 136 | + "addx 1", |
| 137 | + "noop", |
| 138 | + "addx -13", |
| 139 | + "addx 13", |
| 140 | + "addx 7", |
| 141 | + "noop", |
| 142 | + "addx 1", |
| 143 | + "addx -33", |
| 144 | + "noop", |
| 145 | + "noop", |
| 146 | + "noop", |
| 147 | + "addx 2", |
| 148 | + "noop", |
| 149 | + "noop", |
| 150 | + "noop", |
| 151 | + "addx 8", |
| 152 | + "noop", |
| 153 | + "addx -1", |
| 154 | + "addx 2", |
| 155 | + "addx 1", |
| 156 | + "noop", |
| 157 | + "addx 17", |
| 158 | + "addx -9", |
| 159 | + "addx 1", |
| 160 | + "addx 1", |
| 161 | + "addx -3", |
| 162 | + "addx 11", |
| 163 | + "noop", |
| 164 | + "noop", |
| 165 | + "addx 1", |
| 166 | + "noop", |
| 167 | + "addx 1", |
| 168 | + "noop", |
| 169 | + "noop", |
| 170 | + "addx -13", |
| 171 | + "addx -19", |
| 172 | + "addx 1", |
| 173 | + "addx 3", |
| 174 | + "addx 26", |
| 175 | + "addx -30", |
| 176 | + "addx 12", |
| 177 | + "addx -1", |
| 178 | + "addx 3", |
| 179 | + "addx 1", |
| 180 | + "noop", |
| 181 | + "noop", |
| 182 | + "noop", |
| 183 | + "addx -9", |
| 184 | + "addx 18", |
| 185 | + "addx 1", |
| 186 | + "addx 2", |
| 187 | + "noop", |
| 188 | + "noop", |
| 189 | + "addx 9", |
| 190 | + "noop", |
| 191 | + "noop", |
| 192 | + "noop", |
| 193 | + "addx -1", |
| 194 | + "addx 2", |
| 195 | + "addx -37", |
| 196 | + "addx 1", |
| 197 | + "addx 3", |
| 198 | + "noop", |
| 199 | + "addx 15", |
| 200 | + "addx -21", |
| 201 | + "addx 22", |
| 202 | + "addx -6", |
| 203 | + "addx 1", |
| 204 | + "noop", |
| 205 | + "addx 2", |
| 206 | + "addx 1", |
| 207 | + "noop", |
| 208 | + "addx -10", |
| 209 | + "noop", |
| 210 | + "noop", |
| 211 | + "addx 20", |
| 212 | + "addx 1", |
| 213 | + "addx 2", |
| 214 | + "addx 2", |
| 215 | + "addx -6", |
| 216 | + "addx -11", |
| 217 | + "noop", |
| 218 | + "noop", |
| 219 | + "noop", |
| 220 | +}; |
| 221 | +const vector<string> RESULT = { |
| 222 | + "## ## ## ## ## ## ## ## ## ## ", |
| 223 | + "### ### ### ### ### ### ### ", |
| 224 | + "#### #### #### #### #### ", |
| 225 | + "##### ##### ##### ##### ", |
| 226 | + "###### ###### ###### ####", |
| 227 | + "####### ####### ####### ", |
| 228 | +}; |
| 229 | +// clang-format on |
| 230 | + |
| 231 | +void samples() { |
| 232 | + assert(part1(TEST) == "13140"); |
| 233 | + assert(getPixels(TEST) == RESULT); |
| 234 | +} |
| 235 | + |
| 236 | +SMAIN(2022, 10) |
0 commit comments