aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/day13.cpp132
1 files changed, 132 insertions, 0 deletions
diff --git a/src/day13.cpp b/src/day13.cpp
new file mode 100644
index 0000000..43fd39e
--- /dev/null
+++ b/src/day13.cpp
@@ -0,0 +1,132 @@
1#include <algorithm>
2#include <fstream>
3#include <iostream>
4#include <iterator>
5#include <sstream>
6#include <string>
7#include <vector>
8using namespace std;
9
10string
11read_file(string_view filename)
12{
13 fstream input{ filename };
14 return { istreambuf_iterator<char>{ input }, istreambuf_iterator<char>{} };
15}
16
17vector<string>
18split(string_view line, string_view delimiter)
19{
20 size_t pos_start = 0;
21 size_t pos_end = 0;
22
23 vector<string> res;
24
25 while ( (pos_end = line.find(delimiter, pos_start)) != std::string::npos ) {
26 auto token = line.substr(pos_start, pos_end - pos_start);
27 pos_start = pos_end + delimiter.length();
28
29 res.emplace_back(token);
30 }
31
32 res.emplace_back(line.substr(pos_start));
33 return res;
34}
35
36vector<string>
37transpose(const vector<string>& input)
38{
39 vector<string> result(input[0].size());
40
41 for ( const auto& i: input ) {
42 for ( size_t j = 0; j < i.size(); j++ ) {
43 result[j] += i[j];
44 }
45 }
46 return result;
47}
48
49long
50find_mirror(const vector<string>& input)
51{
52 for ( size_t idx = 1; idx < input.size(); ++idx ) {
53 bool equal = true;
54 for ( size_t cnt = 0; cnt != min(idx, input.size() - idx); ++cnt ) {
55 auto cmp_result = (input.at(idx + cnt) == input.at(idx - 1 - cnt));
56 if ( !cmp_result ) {
57 equal = false;
58 break;
59 }
60 }
61 if ( equal ) {
62 return long(idx);
63 }
64 }
65 return 0;
66}
67
68void
69part1()
70{
71 auto contents = read_file("data/day13.txt");
72 auto parts = split(contents, "\n\n");
73
74 long sum = 0;
75 for ( const auto& part: parts ) {
76 auto lines = split(part, "\n");
77
78 sum += find_mirror(transpose(lines));
79 sum += find_mirror(lines) * 100;
80 }
81 cout << sum << endl;
82}
83
84long
85count_differences(string_view a, string_view b) // NOLINT
86{
87 long errs = 0;
88 for (size_t idx = 0; idx != a.size(); ++idx) {
89 if (a[idx] != b[idx]) {
90 ++errs;
91 }
92 }
93 return errs;
94}
95
96long
97find_mirror2(const vector<string>& input)
98{
99 for ( size_t idx = 1; idx < input.size(); ++idx ) {
100 long errs = 0;
101 for ( size_t cnt = 0; cnt != min(idx, input.size() - idx); ++cnt ) {
102 errs += count_differences(input.at(idx + cnt), input.at(idx - 1 - cnt));
103 }
104 if ( errs == 1 ) {
105 return long(idx);
106 }
107 }
108 return 0;
109}
110
111void
112part2()
113{
114 auto contents = read_file("data/day13.txt");
115 auto parts = split(contents, "\n\n");
116
117 long sum = 0;
118 for ( const auto& part: parts ) {
119 auto lines = split(part, "\n");
120
121 sum += find_mirror2(transpose(lines));
122 sum += find_mirror2(lines) * 100;
123 }
124 cout << sum << endl;
125}
126
127int
128main()
129{
130 // part1();
131 part2();
132}