1 vote

Day 2: Fulminating Farmland

Today's problem description: https://adventofcode.com/2026/day/2

Please post your solutions in your own top-level comment. Here's a template you can copy-paste into your comment to format it nicely, with the code collapsed by default inside an expandable section with syntax highlighting (you can replace python with any of the "short names" listed in this page of supported languages):

<details>
<summary>Part 1</summary>

```python
Your code here.
```

</details>

1 comment

  1. lily
    (edited )
    Link
    Took some time to clean this up a bit before posting it, though it could probably still stand to be made more succinct. I used the waypoint characters directly when solving this initially, but...

    Took some time to clean this up a bit before posting it, though it could probably still stand to be made more succinct. I used the waypoint characters directly when solving this initially, but that made part 2 especially kind of ugly. Instead, I now convert the waypoints to integers (from 0 to 5), which as a bonus allows part 1 to be done more efficiently. Part 2 is handled by keeping track of how many of each waypoint remain, and repeatedly selecting the most optimal waypoint that's still available.

    Solution (Lily)
    var program: List[Integer] = []
    
    var waypoints: Hash[Integer, Integer] = [
        'r' => 0, 'o' => 1, 'y' => 2, 'g' => 3, 'b' => 4, 'v' => 5
    ]
    
    for char in File.read_to_string("inputs/day_02.txt"): {
        # The `+ 0` coerces `char` to an `Integer` (`Byte`s can't be `Hash` keys).
        program.push(waypoints[char + 0])
    }
    
    var total_distance = 0
    var current_waypoint = 0
    
    var counts = [0, 0, 0, 0, 0, 0]
    
    for waypoint in program: {
        total_distance += (
            waypoint <= current_waypoint ? waypoint + 6 : waypoint
        ) - current_waypoint
    
        current_waypoint = waypoint
        counts[waypoint] += 1
    }
    
    print("Part 1: {}".format(total_distance))
    
    total_distance = 0
    current_waypoint = 0
    
    for _ in 1...program.size(): {
        for distance in 6...1 by -1: {
            var waypoint = current_waypoint - distance
            if waypoint < 0: {
                waypoint += 6
            }
    
            if counts[waypoint] > 0: {
                total_distance += distance
                current_waypoint = waypoint
                counts[waypoint] -= 1
                break
            }
        }
    }
    
    print("Part 2: {}".format(total_distance))
    
    1 vote