Day 8 – Practice… on Advent of Code

“Hrmpf!” mutter mutter mutter “Bah!”

The head elf Fooby Nimblecalmy was trying to to read an interesting article on Ramsey Theory, but was having a hard time because the latest addition in Santa’s IT Operations Buzz Bargoosey was steaming like a kettle.

Anyway, Fooby was determined to go through the article, so decided to deliberately ignore Buzz.

“Grump! Moan… moan… moan…”

It wasn’t going to end any soon and Ramsey Theory definitely requires attention, so Fooby decided to bite the bullet and ask:

“Well… what’s up Buzz?”

“Uh, oh… sorry Sir Nimblecalmy, nothing Sir…”

“I told you not to call me Sir, and you’re not going to end any soon… so again, what’s up Buzz?”

“Well Si…AHEM Fooby, the fact is that I’m bored to death! There’s nothing to do here!”

Fooby looked at Buzz from over the glasses and noticed a shockingly resemblance to… a younger Fooby, too many years ago. Only that, at the time, there were a lot of automation tasks and there was this new shiny programming language, Perl

Buzz had a point though. They basically implemented all the implementable so far, so these days it was all maintenance every now and then. And not all the other elves were so much into mathematics.

Even though…

“You know… we will soon have to upgrade a few programs here, to take advantage of the more recent multi-core processors. I heard Raku is perfect for going parallel without too much hassle!”

“Oh, Raku yes… it would be great!”

Buzz’s face was bright and dreamy again, so Fooby was ready to delve again into Ramsey Theory. A tad too fast, though, because Buzz sighted in a loud and clear way.

“What’s up again, son?”

“Well… I know a little about Raku, but I need to exercise a lot and I don’t know what to do about it!”

Now it was Fooby’s time to sigh. Ramsey Theory was quickly fading at the horizon… when light came, suddenly!

“Why don’t you try to solve a couple of puzzles a day, say up to Christmas day? You might start with something simple, and increase complexity as days go…”

“This would be brilliant Sir! Yes!”

“So… first of all don’t call me Sir, then head over to Advent of Code and start from the beginning! Each day you will be facing a puzzle, and another one will be available after you solve the first one.”

“OK yes… but how will this help me to learn Raku?!?”

“Well, just by solving the problems you will need to use the language and understand how things can be done in at least one way. Then… after you have solved the daily puzzles, or if you’re stuck, you can head to the Solution Megathreads in Reddit and look for other Raku solutions… there’s a lot of clever people there, although still a tad too few.”

Buzz was thrilled by the idea, but still unsure about it. So Fooby decided to show an example, just to get Buzz started.

“Let’s take day 1 as an example. In part 1, we have a list of numbers in a file, and we have to find out how many times a number is greater than the one immediately before”

“Oh, I know I know! This is how to do it” and started writing on the terminal:

my @numbers = $filename.IO.lines;
my $count = 0;
for 1 .. @numbers.end -> $i {
    ++$count if $numbers[$i - 1] < $numbers[$i];
put $count;

Fooby noticed that Buzz already had some confidence with Raku, but also that there was definitely space for improvement.

“Well, that’s a good start for sure! Now you can take a look at seaker’s solution for fun and learning” and showed Buzz this:

#!/bin/env raku

sub MAIN(Str:D $f where *.IO.e = 'input.txt') {
    my @n = $f.IO.words;
    put 'part 1: ', [+] @n Z< @n[1..*];
    put 'part 2: ', [+] @n Z< @n[3..*];

“You see? seaker is being more precise by using .words instead of .lines, which improves readability.”

“OK but… what’s with that way of calculating the result for part 1?!?”

“Oh, that’s part of the Christmas Magic! I mean, Raku‘s Magic. First, let’s consider the zip metaoperator Z:”

@n Z< @n[1..*]

“It takes one element from the left and one from the right, and applies the comparison operator < to the pair”.

“OK but… what’s with @n[1..*] on the right?” asked Buzz.

“Well, that’s a lazy indexing of the array @n. Raku takes elements as long as they are needed, and nothing more. So here the @n on the left will basically determine how many elements to take. The right hand side might go beyond the end of the array, but it’s not a problem in this case because they will not yield a True value in the comparison”.

“OK, so we’re left with an array of booleans, right?”

“Right. This is where the [+] comes into play. It’s a sum operator +, wrapped into a reduction metaoperator“.

“Oh I know, I know! The Red Auction is when we offer Christmas sweeties to get Santa’s hat on December 26th, right?”

Fooby took off the glasses and massaged a bit the bridge over the nose. Ramsey Theory had never been farther…

“No… not the da…rling Red Auction, but reduction. It’s an operation on a list of values, that takes the first two items and applies an operator to get a result. Then it takes the result and the next item, applies the operation again, and so on. Reduction, because it reduces a list down to a single item. In this case, the operation is the sum and the result is the sum of all values.”

“OK, I get it. But wait! We’re summing booleans here… it that allowed?”

“Good catch!” replied Fooby. “Actually yes, because when a boolean is used as a number, it takes a value that is either 0 for False and 1 for True, which is exactly what we need here, because we’re just counting how many True values we have”.

“Right, I see… in the same spirit of Perl, I daresay. What’s with that signature? That is different from Perl!”

sub MAIN(Str:D $f where *.IO.e = 'input.txt') { ...

“Yes, it is indeed. It’s all for a single input parameter $f, actually. The Str part tells us that we’re expecting a string…”

“Oh!” interrupted Buzz. “Does this mean that we have to assign a type to each variable and parameter? Why don’t we do anything to @n? Which type…”. The flood gates were open!

“Hold on! Hold on a second” interrupted Fooby. “Raku has something called gradual typing, in that you assign a type to a variable only if you think it’s useful for you. In this case, the author thought it was useful and set it.”

“Uh, well, sorry Sir… please go on, what’s with the smiley?”

“Please… don’t call me Sir. That’s not a smiley, it’s a type constraint that asks Raku to check that the input is defined.”

“Anyway” intervened Buzz “I’m always happy when things are defined, so it’s still a smiley for me! Now that where…”

“That is an additional constraint on the input” answered Fooby. “The star is a placeholder for $f itself, and the expression asks Raku to check that the input string, when considered a file name, should refer to a file that actually exists in the filesystem.”

“Brilliant!” observed Buzz. “Then I think that the equal to input.txt part sets a default value, right?”

“Precisely!” agreed Fooby.

“Uhm I think I got it… Fine! I’ll look into the other part of the solution, and try some puzzles myself. Thanks a lot Sir!”

Fooby was about to complain about being called Sir, then decided it was not the case and, at last, delved back to that interesting article on Ramsey Theory…

2 thoughts on “Day 8 – Practice… on Advent of Code

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: