Advent of Code Day 4: Counting Rolls of Paper

On Day 4 of Advent of Code, we’re finding which rolls of paper forklifts can access, so we can get further into the North Pole base.

I model the diagram of rolls of paper as a boolean grid. A cell is true if it has a roll.

Finding adjacent positions

To find the adjacent positions, I add -1, 0, 1 to each coordinate, with proper bounds checking.

Here’s my FindAdjacentPositions():

static IEnumerable<bool> FindAdjacentPositions(bool[][] rollsOfPaper, int i, int j)
{
    for (int row = -1; row <= 1; row++)
    {
        for (int col = -1; col <= 1; col++)
        {
            var x = i + row;
            var y = j + col;
            if (x >= 0 && x < rollsOfPaper.Length
                    && y >= 0 && y < rollsOfPaper[x].Length)
            {
                if (x == i && y == j)
                {
                    continue;
                }

                var cell = rollsOfPaper[x][y];
                yield return cell;
            }
        }
    }
}

After that, the next step is filtering and counting the rolls with less than 4 neighbor rolls.

Filtering and counting

Here’s my final solution,

var rollsOfPaper = new bool[][]
{
    [ false, false, true, true, false, true, true, true, true, false,  ],
    [ true, true, true, false, true, false, true, false, true, true,  ],
    [ true, true, true, true, true, false, true, false, true, true,  ],
    [ true, false, true, true, true, true, false, false, true, false,  ],
    [ true, true, false, true, true, true, true, false, true, true,  ],
    [ false, true, true, true, true, true, true, true, false, true,  ],
    [ false, true, false, true, false, true, false, true, true, true,  ],
    [ true, false, true, true, true, false, true, true, true, true,  ],
    [ false, true, true, true, true, true, true, true, true, false,  ],
    [ true, false, true, false, true, true, true, false, true, false,  ],
};

var count = 0;

for (int i = 0; i < rollsOfPaper.Length; i++)
{
    for (int j = 0; j < rollsOfPaper[i].Length; j++)
    {
        if (rollsOfPaper[i][j])
        {
            var adjacents = FindAdjacentPositions(rollsOfPaper, i, j);
            if (adjacents.Count(roll => roll) < 4)
            {
                count++;
            }
        }
    }
}

Console.WriteLine(count);
Console.ReadKey();

static IEnumerable<bool> FindAdjacentPositions(bool[][] rollsOfPaper, int i, int j)
{
    for (int row = -1; row <= 1; row++)
    {
        for (int col = -1; col <= 1; col++)
        {
            var x = i + row;
            var y = j + col;
            if (x >= 0 && x < rollsOfPaper.Length
                    && y >= 0 && y < rollsOfPaper[x].Length)
            {
                if (x == i && y == j)
                {
                    continue;
                }

                var cell = rollsOfPaper[x][y];
                yield return cell;
            }
        }
    }
}

My goal is to use “functionalish” solutions, but two loops and an accumulator were so easy that I stuck with them.

Et voilà!

Advent of Code sharpens your coding skills. But coding is more than typing symbols fast. It’s also about teamwork, collaboration, and many skills I share in my book, Street-Smart Coding: 30 Ways to Get Better at Coding. That’s the roadmap I wish I’d known from day one.

Get your copy of Street-Smart Coding here

My 400 Daily Posts Reflection

Writing 400 daily posts gave me momentum as a writer.

The first 100 posts were training to find ideas. The next 100 proved I wouldn’t run out of ideas. The next 100 tested my discipline, I wrote even when tired. This last 100 gave me trust to keep going.

In the last 3 months, I released a book, Street-Smart Coding and I shared the behind-the-scenes:

Promoting my book won me my first hater, a milestone in the writing journey.

But beyond criticism, writing daily gave me something more lasting: “the time capsule” effect.

And after many posts, I can see what I was learning each day years ago. I even started to write some “on this day” posts. Here’s the last one I wrote.

That’s a gift for future generations: reading what grandpa wrote 20 years ago.

My most-read and favorite posts

Here are some of the most-read posts from the last 100 days:

Here are my 100-post, 200-post, and 300-post. Next milestone to celebrate: 500 daily posts.

Friday Links: Confessions, manager pendulum, and the junior hiring crisis

Hey! Here are 3 reads (plus a funny bonus) I found interesting this week.

#1. After being afraid of sharing them, here are the confessions of an uncensored coder (10min). I agree, we don’t have to learn everything about everything. And I have to confess I don’t fully understand Blazor, in spite of working with it for almost a year.

#2. Most companies only offer two tracks for coders. You either stay as an Individual Contributor or join the Management side. Switching between the two is called: “manager pendulum.” But AI might be breaking the pendulum (10min), creating hybrid roles. Or maybe it’s just an excuse to get rid of the role and the compensation?

#3. It’s easier than ever to start coding. But it’s getting harder and harder to break into the market as a junior coder. We’re in a junior hiring crisis (10min). AI seems to have a lot with it…And just in the past few weeks, someone in the same situation reached out on LinkedIn asking for advice. I didn’t have much to say.

Bonus: What if we let AI replace our bosses? Here’s replaceyourboss.ai. The funniest line I got: “Let’s cut the single-use cups but keep the private jet for strategic mobility.”


On my blog, this past week, I shared about 4 AI issues I’ve collected (2min) and 4 lessons I learned while fighting next to my mom with a chronic disease (3min). Feel free to skip that last one if you only want to read about coding. I won’t get mad.


(Bzzz…Radio voice) This email was brought to you by… My latest book, Street-Smart Coding: 30 lessons to help you code like a pro, from Googling to clear communication. It’s the roadmap I wish I had moving from junior to senior and the one I hope helps you too.

See you next time,

Cesar

Advent of Code Day 3: Calculating the Joltage of Batteries

On Day 3 of Advent of Code, we’re finding the joltage of a bank of batteries to power an escalator.

Elevators are out of service and the escalator needs the batteries with the higher joltage.

Finding pairs of batteries

I use brute force to find all battery pairs.

Two for loops didn’t feel old-schoool. So I borrow a LINQ query from this StackOverflow answer.

static class Extensions
{
    public static IEnumerable<(T First, T Second)> Pairs<T>(this IEnumerable<T> self)
    {
        return self.SelectMany((fst, i) => self.Skip(i + 1).Select(snd => (fst, snd)));
    }
}

Calculating the joltage

Once we have the pairs, I need two methods to calculate joltage: for a pair and for a bank.

static int FindJoltage(Bank bank)
    => bank.Batteries.Pairs().Select(FindJoltageOfPair).Max();

static int FindJoltageOfPair((int Battery1, int Battery2) pair)
    => pair.Battery1 * 10 + pair.Battery2;

Here’s my full solution:


var allBanks = new List<Bank>
{
    new Bank([ 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1, 1, 1 ]),
    new Bank([ 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9 ]),
    new Bank([ 2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 7, 8 ]),
    new Bank([ 8, 1, 8, 1, 8, 1, 9, 1, 1, 1, 1, 2, 1, 1, 1 ])
};
var totalJoltage = allBanks.Select(FindJoltage).Sum();
Console.WriteLine(totalJoltage);
Console.ReadLine();

static int FindJoltage(Bank bank)
    => bank.Batteries.Pairs().Select(FindJoltageOfPair).Max();

static int FindJoltageOfPair((int Battery1, int Battery2) pair)
    => pair.Battery1 * 10 + pair.Battery2;

record Bank(IEnumerable<int> Batteries);

static class Extensions
{
    public static IEnumerable<(T First, T Second)> Pairs<T>(this IEnumerable<T> self)
    {
        return self.SelectMany((fst, i) => self.Skip(i + 1).Select(snd => (fst, snd)));
    }
}

Et voilà!

Advent of Code sharpens your coding skills. But coding is more than typing symbols fast. It’s also about teamwork, collaboration, and many skills I share in my book, Street-Smart Coding: 30 Ways to Get Better at Coding. That’s the roadmap I wish I’d known from day one.

Get your copy of Street-Smart Coding here

Advent of Code Day 2: Finding Invalid Product IDs

On Day 2 of Advent of Code, we’re helping the Elves find invalid IDs.

I considered a string-based solution, but that felt like cheating. So I tried a “numeric” one instead.

Let’s start with a bottom-up approach.

Finding if a product ID is invalid

First, I count the digits of a number, then split the number in half using powers of 10.

I borrowed this StackOverflow answer to count digits.

And since a product ID is invalid if it has a sequence repeated twice, product IDs with an odd number of digits are valid.

static bool IsAnInvalidId(int n)
    => HasSequenceRepeatedTwice(n);

static bool HasSequenceRepeatedTwice(int n)
{
    var digitCount = (int)(Math.Log10(n) + 1);
    if (digitCount % 2 != 0)
    {
        return false;
    }

    var firstHalf = n / (int)Math.Pow(10, digitCount / 2);
    var secondHalf = n % (int)Math.Pow(10, digitCount / 2);

    return firstHalf == secondHalf;
}

Once we check for repeated sequences, the only task left is counting IDs in each range.

Counting invalid product IDs

I’m creating a function to enumerate a range, Enumerate(), and a LINQ query to find and count invalid IDs.

Here’s my full solution:

var ranges = new[]
{
    new Range(11, 22),
    new Range(95, 115),
    new Range(998, 1012),
    new Range(1188511880, 1188511890),
    new Range(222220, 222224),
    new Range(1698522, 1698528),
    new Range(446443, 446449),
    new Range(38593856, 38593862),
    new Range(565653, 565659),
    new Range(824824821, 824824827),
    new Range(2121212118, 2121212124),
};

var sum = ranges.SelectMany(Enumerate)
                .Where(IsAnInvalidId)
                .Sum();
Console.WriteLine(sum);
Console.ReadLine();

static IEnumerable<int> Enumerate(Range r)
    => Enumerable.Range(r.Left, r.Right - r.Left + 1);

static bool IsAnInvalidId(int n)
    => HasSequenceRepeatedTwice(n);

static bool HasSequenceRepeatedTwice(int n)
{
    var digitCount = (int)(Math.Log10(n) + 1);
    if (digitCount % 2 != 0)
    {
        return false;
    }

    var firstHalf = n / (int)Math.Pow(10, digitCount / 2);
    var secondHalf = n % (int)Math.Pow(10, digitCount / 2);

    return firstHalf == secondHalf;
}

record Range(int Left, int Right);

Et voilà!

Advent of Code sharpens your coding skills. But coding is more than typing symbols fast. It’s also about teamwork, collaboration, and many skills I share in my book, Street-Smart Coding: 30 Ways to Get Better at Coding. That’s the roadmap I wish I’d known from day one.

Get your copy of Street-Smart Coding here