summary refs log tree commit diff
path: root/22.17/main.cpp
blob: a0046fc05d5e40865770a8e5058fb98cef9481f3 (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <cassert>
#include <iostream>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

uint8_t shapes[5][4] = {
	{
		0,
		0,
		0,
		0b00011110
	},
	{
		0,
		0b00001000,
		0b00011100,
		0b00001000,
	},
	{
		0,
		0b00000100,
		0b00000100,
		0b00011100,
	},
	{
		0b00010000,
		0b00010000,
		0b00010000,
		0b00010000,
	},
	{
		0,
		0,
		0b00011000,
		0b00011000,
	},
};

uint8_t
signedShift(uint8_t a, int b) {
	if (0 < b) return a >> b;
	else return a << -b;
}

bool
collide(vector<uint8_t> const& map, int shapeID, int x, int y)
{
	auto &shape = shapes[shapeID];
	// y increasing upwards
	for (int i = 0; i < 4; i++) {
		uint8_t line = shape[3 - i];
		if (signedShift(signedShift(line, x) & 0x7F, -x) != line) // wall coll
			return true;
		line = signedShift(line, x);
		if (!(y + i < map.size())) continue;
		if (line & map[y + i]) return true;
	}
	return false;
}

void
place(vector<uint8_t>& map, int shapeID, int x, int y)
{
	auto &shape = shapes[shapeID];
	for (int i = 0; i < 4; i++) {
		uint8_t line = signedShift(shape[3 - i], x);
		if (line == 0) break;
		if (!(y + i < map.size())) map.push_back(0);
		map[y + i] |= line;
	}
}

void
vis(vector<uint8_t> const& map)
{
	char l[9] = {0};
	for (int i = map.size() - 1; 0 <= i; i--) {
		for (int j = 0; j < 8; j++) {
			l[7 - j] = (map[i] & (1 << j)) ? '#' : '.';
		}
		cout << l << endl;
	}
	cout << endl;
}

int
getWind(string const& s, int step)
{
	char c = s[step % s.size()];
	if (c == '<') return -1;
	if (c == '>') return 1;
	cout << s << endl;
	throw "bad input";
}

/* from nullprogram */
uint32_t
lowbias32(uint32_t x)
{
    x ^= x >> 16;
    x *= 0x7feb352dU;
    x ^= x >> 15;
    x *= 0x846ca68bU;
    x ^= x >> 16;
    return x;
}

long
sim(string const& input, long blocks)
{
	unordered_map<uint32_t, pair<int, int>> prev_hash;
	int step = 0;
	long heightMod = 0;
	vector<uint8_t> map;
	map.push_back(0x7F);
	for (long i = 0; i < blocks; i++) {
		int shape = i % 5;
		int x = 0;
		int y = map.size() + 3;
		for (;;) {
			int wind = getWind(input, step++);
			if (!collide(map, shape, x + wind, y)) {
				x += wind;
			}
			if (collide(map, shape, x, y - 1)) {
				break;
			}
			y--;
		}
		place(map, shape, x, y);
		if (heightMod == 0 && i % (input.size() * 5) == 0) {
			uint32_t sh = 10;
			for (int i = 0; i < 50; i++) {
				if (map.size() - i - 1 < 0) continue;
				int el = map[map.size() - i - 1];
				sh = lowbias32(sh * (el + 1));
			}
			if (prev_hash.count(sh) != 0) {
				long blockRate = i - prev_hash[sh].first;
				long heightRate = map.size() - 1 - prev_hash[sh].second;
				long times = (blocks - i) / blockRate;
				i += times * blockRate;
				heightMod += times * heightRate;
				assert(i <= blocks);
				cerr << "skipped " << times << ", now " << i << "," << heightMod << endl;
			}
			prev_hash[sh] = {i, map.size() - 1};
		}
	}
	return map.size() - 1 + heightMod;
}

int
main()
{
	string input;
	getline(cin, input);
	cout << sim(input, 2022) << endl;
	cout << sim(input, 1000000000000) << endl;
}