Advertisement
musifter

AoC 2022, day 18 (smalltalk)

Dec 18th, 2022
2,475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 1.71 KB | Source Code | 0 0
  1. #!/usr/local/bin/gst -q
  2.  
  3. Collection extend [
  4.     apply: method  [ ^self collect: [:x | x perform: method] ]
  5.  
  6.     min  [ ^self fold: [ :a :b | a min: b ] ]
  7.     max  [ ^self fold: [ :a :b | a max: b ] ]
  8. ]
  9.  
  10. Array extend [
  11.     + arr [ ^self with: arr collect: [:a :b | a + b] ]
  12. ]
  13.  
  14. "
  15. | Mainline
  16. "
  17. dirs := #( (1 0 0) (-1 0 0) (0 1 0) (0 -1 0) (0 0 1) (0 0 -1) ).
  18.  
  19. droplet := Set new.
  20. surface := 0.
  21. minimum := SmallInteger largest.
  22. maximum := SmallInteger smallest.
  23.  
  24. stdin lines contents do: [:line |
  25.     cell := (line substrings: $,) asArray apply: #asNumber.
  26.     droplet add: cell.
  27.  
  28.     surface := surface + 6 - (2 * ((dirs collect: [:d | cell + d])
  29.                                            count: [:n | droplet includes: n])).
  30.  
  31.     minimum := minimum min: cell min.
  32.     maximum := maximum max: cell max.
  33. ].
  34.  
  35. ('Part 1: %1' % {surface}) displayNl.
  36.  
  37. " Expand bounding cube: "
  38. minimum := minimum - 1.
  39. maximum := maximum + 1.
  40.  
  41. " BFS the encasing cube "
  42. encase := Set new.
  43. outer  := 0.
  44. queue  := OrderedCollection with: {minimum. minimum. minimum}.
  45.  
  46. [ queue notEmpty ] whileTrue: [
  47.     cell := queue removeFirst.
  48.     (outer \\ 100 = 0) ifTrue: [
  49.         stderr nextPutAll: ('Outer: %1' % {outer}); cr; flush
  50.     ].
  51.  
  52.     (encase includes: cell) ifFalse: [
  53.         encase add: cell.
  54.         (dirs collect: [:d | cell + d]) do: [:neigh |
  55.             (droplet includes: neigh) ifTrue: [
  56.                 outer := outer + 1.  " ran into droplet, count this face "
  57.             ] ifFalse: [
  58.                 (neigh conform: [:n | n between: minimum and: maximum]) ifTrue: [
  59.                     queue add: neigh
  60.                 ]
  61.             ]
  62.         ]
  63.     ]
  64. ].
  65.  
  66. ('Part 2: %1' % {outer}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement