blob: dd25a9dfa2287319100714195bc8bcaece16ae09 (
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
|
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
using namespace std;
vector<int>
read_file(string_view filename)
{
fstream input{ filename };
string line;
int fid = 0;
getline(input, line);
vector<int> data;
for ( size_t i = 0; i != line.size(); ++i ) {
vector<int> values(size_t(line.at(i) - '0'), (i % 2 == 0) ? fid++ : -1);
data.insert(data.end(), values.begin(), values.end());
}
return data;
}
void
part1(const vector<int>& data)
{
vector<int> result;
auto lhs = data.begin();
auto rhs = data.end();
while ( lhs != rhs ) {
if ( *lhs != -1 ) {
result.push_back(*lhs);
}
else {
--rhs;
while ( *rhs == -1 ) {
--rhs;
}
result.push_back(*rhs);
}
++lhs;
}
long sum = 0;
for ( size_t i = 0; i != result.size(); ++i ) {
sum += long(i) * result.at(i);
}
cout << sum << endl;
}
int
main()
{
auto data = read_file("data/day09.txt");
part1(data);
}
|