summary refs log tree commit diff
path: root/22.17/main.cpp
blob: 6a5c0cb85be6e97b3a63fe64f137f6e8a104bf59 (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
#include <cassert>
#include <iostream>
#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";
}

int
main()
{
	string input;
	getline(cin, input);

	int step = 0;
	vector<uint8_t> map;
	map.push_back(0x7F);
	for (int i = 0; i < 2022; i++) {
		int shape = i % 5;
		int x = 0;
		int y = map.size() + 3;
		for (;;) {
			int wind;
			wind = getWind(input, step++);
			if (!collide(map, shape, x + wind, y)) {
				x += wind;
			}
			// cout << x << "," << y << endl;
			if (collide(map, shape, x, y - 1)) {
				break;
			}
			y--;
		}
		place(map, shape, x, y);
		// vis(map);
	}
	cout << map.size() - 1 << endl;
}