-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
99 lines (89 loc) · 1.76 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
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
/************************************************************************/
/* Advent of Code:
/* Day 8: Matchsticks
/************************************************************************/
#include <iostream>
#include <fstream>
#include <string>
#include <chrono>
int countChars(std::string line)
{
int count = 0;
for (std::string::iterator it = line.begin(); it != line.end(); it++)
{
switch (*it)
{
case '"':
break;
case '\\':
{
if (++it != line.end())
{
if (*it == 'x')
it += 2;
count++;
}
break;
}
default:
count++;
break;
}
}
return count;
}
std::string escapeLine(std::string line)
{
std::string res = "\"";
for (auto x : line)
{
switch (x)
{
case '"':
res += "\\\"";
break;
case '\\':
res += "\\\\";
break;
default:
res += x;
break;
}
}
return res + '"';
}
size_t partOne()
{
std::ifstream infile("data_d08.txt");
std::string input;
size_t codeLen = 0;
size_t memLen = 0;
while (std::getline(infile, input))
{
codeLen += input.length();
memLen += countChars(input);
}
return codeLen - memLen;
}
size_t partTwo()
{
std::ifstream infile("data_d08.txt");
std::string input;
size_t codeLen = 0;
size_t memLen = 0;
while (std::getline(infile, input))
{
std::string newLine = escapeLine(input);
codeLen += newLine.length();
memLen += countChars(newLine);
}
return codeLen - memLen;
}
int main()
{
std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
std::cout << partOne() << std::endl;
std::cout << partTwo() << std::endl;
std::chrono::duration<double> time_span = std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::high_resolution_clock::now() - t1);
std::cout << "Time: " << time_span.count() << "s.";
}