From 56e890cec0a28c0a485212ccebfaf774235a79a2 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Wed, 3 Jan 2024 23:35:54 +0100 Subject: prepare for more puzzles ... :) --- src/day08.cpp | 110 ---------------------------------------------------------- 1 file changed, 110 deletions(-) delete mode 100644 src/day08.cpp (limited to 'src/day08.cpp') diff --git a/src/day08.cpp b/src/day08.cpp deleted file mode 100644 index 3e1b6c9..0000000 --- a/src/day08.cpp +++ /dev/null @@ -1,110 +0,0 @@ -#include -#include -#include -#include -#include -#include -using namespace std; - -struct Generator { - explicit Generator(string_view init) - : values_(init) - , iter_(values_.begin()) - - { - } - - size_t next() - { - size_t gen = (*iter_ == 'L') ? 0 : 1; - - ++iter_; - if ( iter_ == values_.end() ) { - iter_ = values_.begin(); - } - - return gen; - } - - string values_; - string::iterator iter_; -}; - -void -part1() -{ - fstream input{ "data/day08.txt" }; - - string line; - getline(input, line); - - Generator generator{ line }; - - map> puzzle; - - const regex pattern{ R"((.*) = \((.*), (.*)\))" }; - while ( getline(input, line) ) { - if ( line.empty() ) { - continue; - } - - smatch sub_match; - if ( regex_search(line, sub_match, pattern) ) { - puzzle[sub_match[1]] = { sub_match[2], sub_match[3] }; - } - } - - auto counter = 0; - for ( string str{ "AAA" }; str != "ZZZ"; str = puzzle[str][generator.next()] ) { - ++counter; - } - cout << counter << endl; -} - -void -part2() -{ - fstream input{ "data/day08.txt" }; - - string firstline; - getline(input, firstline); - - map> puzzle; - - const regex pattern{ R"((.*) = \((.*), (.*)\))" }; - for ( string line; getline(input, line); ) { - if ( line.empty() ) { - continue; - } - - smatch sub_match; - if ( regex_search(line, sub_match, pattern) ) { - puzzle[sub_match[1]] = { sub_match[2], sub_match[3] }; - } - } - - auto value = 1L; - for ( const auto& entry: puzzle ) { - if ( entry.first[2] != 'A' ) { - continue; - } - - Generator generator{ firstline }; - - auto counter = 0L; - - for ( string str{ entry.first }; str[2] != 'Z'; str = puzzle[str][generator.next()] ) { - ++counter; - } - - value = lcm(value, counter); - } - cout << value << endl; -} - -int -main() -{ - part1(); - part2(); -} -- cgit v1.3