this post was submitted on 04 Dec 2024
18 points (95.0% liked)

Advent Of Code

985 readers
31 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2024

Solution Threads

M T W T F S S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS
 

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

you are viewing a single comment's thread
view the rest of the comments
[โ€“] [email protected] 1 points 3 weeks 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.