aboutsummaryrefslogtreecommitdiff
path: root/2016/src/day25.cpp
blob: ba3ad04994761b234efbb1658d657bef240f0d17 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <filesystem>
#include <fstream>
#include <iostream>
#include <map>
#include <optional>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

namespace {

enum class OpCode : uint8_t {
	CPY,
	INC,
	DEC,
	JNZ,
	OUT,
};

using memory_type = map<char, int>;

struct Argument {
	explicit Argument(string value)
	    : value{ std::move(value) }
	{
	}

	[[nodiscard]]
	int eval(const memory_type& memory) const
	{
		const auto chr = value.at(0);

		if ( chr >= 'a' && chr <= 'z' ) {
			return memory.at(chr);
		}
		else {
			return stoi(value);
		}
	}

	[[nodiscard]]
	char reg() const
	{
		if ( !is_reg() ) {
			throw runtime_error("invalid register access! "s + value);
		}

		return value.at(0);
	}

	[[nodiscard]]
	bool is_reg() const
	{
		const auto chr = value.at(0);

		return chr >= 'a' && chr <= 'z';
	}

	string value;
};

struct Instruction {
	Instruction(OpCode opCode, const Argument& x)
	    : opCode{ opCode }
	    , x{ x }
	{
	}

	Instruction(OpCode opCode, const Argument& x, const Argument& y)
	    : opCode{ opCode }
	    , x{ x }
	    , y{ y }
	{
	}

	OpCode             opCode;
	optional<Argument> x;
	optional<Argument> y;
};

vector<string>
split_words(const string& line, char sep = ' ')
{
	vector<string> words;
	stringstream   strm{ line };

	for ( string word; getline(strm, word, sep); ) {
		words.emplace_back(word);
	}

	return words;
}

vector<Instruction>
read_file(const filesystem::path& filename)
{
	ifstream input{ filename };

	vector<Instruction> instructions;

	for ( string line; getline(input, line); ) {
		const auto words = split_words(line);

		if ( words.at(0) == "cpy" ) {
			instructions.emplace_back(OpCode::CPY, Argument(words.at(1)), Argument(words.at(2)));
		}
		else if ( words.at(0) == "inc" ) {
			instructions.emplace_back(OpCode::INC, Argument(words.at(1)));
		}
		else if ( words.at(0) == "dec" ) {
			instructions.emplace_back(OpCode::DEC, Argument(words.at(1)));
		}
		else if ( words.at(0) == "jnz" ) {
			instructions.emplace_back(OpCode::JNZ, Argument(words.at(1)), Argument(words.at(2)));
		}
		else if ( words.at(0) == "out" ) {
			instructions.emplace_back(OpCode::OUT, Argument(words.at(1)));
		}
		else {
			cerr << "unknown opcode..." << '\n';
		}
	}

	return instructions;
}

#define JIT

string
execute(memory_type& memory, vector<Instruction> instructions)
{
	string result;

	for ( size_t ip = 0; ip < instructions.size(); ) {
		const auto& instruction = instructions.at(ip);

		// cout << "#" << ip << " - " << memory['a'] << ", " << memory['b'] << ", " << memory['c'] << ", " << memory['d'] << ": " << (int) instruction.opCode << '\n';

		switch ( instruction.opCode ) {
			using enum OpCode;
		case CPY:
			memory[instruction.y->reg()] = instruction.x->eval(memory);
			++ip;
			break;
		case INC:
			memory[instruction.x->reg()]++;
			++ip;
			break;
		case DEC:
			memory[instruction.x->reg()]--;
			++ip;
			break;
		case JNZ:
			if ( instruction.x->eval(memory) != 0 ) {
				ip += static_cast<size_t>(instruction.y->eval(memory));
			}
			else {
				++ip;
			}
			break;
		case OUT:
			result += to_string(instruction.x->eval(memory));
			if ( result.size() >= 20 ) {
				ip = instructions.size();
			}
			else {
				++ip;
			}
		}
	}

	return result;
}

void
part1(const vector<Instruction>& instructions)
{
	for ( int seed = 0;; ++seed ) {
		map<char, int> memory;

		memory['a'] = seed;
		memory['b'] = 0;
		memory['c'] = 0;
		memory['d'] = 0;

		auto result = execute(memory, instructions);

		if ( result == "01010101010101010101" ) {
			cout << "Part1: " << seed << '\n';
			return;
		}
	}
}

} // namespace

int
main()
{
	const auto instructions = read_file("data/day25.txt");
	try {
		part1(instructions);
	}
	catch ( exception& e ) {
		cerr << e.what() << '\n';
	}
}