From b366ef386c4e8884225c7eec3f28a7fc1eee8167 Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Fri, 22 Dec 2017 21:21:44 +0100 Subject: day21 --- day21.cc | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 day21.cc (limited to 'day21.cc') diff --git a/day21.cc b/day21.cc new file mode 100644 index 0000000..b6bf177 --- /dev/null +++ b/day21.cc @@ -0,0 +1,90 @@ +#include +#include +#include +#include + +using namespace std; + +using pic = vector>; + +pic parse(string s) { + int n = s.find("/"); + pic p(n, vector(n, '?')); + int y = 0, x = 0; + for (char c : s) + if (c == '/') { + y++; + x = 0; + } else { + p[y][x++] = c; + } + return p; +} + +pic rotate(pic p) { + pic np = p; + const int s = size(p); + for (int y = 0; y < s; y++) + for (int x = 0; x < s; x++) + np[x][s-1-y] = p[y][x]; + return np; +} + +int +main() { + string line; + + vector> book; + + while (getline(cin, line)) { + int i = line.find(" => "); + pic k = parse(line.substr(0, i)); + pic v = parse(line.substr(i + 4)); + + for (int i = 0; i < 4; i++, k = rotate(k)) { + book.emplace_back(k, v); + reverse(begin(k), end(k)); + book.emplace_back(k, v); + reverse(begin(k), end(k)); + } + } + + pic p{{'.','#','.'}, {'.','.','#'}, {'#','#','#'}}; + + for (int n = 1; n <= 18; n++) { + int s = 2 + size(p) % 2; + + pic np{(size(p)/s)*(s+1), vector((size(p)/s)*(s+1), '?')}; + + for (int y = 0; y < size(p); y += s) { + for (int x = 0; x < size(p); x += s) { + for (auto &[k, v] : book) { + if (size(k) != s) + goto next; + + for (int iy = 0; iy < s; iy++) + for (int ix = 0; ix < s; ix++) + if (p[y+iy][x+ix] != k[iy][ix]) + goto next; + + for (int iy = 0; iy < size(v); iy++) + for (int ix = 0; ix < size(v); ix++) + np[(y/s)*(s+1)+iy][(x/s)*(s+1)+ix] = v[iy][ix]; + goto done; + next:; + } + done:; + } + } + + if (n == 5 || n == 18) { + int cnt = 0; + for (auto row : np) + cnt += count(begin(row), end(row), '#'); + cout << cnt << endl; + } + + p.swap(np); + } + +} -- cgit 1.4.1