Advertisement
musifter

AoC 2022 day 6 (smalltalk)

Dec 6th, 2022 (edited)
2,687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 0.83 KB | Source Code | 0 0
  1. #!/usr/local/bin/gst -q
  2.  
  3. input := stdin nextLine.
  4.  
  5. #(4 14) keysAndValuesDo: [ :part :width |
  6.     window := 1 to: width.
  7.     mark := width.
  8.     seen := Set new.
  9.  
  10.     "
  11.     | Basic idea is that we're going to examine on an operating window
  12.     | from the far end, shrinking it as we go until either:
  13.     |   - it shrinks to nothing, meaning no matches and we're done
  14.     |   - we find a pair, in which case we jump the window forward to split them
  15.     "
  16.     [window notEmpty] whileTrue: [
  17.         (seen includes: (input at: window last)) ifTrue: [
  18.             window := window last + 1 to: (mark := window last + width).
  19.             seen empty.
  20.         ] ifFalse: [
  21.             seen add: (input at: window last).
  22.             window := window allButLast.
  23.         ]
  24.     ].
  25.  
  26.     ('Part %1: %2' % {part. mark}) displayNl.
  27. ]
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement