Skip to content

Commit 4b1292b

Browse files
committed
Added day8. Added some parsing routines to lib.
1 parent 8292401 commit 4b1292b

File tree

2 files changed

+364
-0
lines changed

2 files changed

+364
-0
lines changed

AoC8.cpp

Lines changed: 322 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,322 @@
1+
// AoC8.cpp : Defines the entry point for the console application.
2+
//
3+
4+
#include "stdafx.h"
5+
#include "Lib.h"
6+
7+
#include <iostream>
8+
using std::cout;
9+
#include <algorithm>
10+
#include <map>
11+
using std::map;
12+
13+
enum class op{Inc, Dec};
14+
15+
enum class conditions{
16+
Less, LessOrEq, Eq, NotEq, Greater, GreaterOrEq
17+
};
18+
19+
class condition{
20+
21+
public:
22+
string regName;
23+
int value = 0;
24+
conditions a;
25+
};
26+
27+
class instruction{
28+
public:
29+
op oper;
30+
string regName;
31+
int value = 0;
32+
condition cond;
33+
};
34+
35+
condition parseCondition(const string &a, unsigned int offset)
36+
{
37+
condition nc;
38+
unsigned int i = offset;
39+
if (matchString(string("if"), a, i))
40+
{
41+
i += 2;
42+
}
43+
44+
i += consumeWhiteSpace(a, i);
45+
46+
string buf;
47+
while (a[i] >= 'a' && a[i] <= 'z')
48+
{
49+
buf.append(1, a[i]);
50+
i++;
51+
}
52+
nc.regName = buf;
53+
54+
i += consumeWhiteSpace(a, i);
55+
56+
string less("<");
57+
string lessOrEq("<=");
58+
string eq("==");
59+
string notEq("!=");
60+
string greater(">");
61+
string greaterEqual(">=");
62+
if (matchString(lessOrEq, a, i)){
63+
nc.a = conditions::LessOrEq;
64+
i += lessOrEq.length();
65+
}
66+
else if (matchString(less, a, i)){
67+
nc.a = conditions::Less;
68+
i += less.length();
69+
}
70+
else if (matchString(eq, a, i)){
71+
nc.a = conditions::Eq;
72+
i += eq.length();
73+
}
74+
else if (matchString(notEq, a, i)){
75+
nc.a = conditions::NotEq;
76+
i += notEq.length();
77+
}
78+
else if (matchString(greaterEqual, a, i)){
79+
nc.a = conditions::GreaterOrEq;
80+
i += greaterEqual.length();
81+
}
82+
else if (matchString(greater, a, i)){
83+
nc.a = conditions::Greater;
84+
i += greater.length();
85+
}
86+
87+
88+
i += consumeWhiteSpace(a, i);
89+
90+
unsigned int bytesConsumed = 0;
91+
nc.value += decodeInt(a, i, &bytesConsumed);
92+
93+
return nc;
94+
}
95+
96+
instruction parseLine(string &a)
97+
{
98+
instruction ni;
99+
string buf;
100+
101+
unsigned int i = 0;
102+
while (a[i] >= 'a' && a[i] <= 'z')
103+
{
104+
buf.append(1, a[i]);
105+
i++;
106+
}
107+
ni.regName = buf;
108+
buf = string();
109+
110+
i += consumeWhiteSpace(a, i);
111+
112+
string inc("inc");
113+
string dec("dec");
114+
115+
if (matchString(inc, a, i)){
116+
ni.oper = op::Inc;
117+
i += 3;
118+
}
119+
else if (matchString(dec, a, i))
120+
{
121+
ni.oper = op::Dec;
122+
i += 3;
123+
}
124+
125+
i += consumeWhiteSpace(a, i);
126+
127+
unsigned int bytesConsumed = 0;
128+
ni.value += decodeInt(a, i, &bytesConsumed);
129+
i += bytesConsumed;
130+
131+
i += consumeWhiteSpace(a, i);
132+
133+
ni.cond = parseCondition(a, i);
134+
135+
return ni;
136+
}
137+
138+
vector<string> getRegisters(vector<instruction> &insns)
139+
{
140+
vector<string> regs;
141+
for (unsigned int i = 0 ; i!=insns.size() ; i++)
142+
{
143+
regs.push_back(insns[i].regName);
144+
regs.push_back(insns[i].cond.regName);
145+
}
146+
147+
std::sort(regs.begin(), regs.end());
148+
149+
vector<string> reg2;
150+
string prevReg;
151+
bool prFound = false;
152+
153+
for (unsigned int i = 0; i != regs.size(); i++)
154+
{
155+
if (!prFound)
156+
{
157+
prFound = true;
158+
prevReg = regs[i];
159+
}
160+
else
161+
{
162+
if (regs[i] != prevReg)
163+
{
164+
reg2.push_back(regs[i]);
165+
prevReg = regs[i];
166+
}
167+
}
168+
169+
}
170+
171+
return reg2;
172+
}
173+
174+
bool evalCondition(map<string, int> &regState, condition &cond)
175+
{
176+
if (cond.a == conditions::Less){
177+
if (regState[cond.regName] < cond.value)
178+
{
179+
return true;
180+
}
181+
else
182+
{
183+
return false;
184+
}
185+
186+
}
187+
else if (cond.a == conditions::LessOrEq){
188+
if (regState[cond.regName] <= cond.value)
189+
{
190+
return true;
191+
}
192+
else
193+
{
194+
return false;
195+
}
196+
}
197+
else if(cond.a == conditions::Eq){
198+
if (regState[cond.regName] == cond.value)
199+
{
200+
return true;
201+
}
202+
else
203+
{
204+
return false;
205+
}
206+
207+
}
208+
else if(cond.a == conditions::NotEq){
209+
if (regState[cond.regName] != cond.value)
210+
{
211+
return true;
212+
}
213+
else
214+
{
215+
return false;
216+
}
217+
218+
}
219+
else if (cond.a == conditions::Greater){
220+
if (regState[cond.regName] > cond.value)
221+
{
222+
return true;
223+
}
224+
else
225+
{
226+
return false;
227+
}
228+
229+
}
230+
else if (cond.a == conditions::GreaterOrEq){
231+
if (regState[cond.regName] >= cond.value)
232+
{
233+
return true;
234+
}
235+
else
236+
{
237+
return false;
238+
}
239+
240+
}
241+
242+
}
243+
244+
void doInstruction(map<string, int> &regValues, instruction &ins, int *highestVal)
245+
{
246+
if (evalCondition(regValues, ins.cond))
247+
{
248+
if (ins.oper == op::Dec)
249+
{
250+
int val = regValues[ins.regName];
251+
val -= ins.value;
252+
regValues[ins.regName] = val;
253+
254+
if (val > *highestVal)
255+
{
256+
*highestVal = val;
257+
}
258+
}
259+
else if (ins.oper == op::Inc)
260+
{
261+
int val = regValues[ins.regName];
262+
val += ins.value;
263+
regValues[ins.regName] = val;
264+
265+
if (val > *highestVal)
266+
{
267+
*highestVal = val;
268+
}
269+
}
270+
}
271+
}
272+
273+
int _tmain(int argc, _TCHAR* argv[])
274+
{
275+
try{
276+
vector<string> results = readFileAsLines("in_8.txt");
277+
278+
vector<instruction> insnList;
279+
for (int i = 0; i!=results.size() ; i++)
280+
{
281+
insnList.push_back(parseLine(results[i]));
282+
}
283+
284+
vector<string> registers;
285+
registers = getRegisters(insnList);
286+
287+
map<string, int> registerValues;
288+
for (int i = 0 ; i!=registers.size() ; i++)
289+
{
290+
registerValues[registers[i]] = 0;
291+
}
292+
293+
int highestVal = 0;
294+
for (unsigned int i = 0 ; i!= insnList.size() ; i++)
295+
{
296+
doInstruction(registerValues, insnList[i], &highestVal);
297+
}
298+
299+
int greatest = 0;
300+
bool greatestFound = false;
301+
for (auto rIter = registerValues.begin(); rIter!=registerValues.end() ; rIter++)
302+
{
303+
if (!greatestFound)
304+
{
305+
greatestFound = true;
306+
greatest = (*rIter).second;
307+
}
308+
else if (greatest < (*rIter).second)
309+
{
310+
greatest = (*rIter).second;
311+
}
312+
}
313+
314+
cout << "greatest " << greatest << "\n";
315+
cout << "highest val " << highestVal << "\n";
316+
}
317+
catch (FileReadException &e)
318+
{
319+
}
320+
return 0;
321+
}
322+

Lib.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,46 @@ vector<string> readFileAsLines(const char* fileName)
6969
return splitIntoLines(buf);
7070
}
7171

72+
unsigned int consumeWhiteSpace(const string &a, unsigned int offset)
73+
{
74+
unsigned int i = 0;
75+
while (offset + i != a.length())
76+
{
77+
char c = a[i + offset];
78+
if (c != ' ' && c != '\t')
79+
{
80+
return i;
81+
}
82+
i++;
83+
}
84+
return 0;
85+
}
86+
87+
bool matchString(const string &needle, const string &haystack, unsigned int haystackOffset)
88+
{
89+
unsigned int i = 0;
90+
for (; i!=needle.length() ; i++)
91+
{
92+
if (haystack[haystackOffset + i] != needle[i])
93+
{
94+
return false;
95+
}
96+
}
97+
return true;
98+
}
99+
100+
int decodeInt(const string &buf, unsigned int offset, unsigned int *consume)
101+
{
102+
unsigned int i = 0;
103+
int temp = 0;
104+
string obuf;
105+
while ((buf[offset + i] >= '0' && buf[offset + i] <= '9') || buf[offset+i] == '-')
106+
{
107+
obuf.append(1, buf[offset + i]);
108+
i++;
109+
}
110+
*consume = i;
111+
return stoi(obuf, 0, 0);
112+
}
113+
72114
#endif //LIB_H_

0 commit comments

Comments
 (0)