From 72253f5efb10c7892fdb09d52343764124cd717c Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Wed, 13 Dec 2023 19:15:25 +0100 Subject: Lösung für Tag 13 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/day13.cpp | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 src/day13.cpp (limited to 'src/day13.cpp') 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 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +string +read_file(string_view filename) +{ + fstream input{ filename }; + return { istreambuf_iterator{ input }, istreambuf_iterator{} }; +} + +vector +split(string_view line, string_view delimiter) +{ + size_t pos_start = 0; + size_t pos_end = 0; + + vector res; + + while ( (pos_end = line.find(delimiter, pos_start)) != std::string::npos ) { + auto token = line.substr(pos_start, pos_end - pos_start); + pos_start = pos_end + delimiter.length(); + + res.emplace_back(token); + } + + res.emplace_back(line.substr(pos_start)); + return res; +} + +vector +transpose(const vector& input) +{ + vector result(input[0].size()); + + for ( const auto& i: input ) { + for ( size_t j = 0; j < i.size(); j++ ) { + result[j] += i[j]; + } + } + return result; +} + +long +find_mirror(const vector& input) +{ + for ( size_t idx = 1; idx < input.size(); ++idx ) { + bool equal = true; + for ( size_t cnt = 0; cnt != min(idx, input.size() - idx); ++cnt ) { + auto cmp_result = (input.at(idx + cnt) == input.at(idx - 1 - cnt)); + if ( !cmp_result ) { + equal = false; + break; + } + } + if ( equal ) { + return long(idx); + } + } + return 0; +} + +void +part1() +{ + auto contents = read_file("data/day13.txt"); + auto parts = split(contents, "\n\n"); + + long sum = 0; + for ( const auto& part: parts ) { + auto lines = split(part, "\n"); + + sum += find_mirror(transpose(lines)); + sum += find_mirror(lines) * 100; + } + cout << sum << endl; +} + +long +count_differences(string_view a, string_view b) // NOLINT +{ + long errs = 0; + for (size_t idx = 0; idx != a.size(); ++idx) { + if (a[idx] != b[idx]) { + ++errs; + } + } + return errs; +} + +long +find_mirror2(const vector& input) +{ + for ( size_t idx = 1; idx < input.size(); ++idx ) { + long errs = 0; + for ( size_t cnt = 0; cnt != min(idx, input.size() - idx); ++cnt ) { + errs += count_differences(input.at(idx + cnt), input.at(idx - 1 - cnt)); + } + if ( errs == 1 ) { + return long(idx); + } + } + return 0; +} + +void +part2() +{ + auto contents = read_file("data/day13.txt"); + auto parts = split(contents, "\n\n"); + + long sum = 0; + for ( const auto& part: parts ) { + auto lines = split(part, "\n"); + + sum += find_mirror2(transpose(lines)); + sum += find_mirror2(lines) * 100; + } + cout << sum << endl; +} + +int +main() +{ + // part1(); + part2(); +} -- cgit v1.3