2 votes

Day 3

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

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
    Link
    Probably this could be optimized further, but it runs fast enough that I don't think I'll bother. My initial solution was an even slower version of the same approach that took several seconds to...

    Probably this could be optimized further, but it runs fast enough that I don't think I'll bother. My initial solution was an even slower version of the same approach that took several seconds to run. I'm not doing anything clever here, just bruteforcing all the possible squares and counting the valid ones. There's almost certainly a more elegant way to do this, but I couldn't think of it for whatever reason.

    Solution (Lily)
    var map = File
        .read_to_string("inputs/day_03.txt")
        .split("\n")
        .map(|line| List.fill(line.size(), (|i| line[i] == '#')))
    
    var width = map[0].size()
    var height = map.size()
    
    var aligned_squares = 0
    var total_squares = 0
    
    for y, row in map: {
        var remaining_y = height - y
        var max_delta_y = remaining_y - 2
    
        for x, cell in row: {
            if !cell: {
                continue
            }
    
            var max_delta_x = width - x - 1
            for delta_y in 0...(x < max_delta_y ? x : max_delta_y): {
                var y2 = y + delta_y
                var x4 = x - delta_y
    
                for delta_x in 1...max_delta_x: {
                    if delta_x >= remaining_y - delta_y: {
                        break
                    }
    
                    var x2 = x + delta_x
                    if (
                        map[y2][x2]
                        && map[y2 + delta_x][x2 - delta_y]
                        && map[y + delta_x][x4]
                    ): {
                        if !delta_y: {
                            aligned_squares += 1
                        }
    
                        total_squares += 1
                    }
                }
            }
        }
    }
    
    print("Part 1: {}\nPart 2: {}".format(aligned_squares, total_squares))