-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalibrationChallenge.cpp
138 lines (113 loc) · 3.09 KB
/
CalibrationChallenge.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
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
// CalibrationChallenge.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <fstream>
#include "String"
#include <cctype>
#include "vector"
using namespace std;
static string num_String[10] = { "zero","one","two","three","four","five","six","seven","eight","nine" };
int lineCallibration(string& line)
{
string calebration;
int numberOfDigits = 0;
string AssumedlasDigit;
bool bFoundFirst = false;
string DigitString;
auto GetNumberCharacter = [&](string& NumberString)
{ string _calebration;
int _itr = 0;
for (string& str : num_String)
{
size_t position = line.find(str);
if (position != string::npos)
{
_calebration += ('0' + _itr);
}
_itr++;
}
return _calebration;
};
auto IsValidNumberString = [&](string& NumberString)
{
for (string& str : num_String)
{
size_t position = line.find(str);
if (position != string::npos)
{
return true;
}
return false;
}
};
int itr = 0;
for (char& c : line)
{
if (isdigit(c))
{
DigitString.erase(DigitString.length());
if (!bFoundFirst)
{
calebration += c;
numberOfDigits++;
bFoundFirst = true;
}
else
{
AssumedlasDigit = c;
numberOfDigits = 2;
}
}
else
{
DigitString += c;
if (IsValidNumberString(DigitString))
{
if (!bFoundFirst)
{
bFoundFirst = true;
calebration += GetNumberCharacter(DigitString);
numberOfDigits++;
DigitString.erase(DigitString.length());
}
else
{
AssumedlasDigit = GetNumberCharacter(DigitString);
numberOfDigits = 2;
}
}
}
itr++;
}
calebration += AssumedlasDigit;
if (!calebration.empty())
{
if (numberOfDigits == 1)
{
calebration += calebration;
}
return stoi(calebration);
}
else
return -1;
}
int main()
{
string filename = "C:\\Users\\Acer\\Documents\\Inputs.txt";
vector<string> lines;
int sum = 0;
ifstream file(filename);
if (file.is_open()) {
string line;
while (getline(file, line))
{
int lineCaleb = lineCallibration(line);
sum += lineCaleb;
cout << "found line : [ " << line << " ] with callibration of : " << lineCaleb << endl;
lines.push_back(line);
}
file.close();
}
cout << " sum of all calibrations : " << sum << endl;
return 0;
}