Advertisement
musifter

AoC 2023 day 9 (Smalltalk)

Dec 9th, 2023 (edited)
1,295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 0.92 KB | Source Code | 0 0
  1. #!/usr/local/bin/gst -q
  2.  
  3. Symbol extend [ value: arg  [^arg perform: self] ]
  4.  
  5. SequenceableCollection extend [
  6.     " Returns collection of results from calling binBlock for each link pair "
  7.     chain: binBlock [
  8.         | res |
  9.         res := self copyEmptyForCollect.
  10.         self fold: [:curr :next | res add: (binBlock value: curr value: next). next].
  11.         ^res
  12.     ]
  13. ]
  14.  
  15. "
  16. | Mainline
  17. "
  18. input := stdin lines contents collect: [:line | line subStrings collect: #asNumber].
  19.  
  20. part1 := 0.
  21. part2 := 0.
  22. input do: [:seq |
  23.     row  := seq.
  24.     mult := 1.
  25.  
  26.     [row conform: [:n | n = 0]] whileFalse: [
  27.         " Part 1 is sum of lasts, part 2 is alternating sum of firsts "
  28.         part1 := part1 + row last.
  29.         part2 := part2 + (mult * row first).
  30.         mult  := mult negated.
  31.  
  32.         row := row chain: [:a :b | b - a].
  33.     ].
  34. ].
  35.  
  36.  
  37. ('Part 1: %1' % {part1}) displayNl.
  38. ('Part 2: %1' % {part2}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement