OK, I solved a few days of the on-going this year’s Advent of Code, so maybe it is a good idea to document it a bit so that those who like Raku can look at it too.
So, the problem of Day 1 is to take a long list of integers and find such two neighbouring numbers that add to to 2020. The second part of the task is to find three such numbers.
I saved a list of integers in a separate text file (it is also the case for all further tasks), so I use Raku to read the data from it. Then, we can use the combinations
method, which is built-in in Raku and is doing exactly what we need for this task: it takes a list and returns all combinations of 2 or 3 elements:
my @data = 'data.txt'.IO.lines; say [*] @data.combinations(2).first(*.sum == 2020); say [*] @data.combinations(3).first(*.sum == 2020);
And that’s it for this day 🙂
* * *
→ Browse the code on GitHub
→ See all blog posts on Advent of Code 2020
One thought on “Advent of Code 2020 Day 1/25 in the Raku programming language”