Advertisement
musifter

AoC 2022, day 3 (smalltalk)

Dec 3rd, 2022 (edited)
2,423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 1.12 KB | Source Code | 0 0
  1. #!/usr/local/bin/gst -q
  2.  
  3. Character extend [
  4.     " Priority score for letters, a-z (1-26), A-Z (27-52).  XXX: No safety. "
  5.     priority [
  6.         ^self isLowercase ifTrue:  [ self asInteger - 96 ]
  7.                           ifFalse: [ self asInteger - 38 ]  " - 64 + 26 "
  8.     ]
  9. ]
  10.  
  11. Set extend [
  12.     " Return element if there's only one, else return nil. "
  13.     onlyOne [ ^(self size = 1) ifTrue: [self anyOne] ifFalse: [nil] ]
  14. ]
  15.  
  16. "
  17. | Mainline
  18. "
  19. part1 := 0.
  20. part2 := 0.
  21.  
  22. stdin contents lines keysAndValuesDo: [ :num :sack |
  23.     " Part 1 "
  24.     comp1 := Set from: (sack first: sack size / 2).
  25.     comp2 := Set from: (sack  last: sack size / 2).
  26.  
  27.     part1 := part1 + (comp1 & comp2) onlyOne priority.
  28.  
  29.     " Part 2 state machine "
  30.     state := num \\ 3.
  31.     (state = 1) ifTrue: [
  32.         " start new group "
  33.         badge := sack asSet.
  34.     ] ifFalse: [
  35.         " add to group, score badge every third "
  36.         badge := badge & sack asSet.
  37.         (state = 0) ifTrue: [
  38.             part2 := part2 + badge onlyOne priority
  39.         ]
  40.     ]
  41. ].
  42.  
  43. ('Part 1: %1' % {part1}) displayNl.
  44. ('Part 2: %1' % {part2}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement