1 vote

Day 5

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

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. lily
    Link
    A lot of the work for this one was just parsing, but luckily it wasn't too bad. I took some time to clean my solution up and make it run a bit faster (I originally converted values to and from...

    A lot of the work for this one was just parsing, but luckily it wasn't too bad. I took some time to clean my solution up and make it run a bit faster (I originally converted values to and from strings for some operations, which added quite a bit of overhead), and while it could probably still be cleaned up somewhat I'm happy enough with it now. I initially solved Part 1 with three nested loops, but since Part 2 needs BFS anyway both can be solved with a single traversal.

    Solution (Lily)
    define octal(value: Integer): Integer {
        if value >= 4096: {
            return value
        }
    
        var new_value = 0
        var multiplier = 1
    
        do: {
            var digit = value % 8
            value /= 8
            new_value += digit * multiplier
            multiplier *= 10
        } while value
    
        return new_value
    }
    
    define backspace(value: Integer): Integer {
        return value / 10
    }
    
    define parse_button(string: String): Function(Integer => Integer) {
        if string == "[oct]": {
            return octal
        }
    
        if string == "[bksp]": {
            return backspace
        }
    
        var second_char = string[1]
    
        if second_char == '*': {
            var multiplier = string[2] - '0'
            return (|value|
                var new_value = value * multiplier
    
                if new_value >= 10000: {
                    return value
                }
    
                new_value
            )
        }
    
        if second_char == '/': {
            var divisor = string[2] - '0'
            return (|value| value / divisor)
        }
    
        if string.size() == 6: {
            var old = string[1] - '0'
            var new = string[4] - '0'
    
            return (|value|
                var new_value = 0
                var multiplier = 1
    
                do: {
                    var digit = value % 10
                    value /= 10
    
                    if digit == old: {
                        digit = new
                    }
    
                    new_value += digit * multiplier
                    multiplier *= 10
                } while value
    
                new_value
            )
        }
    
        var addend = string[1] - '0'
        return (|value| (value >= 1000 ? value : value * 10 + addend))
    }
    
    var buttons = File
        .read_to_string("inputs/day_05.txt")
        .split(" ")
        .map(parse_button)
    
    var queue = [<[0, 0]>]
    var seen: Hash[Integer, Unit] = []
    
    var sum_max_3 = 0
    var sum_unlimited = 0
    
    while queue: {
        var node = queue.shift()
        var value = node[0]
        var depth = node[1]
    
        for button in buttons: {
            var new_value = button(value)
            if new_value == value || seen.has_key(new_value): {
                continue
            }
    
            queue.push(<[new_value, depth + 1]>)
            seen[new_value] = unit
    
            if depth < 3: {
                sum_max_3 += new_value
            }
    
            sum_unlimited += new_value
        }
    }
    
    print("Part 1: {}\nPart 2: {}".format(sum_max_3, sum_unlimited))
    
  2. Boojum
    Link
    The hardest part of this was just parsing the input into a set of lambdas. Python's lambda capture rules always trip me up. The rest of a straightforward Cartesian product for Part 1 and a BFS for...

    The hardest part of this was just parsing the input into a set of lambdas. Python's lambda capture rules always trip me up. The rest of a straightforward Cartesian product for Part 1 and a BFS for Part 2.

    Parts 1 and 2
    import itertools
    
    b = { ( lambda v, i = int( w[ 1 : ] ): v * i )    if '*' in w else
          ( lambda v, i = int( w[ 1 : ] ): v // i )   if '/' in w else
          ( lambda v, i = p[ 0 ], j = p[ 1 ]: int( str( v ).replace( i, j ) ) )
                                                      if len( p := w.split( "->" ) ) == 2 else
          ( lambda v: int( oct( v )[ 2 : ] ) )        if w == "oct" else
          ( lambda v: v // 10 )                       if w == "bksp" else
          ( lambda v, i = int( w ): v * 10 + i )      # [d]
          for w in open(0).read().replace( '[', '' ).replace( ']', '' ).split() }
    
    print( sum( { k( j( i( 0 ) ) )
                  for i, j, k in itertools.product( b, repeat = 3 ) } ) )
    
    v, q = set(), [ 0 ]
    while q:
        p = q.pop()
        for i in b:
            n = i( p )
            if n <= 9999 and n not in v:
                v.add( n )
                q.append( n )
    print( sum( v ) )
    

    Visualization