Day 4: Ceres Search

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

  • morrowind
    link
    fedilink
    arrow-up
    1
    ·
    6 days ago

    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.