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/day15.cpp | 102 ---------------------------------------------------------- 1 file changed, 102 deletions(-) delete mode 100644 src/day15.cpp (limited to 'src/day15.cpp') diff --git a/src/day15.cpp b/src/day15.cpp deleted file mode 100644 index 6fcbfc2..0000000 --- a/src/day15.cpp +++ /dev/null @@ -1,102 +0,0 @@ -#include -#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{} }; -} - -void -rtrim(string& str) -{ - str.erase(find_if(str.rbegin(), str.rend(), [](auto chr) { return !isspace(chr); }).base(), str.end()); -} - -vector -split(const string& line, char sep) -{ - vector parts{}; - stringstream input{ line }; - - for ( string part; getline(input, part, sep); ) { - rtrim(part); - parts.emplace_back(part); - } - - return parts; -} - -unsigned int -calculate_hash(string_view str) -{ - unsigned int value = 0; - for ( auto chr: str ) { - value += static_cast(chr); - value *= 17; - } - return value % 256; -} - -void -part1() -{ - const auto parts = split(read_file("data/day15.txt"), ','); - cout << accumulate(parts.begin(), parts.end(), 0UL, [](auto init, const auto& str) { return init + calculate_hash(str); }) << endl; -} - -void -part2() -{ - vector>> boxes(256); - - const auto line = read_file("data/day15.txt"); - const auto parts = split(line, ','); - for ( const auto& part: parts ) { - const auto pos = part.find_first_of("=-"); - const auto lens = part.substr(0, pos); - auto& box = boxes[calculate_hash(lens)]; - - if ( part[pos] == '=' ) { - const auto value = stol(part.substr(pos + 1)); - - auto iter = find_if(box.begin(), box.end(), [&lens](const auto& element) { return get<0>(element) == lens; }); - if ( iter == box.end() ) { - box.emplace_back(lens, value); - } - else { - get<1>(*iter) = value; - } - } - else { - box.remove_if([&lens](const auto& element) { return get<0>(element) == lens; }); - } - } - - long value = 0; - for ( size_t idx = 0; idx != boxes.size(); ++idx ) { - const auto& box = boxes[idx]; - - long lens_number = 1; - for ( const auto& lens: box ) { - value += static_cast(idx + 1) * lens_number * get<1>(lens); - ++lens_number; - } - } - cout << value << endl; -} - -int -main() -{ - part1(); - part2(); -} -- cgit v1.3