Skip to content

Commit da90242

Browse files
committed
Day 13
1 parent 79edf5f commit da90242

File tree

2 files changed

+201
-0
lines changed

2 files changed

+201
-0
lines changed

AoC13.cpp

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
#include "stdafx.h"
2+
#include "Lib.h"
3+
#include <iostream>
4+
using std::cout;
5+
using std::cerr;
6+
#include <map>
7+
using std::map;
8+
9+
struct line{
10+
void move()
11+
{
12+
if (direction == 0)
13+
{
14+
if (position == severity - 1)
15+
{
16+
direction = 1;
17+
position--;
18+
}
19+
else
20+
{
21+
position++;
22+
}
23+
}
24+
else
25+
{
26+
if (position == 0)
27+
{
28+
direction = 0;
29+
position++;
30+
}
31+
else
32+
{
33+
position--;
34+
}
35+
}
36+
}
37+
38+
void move(int toMove)
39+
{
40+
int period = (severity - 1) * 2;
41+
toMove %= period;
42+
43+
for (int i = 0; i != toMove; i++)
44+
{
45+
move();
46+
}
47+
}
48+
49+
void reset()
50+
{
51+
direction = 0;
52+
position = 0;
53+
}
54+
55+
int number = 0;
56+
int severity = 0;
57+
58+
int position = 0;
59+
int direction = 0;
60+
};
61+
62+
line parseLine(const string &in)
63+
{
64+
line lb;
65+
vector<string> splitInput = split(in);
66+
67+
lb.number = stoi(trimTrailingColons(splitInput[0]));
68+
lb.severity = stoi(splitInput[1]);
69+
return lb;
70+
}
71+
72+
vector<line> parseLines(const vector<string> &input)
73+
{
74+
vector<line> op;
75+
for (auto l : input)
76+
{
77+
op.push_back(parseLine(l));
78+
}
79+
return op;
80+
}
81+
82+
void progressScanners(vector<line> &a)
83+
{
84+
for (auto &l : a)
85+
{
86+
l.move();
87+
}
88+
}
89+
90+
int isCaught(vector<line> &lines, map<int, line*> &lm, int greatestNumber, bool &caught)
91+
{
92+
int caughtSeverity = 0;
93+
for (int i = 0; i != greatestNumber + 1; i++)
94+
{
95+
96+
if (lm.count(i) != 0)
97+
{
98+
if (lm[i]->position == 0)
99+
{
100+
caughtSeverity += (lm[i]->severity * i);
101+
caught = true;
102+
}
103+
}
104+
105+
progressScanners(lines);
106+
107+
}
108+
return caughtSeverity;
109+
}
110+
111+
int main(int argc, char* argv[])
112+
{
113+
try
114+
{
115+
vector<string> input = readFileAsLines("in_13.txt");
116+
117+
vector<line> parsedInput = parseLines(input);
118+
119+
int greatestNumber = 0;
120+
for (auto &l : parsedInput)
121+
{
122+
if (l.number > greatestNumber)
123+
{
124+
greatestNumber = l.number;
125+
}
126+
}
127+
128+
map<int, line*> lm;
129+
for (auto &l : parsedInput)
130+
{
131+
lm[l.number] = &l;
132+
}
133+
134+
int caughtSeverity = 0;
135+
bool cc = false;
136+
caughtSeverity = isCaught(parsedInput, lm, greatestNumber, cc);
137+
cout << "Part 1 severity " << caughtSeverity << "\n";
138+
139+
int j = 0;
140+
int delay = 0;
141+
while (true)
142+
{
143+
for (auto &l : parsedInput)
144+
{
145+
l.reset();
146+
}
147+
148+
149+
for (auto &l : parsedInput)
150+
{
151+
l.move(delay);
152+
}
153+
154+
155+
156+
//for (int i = 0;i!=delay ; i++)
157+
//{
158+
// progressScanners(parsedInput);
159+
//}
160+
161+
bool caught = false;
162+
isCaught(parsedInput, lm, greatestNumber, caught);
163+
if (!caught)
164+
{
165+
cout << "Not caught at " << delay << " = " << caught <<"\n";
166+
break;
167+
}
168+
else
169+
{
170+
delay += 1;
171+
//cout << "Caught at " << delay << " = " << caught << "\n";
172+
}
173+
174+
}
175+
176+
}
177+
catch (FileReadException &e)
178+
{
179+
}
180+
return 0;
181+
}
182+

Lib.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,23 @@ string trimTrailingCommas(const string &a)
198198
return a.substr(0, a.length() - toSkip);
199199
}
200200

201+
string trimTrailingColons(const string &a)
202+
{
203+
int toSkip = 0;
204+
205+
for (int i = a.length() - 1; i != -1; i--)
206+
{
207+
if (a[i] == ':')
208+
{
209+
toSkip++;
210+
}
211+
else
212+
{
213+
break;
214+
}
215+
}
216+
217+
return a.substr(0, a.length() - toSkip);
218+
}
219+
201220
#endif //LIB_H_

0 commit comments

Comments
 (0)