4 votes

Day 4: Perilous Pier

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>

3 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())
    ))
    
    2 votes
    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.

      2 votes
  2. fidwell
    Link
    I liked this one; a fun problem statement to untangle. Solution (C#) I realized that you can pad any input with an arbitrary number of ?s without changing the answer, so for each line I shoved a...

    I liked this one; a fun problem statement to untangle.

    Solution (C#)

    I realized that you can pad any input with an arbitrary number of ?s without changing the answer, so for each line I shoved a bunch on the end so I wouldn't have to worry about overshooting the string length. Then, for each substring length, just ensure each character at that position is ? or uniquely one other character. Pretty much the same thing everyone else did, it seems.

    public class Puzzle04Solver : PuzzleSolver
    {
        // Padding / chunk size to test for.
        // If these are too low, your answer is wrong.
        // If these are too high, it takes longer for no benefit.
        // My magic number might not be ideal for your input.
        const int MagicMaxNumber1 = 14;
        const int MagicMaxNumber2 = 3690;
    
        public override object SolvePartOne(string input) =>
            input.SplitByNewline().Sum(b => InstructionsPerBarcode(b, MagicMaxNumber1));
    
        public override object SolvePartTwo(string input) =>
            InstructionsPerBarcode(input.Replace("\n", "").Replace("\r", "").Trim(), MagicMaxNumber2);
    
        private static int InstructionsPerBarcode(string input, int maxChunkSize, bool shouldPrint = false)
        {
            // Pad out every code with extra '?' for optimal chunking.
            var paddedInput = $"{input}{new string('?', maxChunkSize)}"[..maxChunkSize];
    
            for (var i = 1; i <= maxChunkSize; i++)
            {
                if (CanStringBeFulfilledWithXCharacters(paddedInput, i))
                {
                    if (shouldPrint)
                    {
                        Console.WriteLine($"{input} => {i}");
                    }
                    return i;
                }
            }
    
            throw new Exception($"No solution found for {input}");
        }
    
        private static bool CanStringBeFulfilledWithXCharacters(string input, int length)
        {
            var chunksOfX = input.ToCharArray().SplitArray(length)
                .Where(c => c.Count() == length)
                .Select(c => new string([.. c])).ToList();
            // Verify chars at all positions are the same (or '?') for each chunk.
            for (var i = 0; i < length; i++)
            {
                var charsAtIndex = chunksOfX.Select(c => c[i]);
                if (charsAtIndex.Distinct().Count(c => c != '?') > 1)
                {
                    return false;
                }
            }
    
            return true;
        }
    }
    
    1 vote