If you’re here, there’s still hope for the internet

Don’t let it fall

  • 204 Posts
  • 2.45K Comments
Joined 3 years ago
cake
Cake day: October 12th, 2021

help-circle








  • Smalltalk

    I could have done it in 2 fns if I made them more generic, but couldn’t be bothered

    day4p1: input
        | lines sum w h|
        
        sum := ('XMAS' asRegex matchesIn: input) size. "forward"
        sum := sum + ('SAMX' asRegex matchesIn: input) size. "backwards sep cause overlapping"
        
        lines := input lines.
        h := lines size.
        w := (lines at: 1) size.
        
        1 to: h-3 do: [ :p1 |
            1 to: w do: [ :p2 |
                sum := sum + (self d4diag: lines p1: p1 p2: p2 dir: -1).
                sum := sum + (self d4diag: lines p1: p1 p2: p2 dir: 0).
                sum := sum + (self d4diag: lines p1: p1 p2: p2 dir: 1).    
            ]
        ].
        
        ^ sum.
    
    d4diag: input p1: p1 p2: p2 dir: dir
        | reverse xm ii |
        
        xm := 'XMAS'.
        
        reverse := ((input at: p1) at: p2) = $S.
        
        0 to: 3 do: [ :i |
            ii := reverse ifTrue: [ 4 - i ] ifFalse: [ i + 1 ].
                                                        "if out of bounds, obv not possible"
            ((xm at: ii) = ((input at: p1 + i) at: i * dir + p2 ifAbsent: [^ 0])) ifFalse: [^ 0]
        ].
    
        ^ 1.
    

    Part 2

    day4p2: input
        | lines sum w h pos |
        "Find all diag mas, then check of 1 . -1 (we can look back on every -1"
        
        sum := 0.
        lines := input lines.
        h := lines size.
        w := (lines at: 1) size.
        
        1 to: h-2 do: [ :p1 |
            pos := Array new: w withAll: false.
            1 to: w do: [ :p2 |
                (self d42: lines p1: p1 p2: p2 dir: -1)
                    ifTrue: [
                        sum := sum + ((pos at: p2-2) ifTrue:[1] ifFalse:[0]).
                    ].
                
                (self d42: lines p1: p1 p2: p2 dir: 1) ifTrue: [pos at: p2 put: true].
            ]
        ].
        
        ^ sum.
    
    d42: input p1: p1 p2: p2 dir: dir
        | reverse xm ii |
        
        xm := 'MAS'.
        
        reverse := ((input at: p1) at: p2) = $S.
        
        0 to: 2 do: [ :i |
            ii := reverse ifTrue: [ 3 - i ] ifFalse: [ i + 1 ].
                                                        "if out of bounds, obv not possible"
            ((xm at: ii) = ((input at: p1 + i) at: i * dir + p2 ifAbsent: [^ false])) ifFalse: [^ false]
        ].
    
        ^ true.
    


  • Smalltalk

    I wrote matchesActual cause all of smalltalk’sstupid matchesDo: or whatever don’t give you the actual match with captures, only substrings (that wasted a good 40 minutes).

    Also smalltalk really needs an index operator

    day3p1: input
      | reg sum |
        reg := 'mul\((\d\d?\d?),(\d\d?\d?)\)' asRegex.
        sum := 0.
        
        reg matchesActual: input do: [ :m | " + sum at end cause operator precedence"
            sum := (m subexpression: 2) asInteger * (m subexpression: 3) asInteger + sum 
        ].
        
        ^ sum.
    
    day3p2: input
      | reg sum do val |
    
        reg := 'do(\:?n''t)?\(\)|mul\((\d{1,3}),(\d{1,3})\)' asRegex.
        sum := 0.
        do := true.
        reg matchesActual: input do: [ :m |
            val := m subexpression: 1.
            (val at: 1) = $d ifTrue: [ do := (val size < 5) ]
            ifFalse: [ 
                do ifTrue: [ 
                    sum := (m subexpression: 2) asInteger * (m subexpression: 3) asInteger + sum.
            ].  ].
        ].
        
        ^ sum.
    





  • Smalltalk

    Discovered a number of frustrations with this supposedly small and elegant language

    1. Smalltalk’s block based iteration has NO control flow
    2. blocks are very dissimilar to functions
    3. You cannot early return from blocks (leading to a lot of horrible nested ifs or boolean operations)
    4. Smalltalk’s messages (~functions) cannot take multiple arguments, instead it has these sort of combined messages, so instead of a function with three arguments, you would send 3 combined messages with one argument. This is fine until you try to chain messages with arguments, as smalltalk will interpret them as a combined message and fail, forcing you to either break into lots of temp variables, or do lisp-like parenthesis nesting, both of which I hate
    5. Smalltalk’s order of operations, while nice and simple, is also quite frustrating at times, similar to #4, forcing you to break into lots of temp variables, or do lisp-like parenthesis nesting. For instance (nums at: i) - (nums at: i+1) which would be nums[i] - nums[i+1] in most languages

    Part 1

    day2p1: input
        ^ (input lines 
            collect: [ :l | l substrings collect: [ :s | s asInteger ]])
            
            count: [ :nums |
                (nums = nums sorted or: nums = nums sorted reverse) 
                    and: [
                        (1 to: nums size-1) allSatisfy: [ :i | 
                            ((nums at: i) - (nums at: i+1)) abs between: 1 and: 3
            ]    ]    ]
    

    Part 2

    day2p2: input    
        | temp |
        
        ^ (input lines 
            collect: [ :l | (l substrings collect: [ :s | s asInteger ]) asOrderedCollection ])
             
            count: [ :nums |
                
                (self day2p2helper: nums)
                or: [ 
                    ((1 to: nums size) anySatisfy: [ :i |
                        temp := nums copy.
                        temp removeAt: i.
                        self day2p2helper: temp
                    ])
                
                
                or: [(self day2p2helper: nums reversed)
                or: [
                    (1 to: nums size) anySatisfy: [ :i |
                        temp := nums reversed.
                        temp removeAt: i.
                        self day2p2helper: temp
                    ]
                ]]] .
            ]
    
    day2p2helper: nums
        ^ (1 to: nums size - 1) allSatisfy: [ :i | 
            ((nums at: i+1) - (nums at: i)) between: 1 and: 3    
        ].