Advertisement
musifter

AoC 2021 day 5 (smalltalk)

Dec 5th, 2021
1,948
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/local/bin/gst -q
  2.  
  3. Collection extend [
  4.     apply: method  [ ^self collect: [:x | x perform: method] ]
  5.     incAt: idx     [ ^self at: idx put: ((self at: idx) + 1) ]
  6. ]
  7.  
  8. Set subclass: LineSet [
  9.     <shape: #pointer>
  10.     | ortho |
  11.  
  12.     LineSet class >> from: pt1 to: pt2 [
  13.         ^(super new) start: pt1 end: pt2
  14.     ]
  15.  
  16.     start: s end: e [
  17.         | pos end step |
  18.         pos   := s y * 1000 + s x + 1.
  19.         end   := e y * 1000 + e x + 1.
  20.         step  := (e y - s y) sign * 1000 + (e x - s x) sign.
  21.  
  22.         self add: pos.
  23.         [pos = end] whileFalse: [
  24.             pos := pos + step.
  25.             self add: pos.
  26.         ].
  27.  
  28.         ortho := (e x = s x) or: [e y = s y].
  29.  
  30.         ^self
  31.     ]
  32.  
  33.     ortho [ ^ortho ]
  34. ]
  35.  
  36. "
  37. | Mainline
  38. "
  39. ObjectMemory spaceGrowRate: 500.0.
  40.  
  41. " Make lines and partition them into orthogonal (part 1) and diagonal. "
  42. parts := stdin lines contents inject: {Set new. Set new} into: [ :acc :str |
  43.             | line |
  44.              nums := (str subStrings: ', ->') apply: #asNumber.
  45.  
  46.              line := LineSet from: (nums first @ nums second)
  47.                                to: (nums third @ nums fourth).
  48.  
  49.              (line ortho) ifTrue:  [ acc first  add: line ]
  50.                           ifFalse: [ acc second add: line ].
  51.              acc
  52.          ].
  53.  
  54. count := 0.
  55. list  := Array new: 1000000 withAll: 0.
  56.  
  57. #(1 2) do: [ :part |
  58.     (parts at: part) do: [ :line |
  59.         line do: [ :x | ((list incAt: x) = 2) ifTrue: [count := count + 1] ].
  60.     ].
  61.     ('Part %1: %2' % {part. count}) displayNl.
  62. ]
  63.                                                                                   62,2          Bo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement