Advertisement
musifter

AoC 2024, day 22 (smalltalk)

Dec 22nd, 2024 (edited)
26
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. "
  4. | Mainline
  5. "
  6. part1 := 0.
  7. count := 0.
  8. hash  := Array new: 160000 withAll: 0.
  9. tmp   := Dictionary new.
  10.  
  11. stdin lines contents do: [:line |
  12.    | num last idx |
  13.  
  14.     num  := line asNumber.
  15.     last := num \\ 10.
  16.     idx  := 0.
  17.     tmp empty.
  18.  
  19.     ((count := count + 1) \\ 50 == 0) ifTrue: [
  20.         stderr nextPutAll: ('[%1] %2   ') % {count. num}; cr; flush.
  21.     ].
  22.  
  23.     (1 to: 2000) do: [:i |
  24.         num := ((num bitShift:  6) bitXor: num) bitAnd: 16rFFFFFF.
  25.         num :=  (num bitShift: -5) bitXor: num.
  26.         num := ((num bitShift: 11) bitXor: num) bitAnd: 16rFFFFFF.
  27.  
  28.         idx  := ((idx * 20) + (num \\ 10) - last + 10) \\ 160000.
  29.         last := num \\ 10.
  30.         (i >= 4) ifTrue: [
  31.             tmp at: idx ifAbsentPut: [last].
  32.         ]
  33.     ].
  34.  
  35.     part1 := part1 + num.
  36.     tmp keysAndValuesDo: [:idx :bananas |
  37.         hash at: idx put: (hash at: idx ifAbsent: [0]) + bananas.
  38.     ]
  39.  ].
  40.  
  41. stdout nl.
  42. ('Part 1: %1' % {part1}) displayNl.
  43. ('Part 2: %1' % {hash fold: [:a :b | a max: b]}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement