From e67bd318726418f37d1541939ac3e97667995d8d Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Mon, 18 Dec 2023 19:12:58 +0100 Subject: Lösungen für Tag 18 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/day18.cpp | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 src/day18.cpp (limited to 'src') diff --git a/src/day18.cpp b/src/day18.cpp new file mode 100644 index 0000000..3037739 --- /dev/null +++ b/src/day18.cpp @@ -0,0 +1,115 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +using puzzle_t = vector>; + +puzzle_t +read_file_part1(string_view filename, long& border_steps) +{ + static map> movement = { + { 'U', { 0, -1 } }, // up + { 'D', { 0, 1 } }, // down + { 'L', { -1, 0 } }, // left + { 'R', { 1, 0 } }, // right + }; + + border_steps = 0; + + fstream input{ filename }; + puzzle_t data; + + data.emplace_back(0, 0); + + for ( string line; getline(input, line); ) { + char dir{}; + long steps{}; + stringstream sstrm{ line }; + + if ( sstrm >> dir >> steps ) { + data.emplace_back(get<0>(data.back()) + get<0>(movement[dir]) * steps, + get<1>(data.back()) + get<1>(movement[dir]) * steps); + + border_steps += steps; + } + } + + return data; +} + +puzzle_t +read_file_part2(string_view filename, long& border_steps) +{ + static map> movement = { + { '0', { 1, 0 } }, // right + { '1', { 0, 1 } }, // down + { '2', { -1, 0 } }, // left + { '3', { 0, -1 } }, // up + }; + + border_steps = 0; + + fstream input{ filename }; + puzzle_t data; + + data.emplace_back(0, 0); + + for ( string line; getline(input, line); ) { + char dir{}; + long steps{}; + string hexcode; + stringstream sstrm{ line }; + + if ( sstrm >> dir >> steps >> hexcode ) { + steps = stol(hexcode.substr(2, 5), nullptr, 16); + dir = hexcode[7]; + + data.emplace_back(get<0>(data.back()) + get<0>(movement[dir]) * steps, + get<1>(data.back()) + get<1>(movement[dir]) * steps); + + border_steps += steps; + } + } + + return data; +} + +long +shoelace(const puzzle_t& puzzle, const long border_steps) +{ + long area = 0; + for ( size_t i = 1; i < puzzle.size(); ++i ) { + const auto [x1, y1] = puzzle[i - 1]; + const auto [x2, y2] = puzzle[i]; + + area += (x1 * y2 - y1 * x2); + } + return (border_steps + area) / 2 + 1; +} + +void +part1() +{ + long border_steps = 0; + auto puzzle = read_file_part1("data/day18.txt", border_steps); + cout << shoelace(puzzle, border_steps) << endl; +} + +void +part2() +{ + long border_steps = 0; + auto puzzle = read_file_part2("data/day18.txt", border_steps); + cout << shoelace(puzzle, border_steps) << endl; +} + +int +main() +{ + part1(); + part2(); +} -- cgit v1.3