0 votes

Day 6

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

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
    This was the hardest puzzle yet, by quite a large margin. Part 1 was easy enough on its own, but my initial solution for it proved to be entirely useless for Part 2, so I had to start over from...

    This was the hardest puzzle yet, by quite a large margin. Part 1 was easy enough on its own, but my initial solution for it proved to be entirely useless for Part 2, so I had to start over from scratch. I now start by building the entire grid at an arbitrary rotation, which makes Part 1 trivial, and then use the first two anchor points (the third isn't necessary) to determine the grid's rotation and translate the coordinates of the target light ID for Part 2. My code here is still pretty messy, but I've already spent a while cleaning it up and would rather not work on it any further. It seems to be decently efficient, at least.

    Solution (Lily)
    import math
    
    var lights: Hash[Integer, List[Integer]] = []
    
    for line in File.read_to_string("inputs/day_06.txt").split("\n"): {
        var parts = line.split("-")
        lights[parts[0].parse_i().unwrap()] = parts[1]
            .split(",")
            .map(String.parse_i)
            .map(Option.unwrap)
    }
    
    lights.each_pair(|id, connections|
        for other_id in connections: {
            with lights.get(other_id) as Some(other_connections): {
                if !other_connections.contains(id): {
                    other_connections.push(id)
                }
            else:
                lights[other_id] = [id]
            }
        }
    )
    
    var size = math.sqrt(lights.size().to_d()).to_i()
    var grid = List.fill(size, (|_| List.repeat(size, -1)))
    
    var corners: List[Integer] = []
    lights.each_pair(|id, connections|
        if connections.size() == 2: {
            corners.push(id)
        }
    )
    
    var current_id = corners[0]
    var row = grid[0]
    
    for i in 1...3: {
        var target_id = corners[i]
    
        var queue = [[current_id]]
        var seen: Hash[Integer, Unit] = []
    
        var found_adjacent_corner = false
        while queue: {
            var path = queue.shift()
            var id = path[-1]
    
            if id == target_id: {
                if path.size() == size: {
                    for x, other_id in path: {
                        row[x] = other_id
                    }
    
                    found_adjacent_corner = true
                }
    
                break
            }
    
            if seen.has_key(id): {
                continue
            }
    
            for other_id in lights[id]: {
                queue.push(path.slice().push(other_id))
            }
    
            seen[id] = unit
        }
    
        if found_adjacent_corner: {
            break
        }
    }
    
    var previous_row = row, previous_previous_row = row
    for y in 1...size - 1: {
        previous_previous_row = previous_row
        previous_row = row
        row = grid[y]
    
        var right_id = previous_row[1]
        var upper_id = previous_previous_row[0]
    
        for other_id in lights[previous_row[0]]: {
            # Since `previous_previous_row == previous_row` for `y == 1`, the check
            # against `upper_id` is irrelevant in that case. It's faster to perform
            # it anyway than conditionally disable it, though, and doing so doesn't
            # cause any problems.
            if other_id != right_id && other_id != upper_id: {
                current_id = other_id
                break
            }
        }
    
        for x in 0...size - 2: {
            row[x] = current_id
    
            upper_id = previous_row[x]
            var upper_right_id = previous_row[x + 1]
    
            for other_id in lights[current_id]: {
                if other_id != upper_id && lights[other_id].contains(
                    upper_right_id
                ): {
                    current_id = other_id
                    break
                }
            }
        }
    
        row[size - 1] = current_id
    }
    
    var center = size / 2
    print("Part 1: {}".format(grid[center][center]))
    
    constant IDEAL_X1 = 47, IDEAL_Y1 = 30
    constant IDEAL_X2 = 29, IDEAL_Y2 = 59
    constant TARGET_ID = 2026
    
    var x1 = -1, y1 = -1, x2 = -1, y2 = -1, x3 = -1, y3 = -1
    for y in 0...size - 1: {
        row = grid[y]
        for x in 0...size - 1: {
            var id = row[x]
    
            if id == 1: {
                x1 = x
                y1 = y
                break
            }
    
            if id == 2: {
                x2 = x
                y2 = y
                break
            }
    
            if id == TARGET_ID: {
                x3 = x
                y3 = y
                break
            }
        }
    }
    
    var ideal_delta_x_12 = IDEAL_X2 - IDEAL_X1
    var ideal_delta_y_12 = IDEAL_Y2 - IDEAL_Y1
    
    var delta_x_12 = x2 - x1
    var delta_y_12 = y2 - y1
    
    var delta_x_13 = -1, delta_y_13 = -1
    if delta_x_12 == ideal_delta_x_12 && delta_y_12 == ideal_delta_y_12: {
        # No rotation.
        delta_x_13 = x3 - x1
        delta_y_13 = y3 - y1
    elif delta_x_12 == ideal_delta_y_12 && delta_y_12 == -ideal_delta_x_12:
        # 90 degrees.
        delta_x_13 = y1 - y3
        delta_y_13 = x3 - x1
    elif delta_x_12 == -ideal_delta_x_12 && delta_y_12 == -ideal_delta_y_12:
        # 180 degrees.
        delta_x_13 = x1 - x3
        delta_y_13 = y1 - y3
    elif delta_x_12 == -ideal_delta_y_12 && delta_y_12 == ideal_delta_x_12:
        # 270 degrees.
        delta_x_13 = y3 - y1
        delta_y_13 = x1 - x3
    }
    
    print("Part 2: {},{}".format(IDEAL_X1 + delta_x_13, IDEAL_Y1 + delta_y_13))