it's because I didn't bother to simplify the code any more when I spotted where is the slow part and how to rewrite it.
I think all these `do`-`while` loops must be possible to translate to `for`, and it'd be shorter after that.
Advent of Code 2017 #3
day 21. void fractal_art(string_view input){ const auto by_pattern = +[](char c){ return c == '.' || c == '#' || c == '/'; }; const auto no_slash = +[](string_view sv){ string s; copy_if(begin(sv), end(sv), back_inserter(s), [](char c){ return c != '/'; }); return move(s); }; unordered_map rules; for (string_view line : input | split(by_line)){ auto [from_, to_, _] = parse_n(line, by_pattern); string from = no_slash(from_), to = no_slash(to_); int8_t sz = 2 | (from.size() & 1); const auto rotations = [&]{ int8_t i = 0; do { rules[from] = to; string rot(from.size(), '\0'); for (int8_t j = 0; j < sz; ++j) for (int8_t k = 0; k < sz; ++k) rot[sz * (sz - k - 1) + j] = from[sz * j + k]; from = move(rot); } while (++i < 4); }; rotations(); if (sz == 3){ for (int8_t j = 0, sz2 = sz * sz; j < sz2; j += sz) swap(from[j], from[j + sz - 1]); rotations(); } } string buf1 = ".#...####", buf2, * from = &buf1, * to = &buf2; size_t sz = 3; const auto generate_art = [&](size_t n){ for (size_t i = 0, nsz, subsz; i < n; ++i, sz = nsz, swap(buf1, buf2)){ if (sz & 1) nsz = sz + sz / 3, subsz = 3; else nsz = sz + (sz >> 1), subsz = 2; to->resize(nsz * nsz); string subfrom(subsz * subsz, '\0'), subto; size_t sz2 = sz * sz, sz_subsz = sz * subsz, nsz_subsz = nsz * subsz; for (size_t j = 0, jj = 0; j < sz2; j += sz_subsz - sz, jj += nsz_subsz) for (size_t jend = j + sz; j < jend; j += subsz, jj += subsz + 1){ for (size_t a = 0, b = 0; a < sz_subsz; a += sz, b += subsz) for (size_t c = 0; c < subsz; ++c) subfrom[b + c] = (*from)[j + a + c]; subto = rules[subfrom]; for (size_t a = 0, b = 0; a < nsz_subsz + nsz; a += nsz, b += subsz + 1) for (size_t c = 0; c < subsz + 1; ++c) (*to)[jj + a + c] = subto[b + c]; } } }; generate_art(5); cout
I was going back to problem 22, interesting background for that: en.wikipedia.org
day 22. constexpr array sporifica_virus_dirs[4] = {{-1, 0, 3, 2, 1}, {1, 0, 2, 3, 0}, {0, -1, 0, 1, 3}, {0, 1, 1, 0, 2}};struct carrier_t { array xy; const array* dir; };uint8_t& at(unordered_map& grid, const array& xy) { return grid[*(const uint64_t*)&xy[0]]; };auto make_sporifica_virus(string_view input){ unordered_map grid; vector grid_center; for (string_view line : input | split(by_line)) grid_center.push_back(line); for (int32_t w2 = grid_center[0].size() / 2, h2 = grid_center.size() / 2, y = -h2; y
Hi magicgoose
Hi but please don't find me by IP address
I would never do that, say Hi to him for me.
nope, why would I?
Is he not your leader?
I'm a free man and I'm not a number