Skip to content

Commit

Permalink
Day 03 - Part 01 - Fix Parser and add Lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
bramtechs committed Dec 13, 2024
1 parent 85fdef7 commit 723922f
Showing 1 changed file with 66 additions and 15 deletions.
81 changes: 66 additions & 15 deletions src/day03.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <iostream>
#include <span>
#include <stdexcept>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

static std::string input =
Expand All @@ -27,41 +29,90 @@ class Token {
: type(type), text(text), value(value) {}
};

void parse(const std::string_view &input, std::vector<Token> &tokens) {
static void parse(const std::string_view &input,
std::vector<Token> &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))) {
break;
}
}

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<Token> tokens,
std::vector<std::pair<int, int>> &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<Token> tokens;
std::vector<Token> tokens{};
parse(input, tokens);

std::vector<std::pair<int, int>> muls{};
lex(tokens, muls);

std::cout << "ok";
}

0 comments on commit 723922f

Please sign in to comment.