-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday6a.cpp
More file actions
166 lines (157 loc) · 2.94 KB
/
Copy pathday6a.cpp
File metadata and controls
166 lines (157 loc) · 2.94 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
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
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <cassert>
#include <algorithm>
#include <array>
namespace day6a
{
int main()
{
std::string input0;
//std::ifstream ifile("day6_sample.txt");
std::ifstream ifile("day6.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);
int h = values.size();
int w = values[0].size();
for (auto& row : values)
{
assert(row.size() == w);
}
for (int x = 0; x < w; x++)
{
char c = values[0][x];
if (c == ' ')
{
bool is_column = true;
for (int y = 1; y < h; y++)
{
char c2 = values[y][x];
if (c2 != ' ')
{
is_column = false;
break;
}
}
if (is_column)
{
for (int y = 0; y < h; y++)
{
values[y][x] = '|';
}
}
}
}
auto str_to_nums = [](std::string s)->std::vector<uint64_t> {
std::vector<uint64_t> result;
std::istringstream ss{ s };
while (true)
{
uint64_t v1;
char c1;
if (!ss.eof())
{
ss >> v1;
result.push_back(v1);
}
if (!ss.eof())
{
ss >> c1;
}
else
{
break;
}
}
return result;
};
auto str_to_oprs = [](std::string s)->std::vector<char> {
std::vector<char> result;
std::istringstream ss{ s };
while (true)
{
char v1;
char c1;
if (!ss.eof())
{
ss >> v1;
result.push_back(v1);
}
if (!ss.eof())
{
ss >> c1;
}
else
{
break;
}
}
return result;
};
constexpr int nums_size = 4; // dummy, dont forget to update this value when switching from sample to actual input
std::array<std::vector<uint64_t>, nums_size> nums;
for (int i = 0; i < nums_size; i++)
{
nums[i] = str_to_nums(values[i]);
}
std::vector<char> oprs = str_to_oprs(values.back());
uint64_t grand_total = 0;
for (int i = 0; i < oprs.size(); i++)
{
uint64_t rollover_temp = grand_total;
char op = oprs[i];
uint64_t num_temp = 0;
switch (op)
{
case '+':
num_temp = 0;
for (int v = 0; v < nums_size; v++)
{
num_temp += nums[v][i];
}
grand_total += num_temp;
break;
case '*':
num_temp = 1;
for (int v = 0; v < nums_size; v++)
{
num_temp *= nums[v][i];
}
grand_total += num_temp;
break;
}
assert(grand_total >= rollover_temp);
}
__debugbreak();
return 0;
}
}