From 756f22d58bb198b8f34589c112e1003614ccdcd6 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sun, 2 Nov 2025 21:41:58 +0100 Subject: aoc 2017, days 1-20 --- 2017/src/day09.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 2017/src/day09.cpp (limited to '2017/src/day09.cpp') diff --git a/2017/src/day09.cpp b/2017/src/day09.cpp new file mode 100644 index 0000000..60b0d3b --- /dev/null +++ b/2017/src/day09.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include + +using namespace std; + +namespace { + +string +remove_garbage(string_view line, int& removed) +{ + string result; + + removed = 0; + for ( size_t pos = 0; pos < line.size(); ) { + if ( line.at(pos) == '<' ) { + ++pos; + while ( pos < line.size() && line.at(pos) != '>' ) { + if ( line.at(pos) == '!' ) { + pos += 2; + } + else { + ++pos; + ++removed; + } + } + ++pos; + } + else { + result += line.at(pos); + ++pos; + } + } + + return result; +} + +string +read_file(const filesystem::path& filename) +{ + ifstream file{ filename }; + return { istreambuf_iterator{ file }, {} }; +} + +void +solve(string_view data) +{ + int score = 0; + int depth = 0; + int removed = 0; + + for ( const auto chr: remove_garbage(data, removed) ) { + if ( chr == '{' ) { + ++depth; + } + else if ( chr == '}' ) { + score += depth; + --depth; + } + } + + cout << "Part1: " << score << '\n'; + cout << "Part2: " << removed << '\n'; +} + +} // namespace + +int +main() +{ + auto data = read_file("data/day09.txt"); + solve(data); +} -- cgit v1.3