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
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/22323136
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465
You are viewing a single thread.
View all comments 1 point
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
].