-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday3a.cpp
More file actions
84 lines (78 loc) · 1.68 KB
/
Copy pathday3a.cpp
File metadata and controls
84 lines (78 loc) · 1.68 KB
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
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <cassert>
namespace day3a
{
int main()
{
std::string input0;
//std::ifstream ifile("day3_sample.txt");
std::ifstream ifile("day3.txt");
if (ifile.is_open())
{
while (!ifile.eof())
{
std::string line;
std::getline(ifile, line);
input0 += line + '\n';
}
assert(input0.back() == '\n');
input0.pop_back(); // no trailing newline character please
//__debugbreak();
}
else
{
assert(0);
}
std::vector<std::string> values;
std::string temp;
for (char c : input0)
{
if (c == '\n')
{
values.push_back(temp);
temp = "";
continue;
}
temp += c;
}
values.push_back(temp);
//__debugbreak();
std::vector<int> joltages;
int max_joltage = 0;
for (auto& bank : values)
{
int max0 = -1;
int max0_pos;
for (int ix = bank.size() - 2; ix >= 0; ix--)
{
char c = bank[ix] - '0';
if (max0 <= c)
{
max0 = c;
max0_pos = ix;
}
// from right to left find the maximum value
// then from right to left, but stopping before the maximum value find the next largest value
// the first digit can't be the last digit in the row because you need 2 digits
// and you want to replace the max if max0_pos is larger as well so you have more chances for the second digit
}
int max1 = -1;
for (int ix = bank.size() - 1; ix > max0_pos; ix--)
{
char c = bank[ix] - '0';
if (max1 < c)
{
max1 = c;
}
}
int v = max0 * 10 + max1;
max_joltage += v;
}
// 17250 wrong
__debugbreak();
return 0;
}
}