Advertisement
musifter

AoC 2022 day 5 (smalltalk)

Dec 5th, 2022
3,345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 2.91 KB | Source Code | 0 0
  1. #!/usr/local/bin/gst -q
  2.  
  3. "
  4. |  Extensions to standard classes
  5. "
  6. Collection extend [
  7.     apply: method  [ ^self collect: [:x | x perform: method] ]
  8. ]
  9.  
  10. OrderedCollection extend [
  11.     " XXX: Quick implementation of multiple remove from front "
  12.     removeFirst: num [
  13.         | res |
  14.         res := self first: num.
  15.         num timesRepeat: [ self removeFirst ].
  16.         ^res
  17.     ]
  18. ]
  19.  
  20. "
  21. | Class descriptions for cranes
  22. |
  23. | CrateMover9001 picks up crates all at once.
  24. | CrateMover9000 picks up one at a time, so they get reversed.
  25. |
  26. "
  27. Object subclass: CrateMover9001 [
  28.     | stacks stackOrder |
  29.     CrateMover9001 class >> new: stackText [
  30.         ^super new init: stackText
  31.     ]
  32.  
  33.     init: input [
  34.         | stackPos |
  35.  
  36.         " Find stack pos in input: "
  37.         stackPos   := Dictionary new.         " local for mapping pos in input "
  38.         stacks     := Dictionary new.         " map of name -> stack "
  39.         stackOrder := OrderedCollection new.  " stacks in the order of the input "
  40.  
  41.         " Using last line as key, build maps and create stacks "
  42.         (input lines last) keysAndValuesDo: [ :pos :name |
  43.             (name ~= Character space) ifTrue: [
  44.                 stackPos at: pos put: name.
  45.                 stackOrder add: (stacks at: name put: OrderedCollection new)
  46.             ]
  47.         ].
  48.  
  49.         " Build stacks: "
  50.         (input lines allButLast) do: [ :line |
  51.             stackPos keysAndValuesDo: [ :pos :name |
  52.                | crate |
  53.                 ((crate := line at: pos) isLetter) ifTrue: [
  54.                     (stacks at: name) add: crate
  55.                 ]
  56.             ]
  57.         ].
  58.         ^self
  59.     ]
  60.  
  61.     pickup: num from: src [
  62.         ^(stacks at: src) removeFirst: num
  63.     ]
  64.  
  65.     move: num from: src to: dst [
  66.         (stacks at: dst) addAllFirst: (self pickup: num from: src)
  67.     ]
  68.  
  69.     stackTops [
  70.         ^String from: (stackOrder apply: #first)
  71.     ]
  72. ]
  73.  
  74. " Making 9000 the subclass, because it needs the extra work of reversing "
  75. CrateMover9001 subclass: CrateMover9000 [
  76.     CrateMover9000 class >> new: stackText [
  77.         ^super new: stackText
  78.     ]
  79.  
  80.     pickup: num from: src [
  81.         ^(super pickup: num from: src) reverse
  82.     ]
  83. ]
  84.  
  85. "
  86. | Mainline
  87. "
  88. section := stdin contents tokenize: '\n\n'.
  89.  
  90. " First section is initial crate stacks, Init some cranes on them: "
  91. cranes := {CrateMover9000 new: section first. CrateMover9001 new: section first}.
  92.  
  93. " Second section is the crane operations: "
  94. (section second lines) do: [ :cmd |
  95.    | match |
  96.     match := (cmd =~ 'move (\d+) from (.) to (.)') asArray.
  97.  
  98.     cranes do: [ :crane |
  99.         crane move: (match first)  asInteger
  100.               from: (match second) first        " only want first character "
  101.                 to: (match third)  first        " only want first character "
  102.     ]
  103. ].
  104.  
  105. cranes keysAndValuesDo: [ :i :crane |
  106.     ('Part %1: %2' % {i. crane stackTops}) displayNl
  107. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement