diff --git a/src/days/06/Day06.cpp b/src/days/06/Day06.cpp index 34565f1..7719683 100644 --- a/src/days/06/Day06.cpp +++ b/src/days/06/Day06.cpp @@ -28,8 +28,8 @@ Result Day06::Task2() { vector Day06::parseRecords() const { vector records; - vector time = parseInts(input[0].substr(5)); - vector distance = parseInts(input[1].substr(10)); + vector time = parseUInts(input[0].substr(5)); + vector distance = parseUInts(input[1].substr(10)); for (size_t i = 0; i < time.size(); i++) records.emplace_back(time[i], distance[i]); diff --git a/src/util.cpp b/src/util.cpp index 16ea5e1..8891f73 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -46,7 +46,7 @@ bool isDigit(char c) { return val >= 0 && val < 10; } -vector parseInts(const string &data) { +vector parseUInts(const string &data) { vector result; uint64 value = 0; @@ -66,5 +66,34 @@ vector parseInts(const string &data) { if (readingInt) result.push_back(value); + return result; +} + +vector parseInts(const string &data) { + vector result; + + int64 value = 0; + bool readingInt = false; + bool negative = false; + + for (const char c : data){ + uint8_t digit; + if (isDigit(c, digit)) { + value = value * 10 + digit; + readingInt = true; + } else if (!readingInt && c == '-') { + negative = true; + } else if (readingInt){ + readingInt = false; + result.push_back(value * (negative ? -1 : 1)); + value = 0; + negative = false; + } else { + negative = false; + } + } + if (readingInt) + result.push_back(value); + return result; } \ No newline at end of file diff --git a/src/util.h b/src/util.h index 6be798c..6d3a59c 100644 --- a/src/util.h +++ b/src/util.h @@ -30,4 +30,5 @@ bool isDigit(char c, uint8_t &result); bool isDigit(char c); -vector parseInts(const string &data); \ No newline at end of file +vector parseUInts(const string &data); +vector parseInts(const string &data); \ No newline at end of file