Welcome everyone to the 2023 advent of code! Thank you all for stopping by and participating in it in programming.dev whether youre new to the event or doing it again.

This is an unofficial community for the event as no official spot exists on lemmy but ill be running it as best I can with Sigmatics modding as well. Ill be running a solution megathread every day where you can share solutions with other participants to compare your answers and to see the things other people come up with


Day 1: Trebuchet?!


Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ or pastebin (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ


🔒This post will be unlocked when there is a decent amount of submissions on the leaderboard to avoid cheating for top spots

🔓 Edit: Post has been unlocked after 6 minutes

  • CameronDev@programming.devM
    link
    fedilink
    arrow-up
    3
    ·
    edit-2
    1 year ago

    I did this in C. First part was fairly trivial, iterate over the line, find first and last number, easy.

    Second part had me a bit worried i would need a more string friendly library/language, until i worked out that i can just strstr to find “one”, and then in place switch that to “o1e”, and so on. Then run part1 code over the modified buffer. I originally did “1ne”, but overlaps such as “eightwo” meant that i got the 2, but missed the 8.

    #include 
    #include 
    #include 
    #include 
    #include 
    
    size_t readfile(char* fname, char* buffer, size_t buffer_len)
    {
    
        int f = open(fname, 'r');
        assert(f >= 0);
        size_t total = 0;
        do {
            size_t nr = read(f, buffer + total, buffer_len - total);
            if (nr == 0) {
                return total;
            }
            total += nr;
        }
        while (buffer_len - total > 0);
        return -1;
    }
    
    int part1(const char* buffer, size_t buffer_len)
    {
        int first = -1;
        int last = -1;
        int total = 0;
        for (int i = 0; i < buffer_len; i++)
        {
            char c = buffer[i];
            if (c == '\n')
            {
                if (first == -1) {
                    continue;
                }
                total += (first*10 + last);
                first = last = -1;
                continue;
            }
            int val = c - '0';
            if (val > 9 || val < 0)
            {
                continue;
            }
            if (first == -1)
            {
                first = last = val;
            }
            else
            {
                last = val;
            }
        }
        return total;
    }
    
    void part2_sanitize(char* buffer, size_t len)
    {
        char* p = NULL;
        while ((p = strnstr(buffer, "one", len)) != NULL)
        {
            p[1] = '1';
        }
        while ((p = strnstr(buffer, "two", len)) != NULL)
        {
            p[1] = '2';
        }
        while ((p = strnstr(buffer, "three", len)) != NULL)
        {
            p[1] = '3';
        }
        while ((p = strnstr(buffer, "four", len)) != NULL)
        {
            p[1] = '4';
        }
        while ((p = strnstr(buffer, "five", len)) != NULL)
        {
            p[1] = '5';
        }
        while ((p = strnstr(buffer, "six", len)) != NULL)
        {
            p[1] = '6';
        }
        while ((p = strnstr(buffer, "seven", len)) != NULL)
        {
            p[1] = '7';
        }
        while ((p = strnstr(buffer, "eight", len)) != NULL)
        {
            p[1] = '8';
        }
        while ((p = strnstr(buffer, "nine", len)) != NULL)
        {
            p[1] = '9';
        }
        while ((p = strnstr(buffer, "zero", len)) != NULL)
        {
            p[1] = '0';
        }
    }
    
    int main(int argc, char** argv)
    {
        assert(argc == 2);
        char buffer[1000000];
        size_t len = readfile(argv[1], buffer, sizeof(buffer));
        {
            int total = part1(buffer, len);
            printf("Part 1 total: %i\n", total);
        }
    
        {
            part2_sanitize(buffer, len);
            int total = part1(buffer, len);
            printf("Part 2 total: %i\n", total);
        }
    }
    
    • CameronDev@programming.devM
      link
      fedilink
      arrow-up
      3
      ·
      1 year ago

      Just realised how inefficient the sanitize function is, it iterates over the buffer way too many times. Should be restarting the strnstr from the location of the last hit instead of from the start.