Advertisement
musifter

AoC 2024, day 2 (smalltalk)

Dec 2nd, 2024
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 1.05 KB | Source Code | 0 0
  1. #!/usr/local/bin/gst -q
  2.  
  3. Symbol extend [ value: arg  [^arg perform: self] ]
  4.  
  5. SequenceableCollection extend [
  6.     chain: binBlock [
  7.         | res |
  8.         res := self copyEmptyForCollect.
  9.         self fold: [:curr :next | res add: (binBlock value: curr value: next). next].
  10.         ^res
  11.     ]
  12.  
  13.     checkReport [
  14.         | diffs |
  15.         diffs := self chain: [:a :b | b - a].
  16.         ^diffs allSatisfy: [:d | (d abs between: 1 and: 3) and: [diffs first * d > 0]]
  17.     ]
  18. ]
  19.  
  20. "
  21. | Mainline
  22. "
  23. part1 := 0.
  24. part2 := 0.
  25. stdin linesDo: [ :line |
  26.     report := line subStrings collect: #asNumber.
  27.     (report checkReport)
  28.         ifTrue:  [ part1 := part1 + 1 ]
  29.         ifFalse: [
  30.             subLists := (1 to: report size) collect: [:i |
  31.                             (report copyReplaceFrom: i to: i withObject: nil) select: #notNil
  32.                         ].
  33.  
  34.             (subLists anySatisfy: [:list | list checkReport]) ifTrue: [part2 := part2 + 1]
  35.         ].
  36. ].
  37.  
  38. ('Part 1: %1' % {part1}) displayNl.
  39. ('Part 2: %1' % {part1 + part2}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement