this post was submitted on 02 Dec 2024
41 points (97.7% liked)

Advent Of Code

885 readers
154 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 18 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 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

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