Advertisement
musifter

AoC 2022, day 4 (gnu smalltalk)

Dec 4th, 2022 (edited)
2,044
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 1.06 KB | Source Code | 0 0
  1. #!/usr/local/bin/gst -q
  2.  
  3. Collection extend [
  4.     " To make up for GNU smalltalk not taking symbols with collect: "
  5.     apply: method [ ^self collect: [:x | x perform: method] ]
  6. ]
  7.  
  8. Interval extend [
  9.     " Get intersection of self and another Interval "
  10.     & other [ ^(self first max: other first) to: (self last min: other last) ]
  11. ]
  12.  
  13. String extend [
  14.     " Interpret (\d+)-(\d+) string as an Interval "
  15.     asInterval [
  16.         | range |
  17.         range := (self subStrings: '-') apply: #asInteger.
  18.         ^Interval from: range first to: range second.
  19.     ]
  20. ]
  21.  
  22. "
  23. | Mainline
  24. "
  25. part1 := 0.
  26. part2 := 0.
  27.  
  28. stdin linesDo: [ :line |
  29.     ranges := (line substrings: $,) apply: #asInterval.
  30.  
  31.     intersection := ranges first & ranges second.
  32.     (intersection notEmpty) ifTrue: [
  33.         part2 := part2 + 1.
  34.  
  35.         " intersection matches a given range making it a subset "
  36.         (ranges includes: intersection) ifTrue: [
  37.             part1 := part1 + 1.
  38.         ].
  39.     ].
  40. ].
  41.  
  42. ('Part 1: %1' % {part1}) displayNl.
  43. ('Part 2: %1' % {part2}) displayNl.
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement