1

I have updated my post to include all my code. My problem is I am testing my program with multiple text files and on one of them it successfully reads both lines where as the others it does not. Can someone please help me figure out why?

Each file will only contain two lines of text

The output for the part one print statement is 0 for the test file that does not work and it only displays the first line and does no computations for the part two print statement. The output should be 1068788 for part two. In all cases part one does not matter.

An example of a file that works:

100123
41,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,37,x,x,x,x,x,557,x,29,x,x,x,x,x,x,x,x,x,x,419,x,x,19

An example of a file that does not work:

1068781
7,13,x,x,59,x,31,19

My main:

#include <iostream>
#include <vector>
#include <string>
#include <fstream>

std::vector<std::string> tokenize(const std::string& s, const char delim)
{
    size_t start;
    size_t end = 0;
    std::vector<std::string> result;

    while ((start = s.find_first_not_of(delim, end)) != std::string::npos)
    {
        end = s.find(delim, start);
        result.push_back(s.substr(start, end - start));
    }

    return result;
}

int part_one(const std::vector<int>& buses, int timestamp)
{
    int min_difference = timestamp;
    int best_bus = 0;

    for (int b : buses)
    {
        if (timestamp % b == 0)
        {
            return 0;
        }

        int diff = (((timestamp / b) + 1) * b) - timestamp;
        if (diff < min_difference)
        {
            min_difference = diff;
            best_bus = b;
        }
    }

    return min_difference * best_bus;
}

uint64_t part_two_slow(const std::vector<std::pair<int, int>>& buses)
{

    int biggest_value = 0;
    int biggest_index = 0;

    for (int i = 0; i < buses.size(); i++)
    {
        if (buses[i].first > biggest_value)
        {
            biggest_value = buses[i].first;
            biggest_index = buses[i].second;
        }
    }

    uint64_t t = biggest_value;
    uint64_t multiple = 100000000000000 / biggest_value;
    uint64_t t0 = (biggest_value * multiple) - biggest_index;
    while (true)
    {

        t0 = (biggest_value * multiple) - biggest_index;
        multiple++;
        bool found = true;
        for (int i = 0; i < buses.size(); i++)
        {
            if (buses[i].second == biggest_index)
            {
                continue;
            }
            else if ((t0 + buses[i].second) % buses[i].first != 0)
            {
                found = false;
                break;
            }
        }


        if (found)
        {
            return t0;
        }

    }
    return 0;
}


std::vector<std::pair<int, int>> modular_equations(const std::vector<std::pair<int, int>>& buses)
{
    std::vector<std::pair<int, int>> r;

    for (auto b : buses)
    {
        r.push_back(std::make_pair(((-b.second % b.first) + b.first) % b.first, b.first));
    }

    return r;
}

int64_t mod_pos(int64_t a, int64_t b)
{
    return ((a % b) + b) % b;
}


int64_t part_two(const std::vector<std::pair<int, int>>& buses)
{
    auto mes = modular_equations(buses);
    std::pair<int64_t, int64_t> r = std::make_pair(0, 1);

    for (auto me : mes)
    {
        int64_t coeficient = r.second;

        for (int64_t i = 1; i <= me.second; i++)
        {
            if (mod_pos(coeficient * i, me.second) == 1)
            {
                r = std::make_pair(mod_pos((me.first - r.first) * i, me.second) * r.second + r.first, r.second * me.second);
                break;
            }

        }
    }
    // std::cout << r.first << ", " << r.second << std::endl;
    return r.first;
}


int main()
{
    std::ifstream in("bus.txt", std::ios::in);
    int timestamp;
    std::string notes;

    in >> timestamp;
    in >> notes;

    auto tokens = tokenize(notes, ',');

    std::vector<int> buses;
    std::vector<std::pair<int, int>> buses2;
    
    int counter = 0;
    for (auto t : tokens)
    {
        if (t != "x")
        {
            int val = std::stoi(t);
            buses.push_back(val);
            buses2.push_back(std::make_pair(val, counter));
        }
        counter++;
    }

    std::cout << "Part one: " << part_one(buses, timestamp) << std::endl;
    std::cout << "Part two: " << part_two(buses2) << std::endl;

}
  • I suspect the `tokenize` function is not working as it should but there is no way to know with only this code. Did you step through this with a debugger to see if `tokens` or `buses` is what you expect? – perivesta May 05 '22 at 16:37
  • I will add the tokenize function then. – blindfox916 May 05 '22 at 16:42
  • Stepping through it did not pick anything up. – blindfox916 May 05 '22 at 16:44
  • The code works for me as expected up until the last 2 lines (buses and buses2 are correct). Your problem is in one of the printing functions. – perivesta May 05 '22 at 16:59
  • https://godbolt.org/z/qxMqcdWje – perivesta May 05 '22 at 17:00
  • "I am testing my program with multiple text files and on one of them it successfully reads both lines where as the others it does not" -- Please specify exactly the desired behavior and the actual behavior. Phrases such as "it doesn't work" or "it is not successful" [are not sufficient descriptions of an error](https://idownvotedbecau.se/itsnotworking/). – Andreas Wenzel May 05 '22 at 18:00
  • I updated and added more information. Thank you! – blindfox916 May 05 '22 at 18:35

0 Answers0