Advertisement
musifter

AoC 2023 day 05 part 1 (Smalltalk)

Dec 5th, 2023 (edited)
1,455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 1.44 KB | Source Code | 0 0
  1. #!/usr/local/bin/gst -q
  2.  
  3. Symbol extend     [ value: arg  [^arg perform: self]             ]
  4. Collection extend [ min         [^self fold: [:a :b | a min: b]] ]
  5.  
  6. Interval extend [
  7.     " Get intersection of self and another Interval "
  8.     & other [ ^(self first max: other first) to: (self last min: other last) ]
  9. ]
  10.  
  11. Interval subclass: Mapping [
  12.     | dest |
  13.     Mapping class >> new: str [
  14.         | nums |
  15.         nums := str subStrings collect: #asNumber.
  16.         ^(super from: nums second to: nums second + nums third - 1) init: nums first.
  17.     ]
  18.  
  19.     init: d [ dest := d.  ^self ]
  20.     dest    [ ^dest ]
  21. ]
  22.  
  23. "
  24. | Mainline
  25. "
  26. sections := (stdin contents tokenize: '\n\n') collect: #lines.
  27.  
  28. seeds := sections first first subStrings allButFirst collect: #asNumber.
  29.  
  30. " Read in mappings.  ASSUME: sections are in order "
  31. map := OrderedCollection new.
  32. sections allButFirst do: [ :lines |
  33.     typeMap := lines allButFirst asOrderedCollection collect: [:str | Mapping new: str].
  34.     typeMap addLast: (Mapping new: '0 0 4294967296').  " add identity map as last default"
  35.  
  36.     map add: typeMap.
  37. ].
  38.  
  39. locations := seeds collect: [:seed |
  40.                  map inject: seed into: [:loc :rules |
  41.                     | trans |
  42.                      trans := rules detect: [:r | loc between: r first and: r last].
  43.                      trans dest + (loc - trans first).
  44.                  ]
  45.              ].
  46.  
  47. ('Part 1: %1' % {locations min}) displayNl.
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement