-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
68 lines (62 loc) · 1.73 KB
/
main.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
/*
Dans l'input, chaque ligne représente une paire de personnes (chaque personne est séparée par une virgule).
Chaque personne de la paire est attribuée au nettoyage d'un intervalle de section.
Dans la partie 1, on compte pour chaque paire si l'intervalle d'une personne est totalement inclus dans l'intervalle de son coéquipier.
Dans la partie 2, on compte pour chaque paire si les intervalles se chevauchent.
*/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
vector<string> split(string s, char delim)
{
vector<string> result;
string element;
istringstream tokenStream(s);
while (getline(tokenStream, element, delim))
{
result.push_back(element);
}
return result;
}
int main()
{
string filename = "input.txt";
ifstream file(filename);
if (!file.is_open())
{
cerr << "Erreur : impossible d'ouvrir le fichier " << filename;
return 1;
}
string line;
int countPart1 = 0;
int countPart2 = 0;
while (getline(file, line))
{
vector<string> pairs = split(line, ',');
vector<string> range1 = split(pairs[0], '-');
vector<string> range2 = split(pairs[1], '-');
int intRange1[2] = {stoi(range1[0]), stoi(range1[1])};
int intRange2[2] = {stoi(range2[0]), stoi(range2[1])};
// Part 1
if (intRange1[0] <= intRange2[0] && intRange1[1] >= intRange2[1])
{
countPart1++;
}
else if (intRange2[0] <= intRange1[0] && intRange2[1] >= intRange1[1])
{
countPart1++;
}
// Part 2;
if (!((intRange1[1] < intRange2[0]) || (intRange2[1] < intRange1[0])))
{
countPart2++;
}
}
file.close();
cout << "Part1: " << countPart1 << '\n';
cout << "Part2: " << countPart2;
return 0;
}