about summary refs log tree commit diff
path: root/day21.cc
blob: b6bf1771f7b163fad415230febf6f2de18cc9869 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

using pic = vector<vector<char>>;

pic parse(string s) {
	int n = s.find("/");
	pic p(n, vector<char>(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<pair<pic, pic>> 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<char>((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);
	}

}