-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.cpp
65 lines (54 loc) · 1.56 KB
/
part1.cpp
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
#include "Day04.hpp"
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
struct Pair {
char ch;
int n;
};
static bool sortPair(Pair a, Pair b){
if( a.n == b.n ){
return a.ch < b.ch;
}
return a.n > b.n;
}
Result Day04::solve_p1(){
int result = 0;
for(unsigned ii = 0; ii < this->data.size(); ii++){
string content;
stringstream line(this->data[ii]);
line >> content;
Pair letters[26] = {0, 0};
for( unsigned jj = 0; jj < 26; jj++){
letters[jj].ch = 'a' + jj;
}
int id = 0;
string checksum = "";
for(unsigned jj = 0; jj < content.length(); jj++){
char letter = content[jj];
if( letter == '-' ){
continue;
} else if ( letter >= 'a' && letter <= 'z' ){
letters[ letter - 'a' ].n++;
} else if ( letter >= '0' && letter <= '9' && id == 0 ){
stringstream numberString(content.substr(jj));
numberString >> id;
} else if ( letter == '[') {
checksum = content.substr(jj+1);
checksum.pop_back();
break;
}
}
sort(letters, letters + 26, sortPair);
string check = "";
for( unsigned jj = 0; jj < 5; jj++){
check += letters[jj].ch;
}
cout << "check: " << check << " checksum: " << checksum << endl;
if( checksum == check ){
result += id;
}
}
return {true, to_string(result)};
}