Smalltalk
Discovered a number of frustrations with this supposedly small and elegant language
- Smalltalk's block based iteration has NO control flow
- blocks are very dissimilar to functions
- You cannot early return from blocks (leading to a lot of horrible nested ifs or boolean operations)
- 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
- 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 benums[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
].