Day 2: Red-Nosed Reports

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://blocks.programming.dev/ if you prefer sending it through a URL

FAQ

  • morrowind@lemmy.ml
    link
    fedilink
    arrow-up
    1
    ·
    23 days ago

    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    
        ].