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/day19.cpp | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 2017/src/day19.cpp (limited to '2017/src/day19.cpp') diff --git a/2017/src/day19.cpp b/2017/src/day19.cpp new file mode 100644 index 0000000..470bb4f --- /dev/null +++ b/2017/src/day19.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +namespace { + +using dir_type = tuple; +using pos_type = tuple; +using puzzle_type = map; + +puzzle_type +read_file(const filesystem::path& filename) +{ + ifstream file{ filename }; + puzzle_type data; + + size_t row = 0; + for ( string line; getline(file, line); ) { + for ( size_t col = 0; col != line.size(); ++col ) { + if ( line.at(col) != ' ' ) { + data.emplace(pos_type{ col, row }, line.at(col)); + } + } + ++row; + } + + return data; +} + +pos_type +find_start(const puzzle_type& puzzle) +{ + for ( size_t col = 0;; ++col ) { + const pos_type pos{ col, 0 }; + if ( puzzle.contains(pos) && puzzle.at(pos) == '|' ) { + return pos; + } + } +} + +void +solve(const puzzle_type& puzzle) +{ + string result; + size_t steps = 0; + + pos_type pos = find_start(puzzle); + dir_type dir{ 0, 1 }; + + while ( puzzle.contains(pos) ) { + auto chr = puzzle.at(pos); + + if ( chr >= 'A' && chr <= 'Z' ) { + result += chr; + } + else if ( chr == '+' ) { + auto [dx, dy] = dir; + auto left = dir_type{ -dy, dx }; + auto right = dir_type{ dy, -dx }; + + pos_type next_left = { get<0>(pos) + get<0>(left), get<1>(pos) + get<1>(left) }; + pos_type next_right = { get<0>(pos) + get<0>(right), get<1>(pos) + get<1>(right) }; + + if ( puzzle.contains(next_left) ) { + dir = left; + } + else if ( puzzle.contains(next_right) ) { + dir = right; + } + else { + cerr << "Error!\n"; + return; + } + } + + get<0>(pos) += get<0>(dir); + get<1>(pos) += get<1>(dir); + ++steps; + } + + cout << "Part1: " << result << '\n'; + cout << "Part2: " << steps << '\n'; +} + +} // namespace + +int +main() +{ + auto puzzle = read_file("data/day19.txt"); + solve(puzzle); +} -- cgit v1.3