summaryrefslogtreecommitdiff
path: root/17.c
blob: 67230fbcc8e7c95f05f6f3a105e8cffcb8487ac7 (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
163
164
165
166
167
168
169
170
171
172
173
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define arrlen(x) (sizeof(x)/sizeof((x)[0]))


struct rock {
	unsigned char s[4];
};


struct rock const rocks[] = {
	{ { 0x1e, 0, 0, 0 } },		// Horizontal line
	{ { 0x08, 0x1c, 0x08, 0 } },	// Cross
	{ { 0x1c, 0x04, 0x04, 0 } },	// Elbow
	{ { 0x10, 0x10, 0x10, 0x10 } },	// Line piece
	{ { 0x18, 0x18, 0, 0 } },	// Block
};


#if 0
void
chamber_dump(unsigned char * chamber, int chamberheight, struct rock * rock, int rockheight)
{
	int i;

	for (i = chamberheight - 1; i >= 0; --i) {
		unsigned char c;

		fputc('|', stderr);

		for (c = 0x40; c != 0; c >>= 1) {
			if (rock && i >= rockheight
					&& i < rockheight + arrlen(rock->s)
					&& (rock->s[i - rockheight] & c))
				fputc('@', stderr);
			else if (chamber && (chamber[i] & c))
				fputc('#', stderr);
			else
				fputc('.', stderr);
		}


		fputc('|', stderr);
		fputc('\n', stderr);
	}
	fprintf(stderr, "+-------+\n");
}
#endif


int
main()
{
	size_t jetsize = 0;
	char * jets = NULL;
	unsigned char * chamber = NULL;
	int chamberheight = 0, top = 0;
	int movement, rocknum, rockheight;
	struct rock rock;
	int i;

	// Read jets.
	while (1) {
		void * p;
		size_t s;

		if ((p = realloc(jets, jetsize + BUFSIZ)) == NULL) {
			fprintf(stderr, "jets: bad realloc (%zu)\n", jetsize + BUFSIZ);
			free(jets);
			return -1;
		}
		jets = p;
		if ((s = fread(jets + jetsize, 1, BUFSIZ, stdin)) == 0)
			break;
		jetsize += s;
	}

	// Remove any characters that are not '<' or '>' (e.g. newlines)
	for (i = 0; i < jetsize; ++i) {
		while (i < jetsize && jets[i] != '<' && jets[i] != '>') {
			memmove(jets + i, jets + i + 1, jetsize - (i + 1));
			--jetsize;
		}
	}

	// Place first rock, and start movement
	rocknum = 0;
	rock = rocks[rocknum];
	rockheight = 3;

	for (movement = 0; rocknum < 2022; ++movement) {
		int jet;

		if (top > chamberheight - 10) {
			void * p;

			if ((p = realloc(chamber, chamberheight + BUFSIZ)) == NULL) {
				fprintf(stderr, "Bad realloc(%d)\n", chamberheight + BUFSIZ);
				free(chamber);
				return -1;
			}
			chamber = p;
			memset(chamber + chamberheight, 0, BUFSIZ);
			chamberheight += BUFSIZ;
		}

		jet = jets[movement % jetsize];

		// Check if jet can push rock.
		for (i = 0; i < arrlen(rock.s); ++i) {
			// Check left wall collision
			if (jet == '<' && (rock.s[i] & 0x40) != 0)
				jet = 0;

			// Check right wall collision
			if (jet == '>' && (rock.s[i] & 0x01) != 0)
				jet = 0;

			// Check moving left into existing rocks.
			if (jet == '<' && ((rock.s[i] << 1) & chamber[rockheight + i]) != 0)
				jet = 0;

			// Check moving right into existing rocks
			if (jet == '>' && ((rock.s[i] >> 1) & chamber[rockheight + i]) != 0)
				jet = 0;
		}

		// If jet can push rock, do so.
		if (jet == '<')
			for (i = 0; jet && i < arrlen(rock.s); ++i)
				rock.s[i] <<= 1;
		if (jet == '>')
			for (i = 0; jet && i < arrlen(rock.s); ++i)
				rock.s[i] >>= 1;

		// Check if rock can fall.
		for (i = 0; rockheight > 0 && i < arrlen(rock.s); ++i)
			if ((rock.s[i] & chamber[rockheight - 1 + i]) != 0)
				break;
		if (i < arrlen(rock.s)) {
			// Rock cannot fall. Place rock.
			for (i = 0; i < arrlen(rock.s) && rock.s[i] != 0; ++i) {
				chamber[rockheight + i] |= rock.s[i];
				if (rockheight + i > top)
					top = rockheight + i;
			}

			// Pick next rock.
			rock = rocks[++rocknum % arrlen(rocks)];
			rockheight = top + 4;

#if 0
			chamber_dump(chamber, top + 7, &rock, rockheight);
#endif
		}
		else {
			// Rock can fall, so reduce its height by 1
			--rockheight;
		}
	}

	printf("Height of rocks in chamber: %d\n", top + 1);

	free(chamber);
	free(jets);

	return 0;
}