Advertisement
musifter

AoC 2024, day 7, part 2 (smalltalk)

Dec 7th, 2024 (edited)
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 1.11 KB | Source Code | 0 0
  1. #!/usr/local/bin/gst -q
  2.  
  3. Symbol     extend [ value: arg  [^arg perform: self] ]
  4. Collection extend [ sum  [^self inject: 0 into: [:a :b | a + b]] ]
  5.  
  6. " Extending Booleans to do multiplication "
  7. True  extend [ * num [^num] ]
  8. False extend [ * num [^0]   ]
  9.  
  10. Integer extend [
  11.     " Concatenation operation for integers "
  12.     || arg [ ^self * (10 raisedToInteger: (arg + 1) log ceiling) + arg ]
  13.  
  14.     " Test if self can be made from list using +, *, || "
  15.     ? list [ ^(self recurse: list first with: list allButFirst) * self ]
  16.  
  17.     recurse: acc with: list [
  18.         | next nlist |
  19.         (list isEmpty or: [acc > self]) ifTrue: [^self = acc].
  20.  
  21.         next  := list first.
  22.         nlist := list allButFirst.
  23.         ^(self recurse: acc + next with: nlist)
  24.             or: [(self recurse: acc * next with: nlist)
  25.                 or: [self recurse: acc || next with: nlist]]. " remove for part 1 "
  26.     ]
  27. ]
  28.  
  29. "
  30. | Mainline
  31. "
  32. input := stdin lines contents collect: [:line | line subStrings collect: #asNumber].
  33.  
  34. part2 := (input collect: [:list | list removeFirst ? list]) sum.
  35. ('Part 2: %1' % {part2}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement