From 1165d51cc10b5795e1ec78f3eb5c9b56bfb31d18 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sun, 10 Dec 2023 10:41:42 +0100 Subject: Lösung für Tag 10, Teil 1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- makefile | 3 +- src/day10.cpp | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 src/day10.cpp diff --git a/makefile b/makefile index 2e63b97..0b89873 100644 --- a/makefile +++ b/makefile @@ -8,7 +8,8 @@ all: bin/day01 \ bin/day06 \ bin/day07 \ bin/day08 \ - bin/day09 + bin/day09 \ + bin/day10 bin: mkdir $@ diff --git a/src/day10.cpp b/src/day10.cpp new file mode 100644 index 0000000..b9d6355 --- /dev/null +++ b/src/day10.cpp @@ -0,0 +1,144 @@ +#include +#include +#include +#include +#include +using namespace std; + +vector +read_file(string_view filename) +{ + fstream input{ filename }; + vector data; + + for ( string line; getline(input, line); ) { + data.emplace_back(line); + } + + return data; +} + +void +part1() +{ + typedef tuple pos_t; + + const auto puzzle = read_file("data/day10.txt"); + + const auto find_start_pos = [&]() -> pos_t { + for ( size_t y = 0; y != puzzle.size(); ++y ) { // NOLINT + auto x = puzzle[y].find('S'); // NOLINT + if ( x != string::npos ) { + return { x, y }; + } + } + + return { 0, 0 }; + }; + + const auto predict_direction = [&](pos_t& current, char& direction) -> bool { + auto x = get<0>(current); // NOLINT + auto y = get<1>(current); // NOLINT + + if ( direction == 'S' ) { + ++y; + if ( y >= puzzle.size() ) { + return false; + } + auto tile = puzzle[y][x]; + if ( tile != 'J' && tile != '|' && tile != 'L' ) { + return false; + } + if ( tile == 'J' ) { + direction = 'W'; + } + else if ( tile == 'L' ) { + direction = 'E'; + } + } + else if ( direction == 'N' ) { + --y; + if ( y >= puzzle.size() ) { + return false; + } + auto tile = puzzle[y][x]; + if ( tile != '7' && tile != '|' && tile != 'F' ) { + return false; + } + if ( tile == '7' ) { + direction = 'W'; + } + else if ( tile == 'F' ) { + direction = 'E'; + } + } + else if ( direction == 'E' ) { + ++x; + if ( x >= puzzle[y].size() ) { + return false; + } + auto tile = puzzle[y][x]; + if ( tile != 'J' && tile != '-' && tile != '7' ) { + return false; + } + if ( tile == 'J' ) { + direction = 'N'; + } + else if ( tile == '7' ) { + direction = 'S'; + } + } + else if ( direction == 'W' ) { + --x; + if ( x >= puzzle[y].size() ) { + return false; + } + auto tile = puzzle[y][x]; + if ( tile != 'L' && tile != '-' && tile != 'F' ) { + return false; + } + if ( tile == 'L' ) { + direction = 'N'; + } + else if ( tile == 'F' ) { + direction = 'S'; + } + } + else { + cerr << "invalid direction " << direction << ")!" << endl; + return false; + } + + current = { x, y }; + return true; + }; + + const auto start_pos = find_start_pos(); + + auto max_steps = 0; + + for ( const auto direction: { 'N', 'S', 'E', 'W' } ) { + auto current_direction = direction; + auto current_pos = start_pos; + + auto steps = 0; + for ( ;; ) { + ++steps; + + if ( !predict_direction(current_pos, current_direction) ) { + break; + } + } + cout << "Richtung: " << direction << " - Schritte: " << steps << endl; + + max_steps = max(max_steps, steps); + } + + cout << max_steps / 2 << endl; +} + +int +main() +{ + part1(); +} -- cgit v1.3