aboutsummaryrefslogtreecommitdiff
path: root/2015/src
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2024-11-14 16:49:45 +0100
committerThomas Schmucker <ts@its1.de>2024-11-14 16:49:45 +0100
commited65c5937950373f96026f476c03bf8984360900 (patch)
tree796329726b388f009cfce208b0d8f2e6639f5640 /2015/src
parent4b6c1b0e539d9fc37b646af5865b75aaa5b49bfc (diff)
downloadadvent-of-code-ed65c5937950373f96026f476c03bf8984360900.tar.gz
advent-of-code-ed65c5937950373f96026f476c03bf8984360900.tar.bz2
advent-of-code-ed65c5937950373f96026f476c03bf8984360900.zip
aoc 2015, day 8
Diffstat (limited to '2015/src')
-rw-r--r--2015/src/day08.cpp71
1 files changed, 71 insertions, 0 deletions
diff --git a/2015/src/day08.cpp b/2015/src/day08.cpp
new file mode 100644
index 0000000..9cd6e21
--- /dev/null
+++ b/2015/src/day08.cpp
@@ -0,0 +1,71 @@
1#include <fstream>
2#include <iostream>
3#include <string>
4#include <vector>
5
6using namespace std;
7
8auto
9read_file(string_view filename)
10{
11 fstream input{ filename };
12 vector<string> lines;
13
14 for ( string line; getline(input, line); ) {
15 lines.emplace_back(line);
16 }
17
18 return lines;
19}
20
21void
22part1(const vector<string>& lines)
23{
24 auto count = [](const string& line) -> size_t {
25 size_t length = 0;
26 auto iter = begin(line);
27
28 while ( iter != end(line) ) {
29 if ( *iter == '\\' ) {
30 ++iter;
31 if ( *iter == '\\' || *iter == '"' ) {
32 ++length;
33 ++iter;
34 }
35 else if ( *iter == 'x' ) {
36 ++length;
37 iter += 3;
38 }
39 }
40 else {
41 ++iter;
42 ++length;
43 }
44 }
45 return length - 2;
46 };
47
48 size_t sum = 0;
49 for ( const auto& line: lines ) {
50 sum += line.size() - count(line);
51 }
52 cout << sum << endl;
53}
54
55void
56part2(const vector<string>& lines)
57{
58 long sum = 0;
59 for ( const auto& line: lines ) {
60 sum += 2 + count_if(begin(line), end(line), [](auto chr) { return chr == '\\' || chr == '"'; });
61 }
62 cout << sum << endl;
63}
64
65int
66main()
67{
68 auto lines = read_file("data/day08.txt");
69 part1(lines);
70 part2(lines);
71}