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 ... :) --- 2023/src/day15.cpp | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 2023/src/day15.cpp (limited to '2023/src/day15.cpp') diff --git a/2023/src/day15.cpp b/2023/src/day15.cpp new file mode 100644 index 0000000..6fcbfc2 --- /dev/null +++ b/2023/src/day15.cpp @@ -0,0 +1,102 @@ +#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