-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay2part1.cpp
More file actions
132 lines (103 loc) · 2.55 KB
/
Copy pathDay2part1.cpp
File metadata and controls
132 lines (103 loc) · 2.55 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
#include <iostream>
#include <fstream>
#include <vector>
#include<string>
#include<iomanip>
#include<sstream>//for converting string digits to int
using namespace std;
int Separatewords(string str)//function for finding whether password is valid or not
{
vector<string>v;//this vector is to store words in a give line
int j=0;
string word = "";
int l=str.length();
for (int i=0;i<l;i++)//Goes through all charecters and separates word
{
string x;
x=str[i];
if ((x == " "||x=="-")&&i != l-1)
{
if(word=="")
{
continue;
}
else{
v.push_back(word);
}
word = "";
}
else if(x==":")
{
if (word == "")
{
continue;
}
else
{
v.push_back(word);
}
word="";
}
else if(i==l-1)
{
word=word+x;//making a
if (word == "")
{
continue;
}
else
{
v.push_back(word);
}
}
else
{
word = word + x;
}
}
int count=0;//for finding frequecy of required charecter
for(int y=0;y<v[3].length();y++)
{
char s=v[2][0];
if(v[3][y]==s)
{
count++;
}
}
stringstream sss(v[0]);//for converting '15' to 15 (example)
int x=0;
sss>>x;
stringstream ss(v[1]);
int z=0;
ss>>z;
if(z>=count&&x<=count )//checking wheather count is in range of given numbers
{
return 1;//this returned value stored in a vector v1 in main function
}
else
{
return 0;
}
}
int main()
{
string p;//to store a line from a given data
vector<int>v1;
ifstream indata;//for reading data from file
indata.open("Day2 data");
if(indata.fail())
{
cout<<"something went wrong"<<endl;//for any kind of error messages
}
while(indata)
{
getline(indata,p);//taking a line at a time and checking its validity
int x=Separatewords(p);//returns either 0 or 1 based on its validity
if(x==1)
{
v1.push_back(1);//main reason to add 1 to a vector is to easily count no of valid passwords
}
}
cout<<v1.size()<<endl;
return 0;
}