From 723922fbf3a1c5233efe74cb4c0793e2ba4105e1 Mon Sep 17 00:00:00 2001 From: bramtechs Date: Fri, 13 Dec 2024 23:01:02 +0100 Subject: [PATCH] Day 03 - Part 01 - Fix Parser and add Lexer --- src/day03.cc | 81 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 66 insertions(+), 15 deletions(-) diff --git a/src/day03.cc b/src/day03.cc index db2a62c..6c552c2 100644 --- a/src/day03.cc +++ b/src/day03.cc @@ -1,7 +1,9 @@ #include +#include #include #include #include +#include #include static std::string input = @@ -27,22 +29,22 @@ class Token { : type(type), text(text), value(value) {} }; -void parse(const std::string_view &input, std::vector &tokens) { +static void parse(const std::string_view &input, + std::vector &outTokens) { if (input.starts_with("mul")) { - tokens.emplace_back(Token::Type::MUL, "mul"); - parse(input.substr(3), tokens); + outTokens.emplace_back(Token::Type::MUL, "mul"); + parse(input.substr(3), outTokens); } else if (input.starts_with("(")) { - tokens.emplace_back(Token::Type::PAREN_OPEN, "("); - parse(input.substr(1), tokens); + outTokens.emplace_back(Token::Type::PAREN_OPEN, "("); + parse(input.substr(1), outTokens); } else if (input.starts_with(")")) { - tokens.emplace_back(Token::Type::PAREN_CLOSE, ")"); - parse(input.substr(1), tokens); + outTokens.emplace_back(Token::Type::PAREN_CLOSE, ")"); + parse(input.substr(1), outTokens); } else if (input.starts_with(",")) { - tokens.emplace_back(Token::Type::COMMA, ","); - parse(input.substr(1), tokens); + outTokens.emplace_back(Token::Type::COMMA, ","); + parse(input.substr(1), outTokens); } else if (!input.empty()) { if (std::isdigit(*input.begin())) { - int value{}; size_t i = 0; for (; i < std::min(input.length(), size_t(3)); i++) { if (!std::isdigit(input.at(i))) { @@ -50,18 +52,67 @@ void parse(const std::string_view &input, std::vector &tokens) { } } - tokens.emplace_back(Token::Type::VALUE, input.substr(0, i), value); - parse(input.substr(i), tokens); + int value = std::stod(input.substr(0, i).data()); + outTokens.emplace_back(Token::Type::VALUE, input.substr(0, i), value); + parse(input.substr(i), outTokens); } else { - tokens.emplace_back(Token::Type::UNKNOWN, *input.begin()); - parse(input.substr(1), tokens); + outTokens.emplace_back(Token::Type::UNKNOWN, *input.begin()); + parse(input.substr(1), outTokens); } } } +static void lex(std::span tokens, + std::vector> &outMuls) { + if (tokens.size() < 5) { + return; + } + + if (tokens.begin()->type != Token::Type::MUL) { + lex(tokens.subspan(1), outMuls); + return; + } + + if (tokens[1].type != Token::Type::PAREN_OPEN) { + lex(tokens.subspan(2), outMuls); + return; + } + + if (tokens[2].type != Token::Type::VALUE) { + lex(tokens.subspan(3), outMuls); + return; + } + + int firstValue = tokens[2].value; + + if (tokens[3].type != Token::Type::COMMA) { + lex(tokens.subspan(4), outMuls); + return; + } + + if (tokens[4].type != Token::Type::VALUE) { + lex(tokens.subspan(5), outMuls); + return; + } + + int secondValue = tokens[4].value; + + if (tokens[5].type != Token::Type::PAREN_CLOSE) { + lex(tokens.subspan(6), outMuls); + return; + } + + // we found a valid sequence! + outMuls.emplace_back(firstValue, secondValue); + lex(tokens.subspan(6), outMuls); +} + int main(int argc, char **argv) { - std::vector tokens; + std::vector tokens{}; parse(input, tokens); + std::vector> muls{}; + lex(tokens, muls); + std::cout << "ok"; } \ No newline at end of file