3 votes

Day 4

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

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>

2 comments

  1. [2]
    lily
    Link
    This was a fun one (albeit easier than yesterday's, I think)! Part 2, though, confused me a little bit... AoC has done these kinds of puzzles a bunch in the past, where Part 2 does nothing but...

    This was a fun one (albeit easier than yesterday's, I think)! Part 2, though, confused me a little bit... AoC has done these kinds of puzzles a bunch in the past, where Part 2 does nothing but increase the input size to prevent brute forcing, but my solution for Part 1 already didn't brute force, and was able to solve Part 2 instantly. So I'm assuming there's a naive solution I lucked into not finding, maybe? I also found it strange that the puzzle description made a point of describing how instructions could be one of six colors, since my solution doesn't care about that at all.

    Solution (Lily)
    define get_instruction_count(barcode: String): Integer {
        var size = barcode.size()
        for length in 1...size - 1: {
            var invalid = false
            for i in 0...length - 1: {
                var instruction = '?'
                do: {
                    var new_instruction = barcode[i]
                    if new_instruction != '?': {
                        if instruction != '?' && new_instruction != instruction: {
                            invalid = true
                            break
                        }
    
                        instruction = new_instruction
                    }
    
                    i += length
                } while i < size
    
                if invalid: {
                    break
                }
            }
    
            if !invalid: {
                return length
            }
        }
    
        return size
    }
    
    var barcodes = File.read_to_string("inputs/day_04.txt").split("\n")
    print("Part 1: {}\nPart 2: {}".format(
        barcodes.fold(0, (|sum, barcode| sum + get_instruction_count(barcode))),
        get_instruction_count(barcodes.join())
    ))
    
    1 vote
    1. fidwell
      Link Parent
      just a classic AoC red herring. it wasn't relevant to my solution either.

      I also found it strange that the puzzle description made a point of describing how instructions could be one of six colors, since my solution doesn't care about that at all.

      just a classic AoC red herring. it wasn't relevant to my solution either.