AoC# 2024 - Day 5: Print Queue

1 minute read

It’s Advent of Code time 🎄

Advent of Code is an Advent calendar of small programming puzzles for a variety of skill levels that can be solved in any programming language you like.

Quite the explanation for Day 5 - there was a lot to read!

Part 1 ⭐️

I started by breaking down the problem into small functions: separating the input into rules and data, checking a rule for validity, and another to return the value of the middle page. Then I used Linq to combine them:

public string Part1()
{
    return _updates
        .Where(u => IsUpdateValid(u))
        .Sum(u => MiddlePageNumber(u))
        .ToString();
}

Part 2 ⭐️

Initially this seemed daunting, then I realised this looked like a bubble sort problem. The separate functions from Part 1 were resuable and I ended up with a correct answer pretty swiftly.

public string Part2()
{
    return _updates
        .Where(u => !IsUpdateValid(u))
        .Sum(u => MiddlePageNumber(FixUpdate(u)))
        .ToString();
}

Updated: