Advertisement
musifter

AoC 2023 day 15 (Smalltalk)

Dec 15th, 2023 (edited)
1,508
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.  
  5. String extend [
  6.     reindeerHash [ ^self inject: 0 into: [:a :b | ((a + b asciiValue) * 17) \\ 256] ]
  7. ]
  8.  
  9. "
  10. | Mainline
  11. "
  12. input := stdin nextLine tokenize: ','.
  13.  
  14. part1 := 0.
  15. boxes := (1 to: 256) collect: [:c | OrderedCollection new].
  16.  
  17. input do: [ :str |
  18.     step  := (str =~ '(\w+)([=-])(\d?)').
  19.     label := (step at: 1) asSymbol.
  20.     oper  := (step at: 2) first.
  21.  
  22.     part1 := part1 + str reindeerHash.
  23.     box   := label reindeerHash + 1.
  24.     found := (boxes at: box) detect: [:l | l first = label] ifNone: [nil].
  25.  
  26.     (oper = $-) ifTrue: [
  27.         found ifNotNil: [ (boxes at: box) remove: found ].
  28.     ] ifFalse: [    " oper '=' "
  29.         focus := (step at: 3) asInteger.
  30.         found ifNil:    [ (boxes at: box) add: {label. focus} ]
  31.               ifNotNil: [ found at: 2 put: focus ].
  32.     ]
  33. ].
  34.  
  35. part2 := 0.
  36. boxes keysAndValuesDo: [:i :box |
  37.     box keysAndValuesDo: [:j :lens |
  38.         part2 := part2 + (i * j * lens second).
  39.     ].
  40. ].
  41.  
  42. ('Part 1: %1' % {part1}) displayNl.
  43. ('Part 2: %1' % {part2}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement