Advent of Code 2024: A Reflection on Using Q

Advent of Code 2024: A Reflection on Using Q

Finally finished the 24.5 days of the advent of code using q this year https://github.com/jglara/aoc-2024-q, not without help from reddit group. I could not have finished it alone for some of the hardest problems; and I can not say I am proud of my solutions, even more after looking at real experts in q solutions like https://q.kx.com/aoc24/index.html; but I really liked the experience.

Key Takeaways

  • Simple data structures and algorithms are often sufficient. For this kind of problems you just need lists, vectors and dictionaries, and not so many algorithms to work with them, and a terse way to compose them to build more comples algorithms. Q provides a good collection of built-in algorithms that can be easily combined.
  • A REPL is invaluable for exploration. A REPL (read-eval-print loop) allows you to experiment with code interactively, which is very helpful for solving these types of problems.
  • Focus on the problem, not the tools. With q you don't have to worry about compilation tools, external libraries or ides. Just emacs , the repl and Qs built-in functionality, allowing you to focus on solving the problem itself. In contrast, I found that when using other languages in previous years, I spent a lot of time dealing with external tools and libraries.
  • Limited error messages and parsing can be challenges. The one drawback I found with Q is that the error messages can be cryptic at times. Additionally, Q lacks built-in parsing libraries for complex input data. For the latter, I relied on Perl to pre-process the data before feeding it into Q.

Q vs. C++

After completing the Advent of Code challenges in Q, I was curious to see how I would fare using C++. Initially, I thought it would be straightforward to translate the Q code to C++, but I found that the C++ solutions were not as elegant.

Compare solutions for Day 1 in both languages:

Q:

/ part 1: sum differences after ordering 
d1p1:{ sum abs (asc x[;0]) - (asc x[;1]) }

/ part 2: count each different value in second column and multiply key by value if it appears in column 1
d1p2:{ k: count each group x[;1]; sum x[;0]  k x[;0] }         


C++:

int day01a(std::vector<int> &c1, std::vector<int> &c2) {
    std::ranges::sort(c1);
    std::ranges::sort(c2);


    auto total = std::ranges::fold_left(
        std::views::zip(c1, c2) |
        std::views::transform([](const auto &p) {
            auto [a,b] = p;
            return std::abs(a-b);
        }),
        0,
        std::plus<>{});

    return total;
}

int day01b(std::vector<int> &c1, std::vector<int> &c2)
{
    std::unordered_map<int, int> m;
    std::ranges::for_each(c2, [&m] (const auto &x) {
        m[x]++;
    });

    auto total = std::ranges::fold_left(
        c1 | std::views::transform([&m] (auto x) {
            return x*m[x];
        }),
        0,
        std::plus<>{});

    return total;
    
}        

For learning more about q language this is a good guide: https://www.defconq.tech/docs/studyPlan/kdbDevs



Jose Antonio Garvayo

Software Designer at Ericsson

1 个月

Thanks for your blog, it is very useful for newcomers to q

回复
Alexander Unterrainer

KDB/Q Consultant | Blogger | DefconQ | Data Intellect

1 个月

Thanks for mentioning my blog post ??

回复

要查看或添加评论,请登录

Jose Antonio Garvayo的更多文章

  • Halfway Through Advent of Code 2024

    Halfway Through Advent of Code 2024

    I've just crossed the halfway point of Advent of Code, and let me tell you I’m struggling as usual. This year, I…

    5 条评论
  • Reinforcement Learning in network control systems

    Reinforcement Learning in network control systems

    While there’s currently much excitement surrounding Large Language Models (LLMs), another AI breakthrough that once…

  • Rust, eBPF, and Deep Learning: A Powerful Trio

    Rust, eBPF, and Deep Learning: A Powerful Trio

    I've been exploring the potential of eBPF (extended Berkeley Packet Filter) for traffic detection and network security.…

社区洞察

其他会员也浏览了