Advertisement
musifter

AoC 2022, day 20 (Smalltalk, part 1)

Dec 20th, 2022
2,789
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 2.31 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.  
  7. Object subclass: RingNode [
  8.     | value next prev |
  9.     RingNode class >> new: val [ ^super new init: val ]
  10.  
  11.     init: val [
  12.         value := val.
  13.         ^prev := next := self
  14.     ]
  15.  
  16.     insertAfter: node [
  17.         next := node next.
  18.         prev := node.
  19.         node next prev: self.
  20.         node next: self.
  21.         ^self
  22.     ]
  23.  
  24.     removeSelf [
  25.         next prev: self prev.
  26.         prev next: self next.
  27.         ^self
  28.     ]
  29.  
  30.     at: dist [
  31.         | curr step count |
  32.         (dist = 0) ifTrue: [ ^self ].
  33.  
  34.         curr := self.
  35.         step := (dist > 0) ifTrue: [#next] ifFalse: [#false].
  36.  
  37.         count := dist abs.
  38.         [count > 0] whileTrue: [
  39.             curr  := curr perform: step.
  40.             count := count - 1.
  41.         ].
  42.         ^curr
  43.     ]
  44.  
  45.     prev        [ ^prev ]
  46.     prev: node  [ ^prev := node ]
  47.     next        [ ^next ]
  48.     next: node  [ ^next := node ]
  49.     value       [ ^value ]
  50. ]
  51.  
  52. Object subclass: Ring [
  53.     | head order zero size |
  54.     Ring class >> from: anArray [ ^super new init: anArray ]
  55.  
  56.     init: arr [
  57.         size  := arr size.
  58.         order := OrderedCollection new.
  59.         head  := order add: (RingNode new: arr first).
  60.  
  61.         arr allButFirst do: [ :val |
  62.             head := order add: ((RingNode new: val) insertAfter: head).
  63.             (val = 0) ifTrue: [ zero := head ].
  64.         ].
  65.         ^self
  66.     ]
  67.  
  68.     order   [ ^order ]
  69.     zero    [ ^zero  ]
  70.     size    [ ^size  ]
  71.  
  72.     printOn: aStream [
  73.         | curr |
  74.         curr := head.
  75.         [
  76.             aStream nextPutAll: ('%1, ' % {curr value}).
  77.             (curr := curr next) ~= head
  78.         ] whileTrue.
  79.     ]
  80. ]
  81.  
  82. "
  83. | Mainline
  84. "
  85. input := (stdin lines contents apply: #asNumber).
  86. ring  := Ring from: input.
  87.  
  88. ring order do: [ :curr |
  89.     dist := (curr value) \\ (ring size - 1).
  90.  
  91.     (dist ~= 0) ifTrue: [
  92.         dest := curr removeSelf.
  93.         dest := dest at: dist.
  94.         curr insertAfter: ((dist > 0) ifTrue:  [dest]
  95.                                       ifFalse: [dest prev]).
  96.     ]
  97. ].
  98.  
  99. part1 := 0.
  100. curr  := ring zero.
  101. 3 timesRepeat: [
  102.     curr  := curr at: 1000.
  103.     part1 := part1 + curr value.
  104. ].
  105.  
  106. ('Part 1: %1' % {part1}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement